如何从C ++调用dll中的类方法? [英] How do I call a class method in a dll from C++?

查看:704
本文介绍了如何从C ++调用dll中的类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ATL Project Wizard来开发我的dll。我想在我的C ++客户端中实例化我的抽象类PT2_B_db的实例,并从该类实例中调用纯虚方法DoubleValueString。如何编写客户端代码?我在客户端代码中包含哪个头文件?



扩展接口头代码可能会给出一些提示...



虚拟HRESULT __stdcall IPT2_B_db :: DoubleValueString(DOUBLE dblInputValue,

BSTR bstrFormatString,BSTR * bstrReturnString)= 0;



-------------------------------------------------- ----------

我在Visual Studio 2013中使用了ATL项目模板来创建PT2_B.dll。我试图调用的DoubleValueString方法是一部分该DLL中的CPT2_B_db类。我可以从Visual Basic调用此dll中的所有函数,引用PT2_BLib。但我需要能够在一个单独的程序中调用C ++中的函数。



我能够执行LoadLibrary()函数并获得一个有效的指针图书馆。但我无法构造我的函数指针的签名以正确调用GetProcAddress()。我不知道该怎么做。我怀疑我需要以某种方式包含类名。以下是项目中不同文件的各种定义。如何正确转换我的函数指针,调用GetProcAddress,最后执行函数?



来自我的PT2_B.idl文件......

[ id(10)] HRESULT DoubleValueString([in] DOUBLE dblInputValue,[in] BSTR bstrFormatString,[out,retval] BSTR * bstrReturnString);



来自我的PT2_B_db。 h file ...

STDMETHOD(DoubleValueString)(DOUBLE dblInputValue,BSTR bstrFormatString,BSTR * bstrReturnString);



来自我的PT2_B_db.cpp文件...

STDMETHODIMP CPT2_B_db :: DoubleValueString(DOUBLE dblInputValue,BSTR bstrFormatString,BSTR * bstrReturnString){}



来自我的PT2_B_i.h文件......

virtual / * [id] * / HRESULT STDMETHODCALLTYPE DoubleValueString(

/ * [in] * / DOUBLE dblInputValue,

/ * [in ] * / BSTR bstrFormatString,

/ * [retval] [out] * / BSTR * bstrReturnString)= 0;

I used the ATL Project Wizard to develop my dll. I want to intantiate an instance of my abstract class "PT2_B_db" in my C++ client and call the pure virtual method "DoubleValueString" from this class instance. How do I write the client code? Which header file do I include in my client code?

the expanded interface header code might give some hints...

virtual HRESULT __stdcall IPT2_B_db::DoubleValueString(DOUBLE dblInputValue,
BSTR bstrFormatString, BSTR *bstrReturnString) = 0;

------------------------------------------------------------
I used the ATL Project template in Visual Studio 2013 to create a "PT2_B.dll." The "DoubleValueString" method I’m trying to call is part of the "CPT2_B_db" class in that dll. I am able to call all the functions in this dll from Visual Basic, referencing the PT2_BLib. But I need to be able to call the functions from C++ in a separate program.

I’m able to execute the LoadLibrary() function and get a valid pointer to the library. But I’m having trouble structuring the signature of my function pointer to correctly call GetProcAddress(). I’m not sure how to do this. I suspect I need to include the class name somehow. Here are the various definitions from different files in the project. How do I correctly cast my function pointer, call GetProcAddress, and finally execute the function?

From my PT2_B.idl file…
[id(10)] HRESULT DoubleValueString([in] DOUBLE dblInputValue, [in] BSTR bstrFormatString, [out, retval] BSTR* bstrReturnString);

From my PT2_B_db.h file…
STDMETHOD(DoubleValueString)(DOUBLE dblInputValue, BSTR bstrFormatString, BSTR* bstrReturnString);

And from my PT2_B_db.cpp file…
STDMETHODIMP CPT2_B_db::DoubleValueString(DOUBLE dblInputValue, BSTR bstrFormatString, BSTR* bstrReturnString) {}

From my PT2_B_i.h file…
virtual /* [id] */ HRESULT STDMETHODCALLTYPE DoubleValueString(
/* [in] */ DOUBLE dblInputValue,
/* [in] */ BSTR bstrFormatString,
/* [retval][out] */ BSTR *bstrReturnString) = 0;

推荐答案

请参阅:

https://msdn.microsoft.com/en-us /library/81h27t8c.aspx [ ^ ],

HowTo:Export来自DLL的C ++类 [ ^ ]。



-SA
Please see:
https://msdn.microsoft.com/en-us/library/81h27t8c.aspx[^],
HowTo: Export C++ classes from a DLL[^].

—SA


这篇关于如何从C ++调用dll中的类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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