开发者

VB6 DLL takes callback as Integer, VB.NET requires delegate reference type

开发者 https://www.devze.com 2022-12-14 23:42 出处:网络
I have an issue with a开发者_开发知识库 third-party COM+ DLL meant to be used from VB6, where it has a function to set a Callback for a hardware event.However, I\'m using VB.NET, and AddressOf now ret

I have an issue with a开发者_开发知识库 third-party COM+ DLL meant to be used from VB6, where it has a function to set a Callback for a hardware event. However, I'm using VB.NET, and AddressOf now returns a reference type instead of an integral type, which means that the setCallback function on the COM+ DLL apparently can't be used.

Is there a way around this problem (I don't have VB6 available to develop some sort of wrapper with), or will I have to find a different 3rd-party DLL in order to get this to work? For reference, I'm trying to access the LCD on a Logitech G15(v2) keyboard.

For Reference, here is the setCallback function and the callback's prototype itself:

Public Sub setCallback(funcAddr As Integer)
Public Sub LCDbuttonPress(ByVal butStates As Integer)


I think if you declare your setCallback function like this:

Public Sub setCallback([MarshalAs(UnmanagedType.FunctionPtr)]funcAddr as function)

I'm not a VB guy, so I don't know the exact syntax. But the basic idea here is that in VB.Net you declare the prototype as taking a delegate, even a delegate of the correct type. But then you use the MarshalAs attribute to tell the marshalling code to treat it as a function pointer (actually a Integer) on the other side. I do this in C# to pass callbacks to C++ code and it works just fine.

For instance this in C#

public delegate int MyProgress(double dPercentComplete);
...
int WaitWithProgress([MarshalAs(UnmanagedType.FunctionPtr)]MyProgress pfn);

Shows up on the C++ side of the house as this

HRESULT __stdcall WaitWithProgress (long pfn, int * pRetVal);

Note the comments. The actual solution to this problem turned out to be Marshal.GetFunctionPointerForDelegate() And be careful to read the docs on this. If you use this method, it becomes your responsibility to make sure that the delegate isn't garbage collected before you setCallback(NULL)

0

精彩评论

暂无评论...
验证码 换一张
取 消