Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

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?

"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*
Posted

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...

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...