如何调用静态成员函数作为dll中的参数 [英] how to call static member function as a parameter in dll

查看:145
本文介绍了如何调用静态成员函数作为dll中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

头文件

class MainClass
{

public:
    MainClass(void);
    ~MainClass(void);
public:
    void RegCallBack();
    static void __stdcall CB_CardEncode2(unsigned int nChannel,ISIL_ENCODE_DATA *pEncData,void *pContext);
};



Cpp文件



Cpp file

extern "C"
{

void MainClass:: CB_CardEncode2(unsigned int nCh,ISIL_ENCODE_DATA *pEncData,void *pContext)
{
    ////////////FILEcreate/////////

    FILE *P=fopen("c:/Record/TP5.txt","w");
    fprintf(P, "i am card encode2!!!");
        fclose(P);
}

void RegCallBack()
{
    ISIL_Card_RegEncode2Callback(CB_CardEncode2,NULL);
}




我正在创建一个dll,我的问题是我正在将ISIL_Card_RegEncode2Callback(CB_CardEncode2,NULL)中的静态成员函数CB_CardEncode2作为参数传递;
但是CB_CardEncode2成员函数未调用.
我不知道如何将静态成员函数作为其他函数中的参数传递,以及如何调用CB_CardEncode2静态成员函数.

谢谢......






我必须输入RegcallBack()的入口点 就像
int __stdcall RegCallBack();





Hi,
i am creating a dll,my problem is that i am passing as a parameter a static member function CB_CardEncode2 in ISIL_Card_RegEncode2Callback(CB_CardEncode2,NULL);
but CB_CardEncode2 member function is not calling.
i dont have idea how to pass a static member function as a parameter in other function and how to call CB_CardEncode2 static member function.

Thanks....





Hi,
I have to make entry point of the RegcallBack()
like
int __stdcall RegCallBack();



__declspec(dllexport)  int __stdcall RegCallBack()
{
        ISIL_Card_RegEncode2Callback1(MainClass::CB_CardEncode2,NULL);
    //ISIL_Card_RegReadEncodeCallback(CB_CardReadEncode, NULL);
    return TRUE;

}


但是当编译出现错误时,
请帮助我.给我任何例子.

谢谢



but when the compile then gives error,
please help me.give me any example.

Thanks


when i am using classscope ISIL_Card_RegEncode2Callback1(MainClass::CB_CardEncode2,NULL);
then give this error
Error   1   error C2664: 'ISIL_Card_RegEncode2Callback1' : cannot convert parameter 1 from 'void (__stdcall *)(unsigned int,ISIL_ENCODE_DATA *,void *)' to 'CALL'   c:\documents and settings\administrator\desktop\test\dll_prj\dll_prj\test111\carddemo\mainclass.cpp 180 1   CardDemo



并且在使用
时 ISIL_Card_RegEncode2Callback1((CALL)MainClass :: CB_CardEncode2,NULL);
然后成功
无法读取CB_CardEncode2函数.
我要去哪里错了.请帮帮我...


谢谢



and when using
ISIL_Card_RegEncode2Callback1((CALL)MainClass::CB_CardEncode2,NULL);
then succesful
CB_CardEncode2 function not read.
where am i going wrong .please help me......


thanks

推荐答案

typedef void (__stdcall *CALLBACK)(int, void*);

void Global(CALLBACK pfn)
{
	pfn(10, 0);
}

class MainClass
{
public:
	void Reg();
	static void __stdcall Callback(int i, void* p);
};

void MainClass::Reg()
{
	Global(Callback);
}

void MainClass::Callback(int i, void* p)
{
	cout << i << " " << p << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	MainClass m;
	m.Reg();

	return 0;
}


我的问题很含糊,无法通过提供的信息进行故障排除.总的来说,我给出了一个代码片段,用于将类静态函数调用为回调.

I question is very vague and can''t be trouble-shooted with the info provided. In general I am giving a code snippet for calling a class static function as a callback.

#include <iostream>
using namespace std;

class MyClass
{
public:
   static int StaticFun(int i)
   {
      cout<<i<<endl;
      return 0;
   }
};

typedef int (*FN)(int);
void RegCallBack(FN pFn)
{
   pFn(100);
}

void main()
{
  RegCallBack(MyClass::StaticFun);
}



希望这会有所帮助.



Hope this helps.


这篇关于如何调用静态成员函数作为dll中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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