*Experts* Bucky Posted December 8, 2003 *Experts* Posted December 8, 2003 I've been trying to replicate some C++ code in VB.NET. The C++ code gets a function address by using GetProcAddress, then casts that into a delegate that then calls a callback function in the code. So far I can correctly get the procedure address into an IntPtr variable, but now I'm trying to create an instance of a function delegate based on this address. DirectCast won't take a value type (the procedure address) as a first parameter, so how else might I cast it? The solution may lie in the Marshal object, but I cannot find it. Here's the C++ code: // Here's the delegate declaration typedef ULONG (*GetAttachedDevicesProc) (ULONG* pDeviceCount, PTCHAR pBuffer, ULONG* pBufferSize); // And the instance of that delegate static GetAttachedDevicesProc gGetAttachedDevices; // This is the tricky part, it casts the proc address to an actual delegate gGetAttachedDevices = (GetAttachedDevicesProc) ::GetProcAddress (fgUSBLib, "PalmUsbGetAttachedDevices") And the VB.NET code I have so far: ' The delegate: Private Delegate Function GetAttachedDevicesDelegate(ByVal deviceCount As Integer, ByVal buffer As String, ByVal bufferSize As Integer) As Integer ' Getting the procedure address: procAddress = New IntPtr(GetProcAddress(moduleHandle.ToInt32(), "PalmUsbGetAttachedDevices")) ' Now how do I make a new instance of GetAttachedDevicesDelegate based on the address? Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Experts* Volte Posted December 8, 2003 *Experts* Posted December 8, 2003 Perhaps you should make the return type of 'GetProcAddress' a Delegate type. Quote
*Experts* Bucky Posted December 8, 2003 Author *Experts* Posted December 8, 2003 Oooh, good idea... unfortunately it didn't work. An error was thrown: "System.ArgumentException - Function pointer was not created by a Delegate." So it looks like it's closer than before... Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Experts* Volte Posted December 8, 2003 *Experts* Posted December 8, 2003 Try here: http://staff.develop.com/woodring/dotnet/#getprocaddress Quote
Recommended Posts