如何使用函数指针作为参数将函数添加到ATL DLL [英] How to add a function to an ATL DLL with function pointer as parameter

查看:112
本文介绍了如何使用函数指针作为参数将函数添加到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.

推荐答案

它取决于应用程序的体系结构。

任何COM方法在其方法中仅接受已知参数,这些参数是原始数据类型,基于VARIANT或用户定义的接口。因此,如果要在其实现中调用某个方法,则必须在其他某个接口中进行定义。并且您的COM接口方法应该使用指针传递给另一个接口,而不是方法。尝试创建.idl文件并使用/ TLBOUT参数进行编译。

让我们看一个例子。

It depends on architecture of your application.
Any COM method accepts only "known" parameters in its methods which are primitive datatypes, VARIANT-based or user defined interfaces. So if you want to invoke some method in its implementation it must be defined in some other interface. And your COM-interface method should pass with pointer to another interface, not method. Try to create .idl file and compile it with /TLBOUT parameter.
Lets see an example.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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