Works:
static void WINAPI ServiceStart(DWORD argc, LPTSTR* argv);
int main() {
SERVICE_TABLE_ENTRY DispatchTable[] = {
{"MyService", ServiceStart},
{NULL, NULL}
};
}
Doesn't work:
error C2440: 'initializing' : cannot convert from 'void (__stdcall Service::* )(DWORD,LPTSTR *)' to 'LPSERVICE_MAIN_FUNCTIONA'
class Service {
static void WINAPI ServiceStart(DWORD argc, LPTSTR* argv);
};
int main() {
SERVICE_TABLE_ENTRY DispatchTable[] = {
{"MyService", Service::ServiceStart},
{NULL, NULL}
};
}
You must use & in that context to obtain a correct pointer.
Use &Service::ServiceStart, not Service::ServiceStart.
One piece of advice: since you made it a static function of Service class, rename it as well. ServiceStart is unnecessarily long. I think Service is implied here. If so, then make it just Start:
class Service
{
static void WINAPI Start(DWORD argc, LPTSTR *argv);
};
And then use &Service::Start.
Related
I have a working example in single file with its spapidllm64.h but wanna split into separate files for easier coding. I have tried to reduce it into a minimal version as follows:
// spapidll.h
#define SPDLLCALL __stdcall
typedef void (SPDLLCALL *LoginReplyAddr)(char *user_id, long ret_code, char *ret_msg);
typedef void (SPDLLCALL *p_SPAPI_RegisterLoginReply)(LoginReplyAddr addr);
typedef int (SPDLLCALL *p_SPAPI_Login)();
//main.cpp
void SPDLLCALL LoginReply(char *user_id, long ret_code, char *ret_msg);
p_SPAPI_RegisterLoginReply SPAPI_RegisterLoginReply;
p_SPAPI_Login SPAPI_Login;
int main(void)
{
HINSTANCE hInst = LoadLibrary("spapidllm64.dll");
SPAPI_RegisterLoginReply = (p_SPAPI_RegisterLoginReply)GetProcAddress(hInst, "SPAPI_RegisterLoginReply");
SPAPI_RegisterLoginReply(LoginReply);
SPAPI_Login = (p_SPAPI_Login)GetProcAddress(hInst, "SPAPI_Login");
int rc = SPAPI_Login();
printf("\nSPAPI_Login: %d\n", rc);
Sleep(3000);
FreeLibrary(hInst);
return 0;
}
void SPDLLCALL LoginReply(char *user_id, long ret_code, char *ret_msg)
{
printf("\nLoginReply[%s]: Ret code: %d, Ret msg: %s",user_id, ret_code, (ret_code == 0)?"OK":ret_msg);
}
now I want to put those codes into a class like this:
// test.h
class ApiTester
{
public:
HINSTANCE hInst;
ApiTester(void){
hInst = LoadLibrary("spapidllm64.dll");
}
~ApiTester(void){
FreeLibrary(hInst);
}
void RegisterDLL();
int Login();
void SPDLLCALL LoginReply(char *user_id, long ret_code, char *ret_msg);
void SPDLLCALL ConnectingReply(long host_type, long con_status);
p_SPAPI_RegisterLoginReply SPAPI_RegisterLoginReply;
p_SPAPI_RegisterConnectingReply SPAPI_RegisterConnectingReply;
p_SPAPI_Login SPAPI_Login;
};
// test.c
#include "tester.h"
void ApiTester::RegisterDLL()
{
SPAPI_RegisterLoginReply = (p_SPAPI_RegisterLoginReply)GetProcAddress(hInst, "SPAPI_RegisterLoginReply");
SPAPI_RegisterLoginReply(LoginReply); // Error E0167 C3867
SPAPI_Login = (p_SPAPI_Login)GetProcAddress(hInst, "SPAPI_Login");
}
int ApiTester::Login()
{
int rc = SPAPI_Login();
printf("\nSPAPI_Login: %d\n", rc);
}
void SPDLLCALL ApiTester::LoginReply(char *user_id, long ret_code, char *ret_msg){
printf("\nLoginReply[%s]: Ret code: %d, Ret msg: %s",user_id, ret_code, (ret_code == 0)?"OK":ret_msg);
}
Then Windows VS C++ shows this error:
Error (active) E0167 argument of type "void (__stdcall ApiTester::*)(char *user_id, long ret_code, char *ret_msg)" is incompatible with parameter of type "LoginReplyAddr"
Error C3867 'ApiTester::LoginReply': non-standard syntax; use '&' to create a pointer to member
Not sure why it works in single file but couldn't work in this way. I thought it should be straight-forward(Hopefully, there is no typo). I'd be appreciate if there is any help.
typedef void (SPDLLCALL *LoginReplyAddr)(char *user_id, long ret_code, char *ret_msg);
typedef void (SPDLLCALL *p_SPAPI_RegisterLoginReply)(LoginReplyAddr addr);
According to the definition above, LoginReplyAddr is a function pointer, not a class member function pointer.
void ApiTester::RegisterDLL()
{
SPAPI_RegisterLoginReply = (p_SPAPI_RegisterLoginReply)GetProcAddress(hInst, "SPAPI_RegisterLoginReply");
SPAPI_RegisterLoginReply(LoginReply); // Error E0167 C3867
...
In this code, LoginReply is class member function pointer.
void (__stdcall ApiTester::*)(char *user_id ...
I guess that is the problem.
If you want to use the class member function as a callback, you also have to pass the 'this' of the class.
Or you can use the static member function.
The link below could help you.
Using a C++ class member function as a C callback function
On Debian I try to mock system function bind to bind an interface address to a socket. To set an ip address I think I have to use side effects on a parameter of bind. I use this code:
extern "C" {
int mysocket();
}
// --- mock bind -------------------------------------------
class BindInterface {
public:
virtual ~BindInterface() {}
virtual int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen) = 0;
};
class BindMock : public BindInterface {
public:
virtual ~BindMock() {}
MOCK_METHOD(int, bind, (int, const struct sockaddr*, socklen_t), (override));
};
BindInterface* ptrBindMockObj = nullptr;
int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
return ptrBindMockObj->bind(sockfd, addr, addrlen);
}
TEST(MockTestSuite, bind_address_to_a_socket)
{
using ::testing::_;
using ::testing::Return;
using ::testing::DoAll;
using ::testing::SetArgPointee;
using ::testing::SetArgReferee;
struct sockaddr_in my_addr = {0};
BindMock bindMockObj;
ptrBindMockObj = &bindMockObj;
my_addr.sin_family = AF_INET;
EXPECT_EQ(inet_pton(AF_INET, "192.168.55.66", &my_addr.sin_addr.s_addr), 1);
EXPECT_CALL(bindMockObj, bind(_, _, _))
.WillOnce(DoAll(SetArgPointee<1>((struct sockaddr*)my_addr),
SetArgReferee<2>(sizeof(my_addr)),
Return(0)));
EXPECT_EQ(mysocket(), EXIT_SUCCESS);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
It doesn't work because the compiler complains invalid cast as follows:
test_mylib.cpp: In member function ‘virtual void MockTestSuite_bind_address_to_a_socket_Test::TestBody()’:
test_mylib.cpp:52:42: error: invalid cast from type ‘sockaddr_in’ to type ‘sockaddr*’
52 | .WillOnce(DoAll(SetArgPointee<1>((struct sockaddr*)my_addr),
| ^~~~~~~~~~~~~~~~~~~~~~~~~
Seems I do not really understand what SaveArgPointee<N>(pointer) is doing. What I'm doing wrong with types and cast?
UPDATE:
With the suggestion from the answer of #Quarra to define the ACTION_P outside the TEST body I get two error messages within a bunch of stacked calls:
googlemock/include/gmock/gmock-actions.h:1116:56: error: static assertion failed: Argument must be a reference type.
1116 | static_assert(std::is_lvalue_reference<argk_type>::value,
| ^~~~~^
--- snip ---
test_mylib.cpp:33:54: error: no match for ‘operator=’ (operand types are ‘sockaddr’ and ‘const sockaddr_in’)
33 | ACTION_P(AssignSockAddr, param) { *(sockaddr*)(arg0) = param; }
| ~~~~~~~~~~~~~~~~~~~^~~~~~~
In file included from /usr/include/x86_64-linux-gnu/sys/socket.h:33,
from /usr/include/netinet/in.h:23,
from test_mylib.cpp:7:
I agree that it's strange that your example doesn't compile: it's trying to change the pointer to const socaddr, not const pointer...
You can define your own action and use it like that:
ACTION_P(AssignSockAddr, param) { *(sockaddr*)(arg0) = param; }
TEST(MockTestSuite, bind_address_to_a_socket) {
// [ rest of the test code here ]
EXPECT_CALL(bindMockObj, bind(_, _, _)).WillOnce(
DoAll(WithArg<1>(AssignSockAddr(my_addr)),
SetArgReferee<2>(sizeof(my_addr)),
Return(0)));
EXPECT_EQ(mysocket(), EXIT_SUCCESS);
}
I have defined the following structure:
struct com_NetworkStruct
{
enum com_NetworkStatus status;
char IPV4_Addr[COM_IPV4_ADDR_LENGTH];
char IPV4_Netmask[COM_IPV4_ADDR_LENGTH];
char IPV4_DNS1[COM_IPV4_ADDR_LENGTH];
char IPV4_DNS2[COM_IPV4_ADDR_LENGTH];
int IPV4_DHCP_Enabled;
int IPV6_Supported;
int IPV6_Enabled;
struct com_IPv6_Data *IPV6_Address_List;
char HW_Addr[COM_ETH_MAC_ADDR_LENGTH];
int MTU_Size;
int Link_Speed;
enum com_LinkType Link_Type;
};
And the following method definition:
int foo(struct com_NetworkStruct **netinfo);
This is the main function and how i call the function:
int main(int argc, char const *argv[])
{
int ret;
char data_aux[200];
struct com_NetworkStruct *netinfo = NULL;
ret = foo(&netinfo);
memset(data_aux, 0, sizeof(data_aux));
sprintf(data_aux, "%s", netinfo->IPV4_Addr);
cout<<string(data_aux)<<endl;
return 0;
}
The problem is when I try to read the data of the IPV4_Addr value since it sends me the following error:
Error: #289: no instance of constructor "std::basic_string<_CharT, Traits, Alloc>::basic_string [with CharT=char, Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list
argument types are: (const char [20], char [16])
I already tried this, but nothing works:
&netinfo->IPV4_Addr
netinfo.IPV4_Addr
string(netinfo->IPV4_Addr)
Here is my code:
// WorkDamnit.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class Scheduler
{
public:
typedef void (*function_ptr) (void);
struct Task
{
function_ptr function;
int numOfTasks;
};
void Init(Task *tasks, int numOfTasks);
private:
int _numOfTasks;
Task *_tasks;
};
void Scheduler::Init(Scheduler::Task *tasks, int numOfTasks)
{
_tasks = tasks;
_numOfTasks = numOfTasks;
}
void count() {};
Scheduler::Task task_list =
{
count, 1
};
Scheduler scheduler;
Scheduler.Init(Scheduler::Task &task_list,1);
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
I get the following errors from the compiler:
1>c:\users\evan\documents\visual studio 2012\projects\workdamnit\workdamnit\workdamnit.cpp(49): error C2143: syntax error : missing ';' before '.'
1>c:\users\evan\documents\visual studio 2012\projects\workdamnit\workdamnit\workdamnit.cpp(49): error C2059: syntax error : '.'
The compiler doesnt seem to like the line after the class object definition. When i try to call the init() member. All i can think of is that it has to do with the pointer to function reference. Maybe someone can shed some light on this for me?
You can call call functions/methods directly outside of other methods/functions.
Scheduler.Init(Scheduler::Task &task_list,1);
2 problems in this line.
The above seems to be outside of any function/method. For eg. you can put in inside main.
The line itself is not correct. So change it to
scheduler.Init(&task_list,1);
Usually you call a method on an object not a class name, except for static methods. You don't pass the parameter type while passing parameters to the method.
So the changed line in main will look like
int _tmain(int argc, _TCHAR* argv[])
{
scheduler.Init(&task_list,1);
return 0;
}
Line 49 should be:
scheduler.Init(Scheduler::Task &task_list,1); // note the lowercase 's': the object should be used, not the class
Also it should be within a function (maybe main in your case)
Anyone knows how to compile this example code under msvc2010 ? (supposedly compiles under GCC)
class cmdLine;
struct cmdList
{
const char *c;
const char *s;
const char *h;
void (cmdLine::*cmdFuncPtr)();
};
class cmdLine
{
public:
cmdLine();
static cmdList myCmdList[];
void test();
};
cmdLine::cmdLine()
{
}
void cmdLine::test()
{
}
cmdList cmdLine::myCmdList[] =
{
{"xxx", "yyy", "zzzzz", &cmdLine::test},
{"aaa", "bbb", "ccc", 0}
};
int _tmain(int argc, _TCHAR* argv[])
{
cmdLine c;
(c.myCmdList[0].*cmdFuncPtr) (); //error (why?)
}
I get error C2065: 'cmdFuncPtr' : undeclared identifier and dont know whats wrong ?
Use this syntax
(c.*c.myCmdList[0].cmdFuncPtr) ();
As cmdFuncPtr is a pointer to a method of cmdLine, it needs an instance of the class to be invoked on, which is c. At the same time, cmdFuncPtr is a member of cmdList, so it needs an instance of the class where it is stored, which is c.myCmdList[0]. That's why c shall be used twice in the expression.
The expression presented by OP parses as: "Invoke a method on an instance of a class in c.myCmdList[0] through a method pointer stored in a standalone variable cmdFuncPtr". Such variable doesn't exist, that's what the compiler complains about.