本文介绍了如何使用函数指针作为参数将函数添加到ATL DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我尝试添加一个带有函数指针的函数作为我的ATL C ++项目的参数。

不幸的是,VS2008没有提供函数指针参数类型! :-(

在我的非ATL DLL库中,我将我的函数声明如下:

Hello,
I tried to add a function with a function pointer as parameter to my ATL C++ project.
Unfortunately, VS2008 doesn''t provide function pointer as parameter type! :-(
In my non-ATL DLL library, I declared my function as following:

void SetCashierDisplayEventHandler(void (*CashierDisplayEvent)(char Text[1024]));



我用这种方式实现了这个功能:


And I implemented the function this way:

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



是否有可能在ATL项目中做我想做的事情?

非常感谢您的建议。

问候。


Is it possible to do what I want to do in an ATL project?
Thank you very much in advance for your suggestions.
Regards.

推荐答案

library IMParameterLib
{
	importlib("stdole2.tlb");

        // Test Interface
	[
		odl,
		uuid(33377793-AABB-CCCC-DDDD-F0000000000F),
		version(1.0),
		helpstring("Test method interface"),
		dual,
		nonextensible,
		oleautomation
	]
    interface IMethodTest : IDispatch{
        [id(0x00000009), helpstring("parameter function")]
        HRESULT ExchangeData([in] BSTR data);

        [id(0x00000010), helpstring("test function")]
        HRESULT SetTaskData([in] LONG type,
                            [in] void (*ExchangeData)(BSTR Text)
                            );
    };
}



在.tlb文件中编译之后我们会看到


After compilation in .tlb file we''ll see

interface IMethodTest : IDispatch {
        [id(0x00000009), helpstring("parameter function")]
        HRESULT ExchangeData([in] BSTR data);
        [id(0x60020001)]
        void __MIDL_0011(BSTR Text);
        [id(0x00000010), helpstring("test function")]
        HRESULT SetTaskData(
                        [in] long type,
                        [in] IMethodTest* ExchangeData);
    };



那么我们在编译后得到了什么?指向IMethodTest接口的指针!因此compiller只为它设置已知数据类型,而不是匿名函数(即使它在界面中定义),它显示为void __MIDL_0011(BSTR Text);



但是如果我们要进行圆顶更改,例如


So what we got after compilation? The pointer to IMethodTest interface! Therefore compiller sets only "known" data type for it, not an anonymous function (even if it defined within interface), which appears as void __MIDL_0011(BSTR Text);

but if we''ll do dome changes like

    [
		uuid(99933366-FFFF-EEEE-DDDD-EEEEEEEEEEEE),
		version(1.0),
	]
    interface IMethodToInvoke : IDispatch{
        [id(0x00000009), helpstring("parameter function")]
        HRESULT ExchangeData([in] BSTR data);
    };

	[
//...
	]
    interface IMethodTest : IDispatch{

        [id(0x00000010), helpstring("test function")]
        HRESULT SetTaskData([in] LONG type,
                            [in] IMethodToInvoke * method
                            );
    };



编译后我们会看到上面代码中的预期。

这是由COM技术保证的在应用程序之间提供稳定和安全的(在某些情况下)数据交换。


After compilation we''ll see what we expected in code above.
This is guaranteed by COM technology to provide stable and safe (in some cases) data exchange between applications.


这篇关于如何使用函数指针作为参数将函数添加到ATL DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:37