C3374:不能把“功能”的地址,除非创建委托实例 [英] C3374: can't take address of 'function' unless creating delegate instance

查看:440
本文介绍了C3374:不能把“功能”的地址,除非创建委托实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是第三方库注册功能来注册一个回调有困难。我写在C ++ CLI,并访问用C或C ++库。

什么是上面的编译器错误是什么意思?

这是由供应商所定义的登记功能:

  MYCO INT WINAPI MyCo_Device_Register_CallbackFunc(LPVOID FUNC,LPVOID lpParam,DWORD面罩);
 

这是由供应商所定义的回调:

 无效的typedef(CALLBACK * MyCo_Device_CallbackProc)(DWORD ddStatus,L​​PVOID lpParam);
 

我的回调函数:

 公开引用类设备
{
    无效CallBackFunc(DWORD ddStatus,L​​PVOID lpParam){};
}
 

我的电话(这失败):

  MyCo_Device_Register_CallbackFunc((LPVOID)设备:: CallBackFunc,nullptr,0xFFFF的);
 

解决方案

如果我不得不打赌猜测,你想有管理的功能而采取的地址,并把它作为一个本地函数指针。这将无法工作。要么写本机桥(更难,可能没有什么优势),或(这是更简单的方法),创建一个委托并使用​​<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getfunctionpointerfordelegate.aspx"相对=nofollow> Marshal.GetFunctionPointerForDelegate 。不要忘记,委托有活路为本地code预计回调的持续时间,所以你很可能需要存储在某个地方。

呵呵,和引用类或值类中的所有功能的管理功能,以免不明确。

扩展答案覆盖意见的问题。第一步是建立一个匹配委托。对于您所提供的功能,这将是这样的:

 公共委托无效CallbackFuncDelegate(DWORD ddStatus,L​​PVOID lpParam);
 

然后,你需要创建一个:

  CallbackFuncDelegate德尔= gcnew CallbackFuncDelegate(CallbackFunc);
 

和如前所述previously,你应该存储在您的类作为成员,以确保它不会消失。最后,你需要得到函数指针,并传递了下去:

  MyCo_Device_Register_CallbackFunc((MyCo_Device_CallbackProc)元帅:GetFunctionPointerForDelegate(DEL).ToPointer(),0,0);
 

我觉得,涵盖一切。所有你需要的是一个使用命名空间系统;你应该罚款。

I am having difficulty using a thirdparty library registration function to register a callback. I am writing in C++ CLI, and accessing a library written in C or C++.

What does the above compiler error mean?

this is the registration function as defined by the vendor:

MYCO int WINAPI MyCo_Device_Register_CallbackFunc(LPVOID func, LPVOID lpParam, DWORD mask);

this is the callback as defined by the vendor:

typedef void (CALLBACK* MyCo_Device_CallbackProc)(DWORD ddStatus, LPVOID lpParam);

My callback function:

public ref class Device 
{
    void CallBackFunc(DWORD ddStatus, LPVOID lpParam) {};
}

My call (which fails):

MyCo_Device_Register_CallbackFunc((LPVOID) Device::CallBackFunc, nullptr, 0xFFFF);

解决方案

If I had to wager a guess, you're trying to taken the address of a MANAGED function and pass it as a NATIVE function pointer. That won't work at all. Either write a native bridge (harder and probably no advantage), or (and this is the easier approach), create a delegate and use Marshal.GetFunctionPointerForDelegate. Don't forget that the delegate has to stay alive for the duration that the native code expects to call back, so you most likely need to store it somewhere.

Oh, and all functions inside a ref class or value class are managed functions, in case that wasn't clear.

[EDIT] Expanding answer to cover questions in comments. The first step is to create a matching delegate. For the function you provided, it's going to look like this:

public delegate void CallbackFuncDelegate(DWORD ddStatus, LPVOID lpParam);

Then, you'll need to create one:

CallbackFuncDelegate del = gcnew CallbackFuncDelegate(CallbackFunc);

And as noted previously, you should store that IN your class as a member in order to make sure it doesn't vanish. Lastly, you need to get the function pointer and pass it down:

MyCo_Device_Register_CallbackFunc((MyCo_Device_CallbackProc) Marshal::GetFunctionPointerForDelegate(del).ToPointer(), 0, 0);

I think that covers everything. All you need is using namespace System; and you should be fine.

这篇关于C3374:不能把“功能”的地址,除非创建委托实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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