Red Gate forums :: View topic - Guidance needed with PInvoke with pointers
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

Guidance needed with PInvoke with pointers

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
chris123



Joined: 27 Aug 2011
Posts: 3

PostPosted: Sat Aug 27, 2011 4:51 pm    Post subject: Guidance needed with PInvoke with pointers Reply with quote

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:

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:

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

ret = InitialCommon( name, func );   


I think I need

Code:

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

Dim pntName As IntPtr = Marshal.StringToHGlobalAnsi("ComputerName")


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

Code:

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

Quote:

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
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6578

PostPosted: Mon Aug 29, 2011 10:34 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
chris123



Joined: 27 Aug 2011
Posts: 3

PostPosted: Tue Aug 30, 2011 10:26 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Brian Donahue



Joined: 23 Aug 2004
Posts: 6578

PostPosted: Wed Aug 31, 2011 9:28 am    Post subject: Reply with quote

Yeah, in C you would probably pass in &name, which would pass in the address. I think Marshal calls do this stuff automagically.
Back to top
View user's profile Send private message
chris123



Joined: 27 Aug 2011
Posts: 3

PostPosted: Wed Sep 07, 2011 11:16 pm    Post subject: Reply with quote

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

Chris
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group