在C ++ DLL中实现事件 [英] Implement events in a C++ DLL

查看:232
本文介绍了在C ++ DLL中实现事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我必须在我的C ++ DLL中编写代码来实现外部代码使用的事件。

我实现了一个函数来将事件处理程序指针传递给DLL在主库.cpp文件中:

  extern     C  void  WINAPI SetEventHandlers( void (* CashierDisplayEvent)( char 文本[ 256 ]))
{
CSharedData :: CashierDisplayEventRef = CashierDisplayEvent;
}



我在另一个名为CSharedData的类中声明了函数指针如下:

  static   void (* CashierDisplayEventRef)( char 文本[ 256 ]); 



当我尝试编译它时,我收到以下错误:

错误LNK2001:symbole externenonrésolupublic:static void(__stdcall * CSharedData :: CashierDisplayEventRef)(char * const)(?CashierDisplayEventRef @ CSharedData @@ 2P6GXQAD @ ZA)ccvJIL.obj ccvJIL 



致命错误LNK1120:1 externesnonrésolusc:\ Project:\\ Visual Studio Projects \ JILtreibauf \ release.\\ccvJIL.dll 1 ccvJIL 



我怀疑有关C ++函数命名的问题,但我的知识在这个主题上是有限的。

欢迎提供帮助。

问候。

解方案
你可能已经忘记了在课堂上CSharedData你的CPP文件来定义静态指针。应该有一个定义:

  void (* CSharedData :: CashierDisplayEventRef)( char 文本[ 256 ])=  0 ; 



这就是未解决的外部来源。


你已经宣布了一个静态成员 CashierDisplayEventRef 但是您是否已经开始创建这样的成员?

需要在类声明之外定义和初始化静态成员。你在.cpp文件中有一行代码如下: -

 void(* CSharedData :: CashierDisplayEventRef)(char [256])= 0; 





不要过于字面意思地使用我的语法,我永远不会记得引用函数指针成员的正确方法,这就是为什么我通常使用 typedef 用于头文件中的函数指针类型。

 //在CSharedData声明之前的.h 

typedef void(* CashierEventCallback)(char [256]);

//在类声明中

static CashierEventCallback m_spEventCallbackFunc;

//在.cpp

CashierEventCallback CSharedData :: m_spEventCallbackFunc = 0;


Hello,
I have to write code in my C++ DLL to implement events used by external code.
I implemented a function to pass event handler pointer to the DLL in the main library .cpp file:

extern "C" void WINAPI SetEventHandlers(void (*CashierDisplayEvent)(char Text[256]))
{
	CSharedData::CashierDisplayEventRef = CashierDisplayEvent;
}


I declared the function pointer in another class named "CSharedData" as following:

static void (*CashierDisplayEventRef)(char Text[256]);


When I try to compile this, I get the following error:

error LNK2001: symbole externe non résolu "public: static void (__stdcall* CSharedData::CashierDisplayEventRef)(char * const)" (?CashierDisplayEventRef@CSharedData@@2P6GXQAD@ZA)	ccvJIL.obj	ccvJIL


fatal error LNK1120: 1 externes non résolus	c:\Projects\Visual Studio Projects\JILtreibauf\Release\ccvJIL.dll	1	ccvJIL


I suspect a problem with mangling of C++ function naming but my knowledge is limited in this subject.
Your help is welcome.
Regards.

解决方案

You have probably forgotten to define the static pointer in your cpp file of class CSharedData. There should be a definition:

void (*CSharedData::CashierDisplayEventRef)(char Text[256]) = 0;


That''s where the unresolved external originates from.


You''ve declared a static member CashierDisplayEventRef but have you actaully created such a member?
Static members need to be defined and initialised outside the class declaration. Do you have a line in a .cpp file somewhere like:-

void (*CSharedData::CashierDisplayEventRef)( char[256] ) = 0;



Don''t take my syntax too literally, I can never remember the correct way of refering to a function pointer member which is why I usually use a typedef for the function pointer type in the header file.

//in the .h before the CSharedData declaration

typedef void (*CashierEventCallback)( char[256] );

//inside the class declaration

static CashierEventCallback m_spEventCallbackFunc;

//in the .cpp

CashierEventCallback CSharedData::m_spEventCallbackFunc = 0;


这篇关于在C ++ DLL中实现事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆