正确使用__stdcall函数

时间:2018-09-04 10:25:59

标签: c++ winapi calling-convention

我有一个客户端,该客户端连接到服务器并等待有关连接状态的答案。如果服务器更改状态,它将向客户端发送一条消息。 首先,客户端通过函数RegisterConnectionStatusDelegate注册自己,该函数定义如下:

MNS_SDK_API void  RegisterConnectionStatusDelegate(void (CALLBACK *cb)(int idx, int status));

The CALLBACK is defined in another class as: #define CALLBACK    __stdcall
The MNS_SDK_API  is defined in in another class as: #define MNS_SDK_API __declspec(dllimport) 

我的问题是,如何在应用程序中正确使用此RegisterConnectionStatusDelegate函数。

我当前的实现方式如下:

main.cpp

#include "test.h"

int main(int argc, char const *const *argv)
{
  test *client = new test();
  client->init();
}

test.h

class test: public CMnsApp
{
  public:
    test();
    ~test();
    void init();
    void connectToServer(const char *, const char *, int);
    void (CALLBACK *connectionStatus)(int idx, int status);
  private:
    CMnsApp *app;
};

test.cpp

#include "test.h"
#include <WinUser.h>
#include <Windows.h>

void CALLBACK connectionStatus(int idx, int status)
{
  if (status == 0)
    std::cout << "Connection OFF\n" << std::endl;
  else if (status == 1)
    std::cout << "Connection OK\n" << std::endl;
  else
    std::cout << "Connection status UNKNOWN\n" << std::endl;
}

void test::init()
{
  app = new CMnsApp();

  connectToServer("testClient", "localhost", 1390);

  // Process Windows events. typedef struct tagMSG in WinUser.h
  MSG msg;

 while (GetMessage(&msg, nullptr, 0, 0))
 {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
 }
}

void test::connectToServer(const char *clientName, const char* hostName, int port)
{
  RegisterConnectionStatusDelegate(connectionStatus);

  app->InitMnsClientApp(clientName, hostName, port);
}

如果我运行上述代码,则在创建连接后它会进入while循环,但不会调用函数connectionStatus

谢谢您的任何建议

0 个答案:

没有答案