Guidance needed with PInvoke with pointers

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

Guidance needed with PInvoke with pointers

Postby chris123 » Sat Aug 27, 2011 4:51 pm

Hi,

I am trying to interface a native DLL with my VB.Net application using PInvoke. I think I have several of the simpler methods working ok, but am struggling with an initialization method that uses pointers.

The method is described as:
Code: Select all
INT32   InitialCommon ( UINT8 *name, INT32 *func )

UINT8   *name   Own name (maximum 8 characters)
INT32   *func;   Pointer storing function code to be used


and the DLL comes with some sample code:

Code: Select all
INT32           func[3];
int            ret;
UINT8         name[8+1] = {"ComputerName"};

ret = InitialCommon( name, func );   


I think I need

Code: Select all
<DllImport("C:\\ChartPlot\\bin\\dll\\Fnet.dll", EntryPoint:="_InitialCommon@8", _
       SetLastError:=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:=True, _
       CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function InitialCommon(ByVal pntName As IntPtr, ByVal pntFunc As IntPtr) As Int32
       
    End Function


I have tried:
Code: Select all
Dim pntName As IntPtr = Marshal.StringToHGlobalAnsi("ComputerName")


to create the first pointer, with various ways of creating the second pointer, including:

Code: Select all
Dim func(2) As Integer

Dim gcHandle As GCHandle = gcHandle.Alloc(func, GCHandleType.Pinned)

Dim pntFunc As IntPtr = gcHandle.AddrOfPinnedObject()


However with all the options I have tried I keep getting

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Which I believe is due to a mismatch somewhere.

Can anyone point me in the right direction please?

Thanks

Chris
chris123
 
Posts: 3
Joined: Sat Aug 27, 2011 3:48 pm

Postby Brian Donahue » Mon Aug 29, 2011 10:34 am

It looks like "name" is a fixed-size buffer of 9 bytes rather than a pointer. I'd try declaring name as a byte array rather and an intptr and use System.Text.ASCIIEncoding.GetByteArray() on your string and make sure it isn't bigger than 9 bytes.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby chris123 » Tue Aug 30, 2011 10:26 pm

Thanks for the guidance - as you may have realised my knowledge of C is very limited, I took the asterisk to mean that the variable was a pointer.

I will try a byte array and let you know.

Thanks

Chris
chris123
 
Posts: 3
Joined: Sat Aug 27, 2011 3:48 pm

Postby Brian Donahue » Wed Aug 31, 2011 9:28 am

Yeah, in C you would probably pass in &name, which would pass in the address. I think Marshal calls do this stuff automagically.
Brian Donahue
 
Posts: 6670
Joined: Mon Aug 23, 2004 10:48 am

Postby chris123 » Wed Sep 07, 2011 11:16 pm

Yes, that sorted it. Many thanks for your help.

Chris
chris123
 
Posts: 3
Joined: Sat Aug 27, 2011 3:48 pm


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests