| Author |
Message |
chris123
Joined: 27 Aug 2011 Posts: 3
|
Posted: Sat Aug 27, 2011 4:51 pm Post subject: Guidance needed with PInvoke with pointers |
|
|
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 |
|
 |
Brian Donahue
Joined: 23 Aug 2004 Posts: 6369 Location: Red Gate Software
|
Posted: Mon Aug 29, 2011 10:34 am Post subject: |
|
|
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
Technical Support
Red Gate Software Ltd.
44 (0)870 160 0037 ext 8521
US and CAN 1-866-RED GATE ext 8521 |
|
| Back to top |
|
 |
chris123
Joined: 27 Aug 2011 Posts: 3
|
Posted: Tue Aug 30, 2011 10:26 pm Post subject: |
|
|
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 |
|
 |
Brian Donahue
Joined: 23 Aug 2004 Posts: 6369 Location: Red Gate Software
|
Posted: Wed Aug 31, 2011 9:28 am Post subject: |
|
|
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
Technical Support
Red Gate Software Ltd.
44 (0)870 160 0037 ext 8521
US and CAN 1-866-RED GATE ext 8521 |
|
| Back to top |
|
 |
chris123
Joined: 27 Aug 2011 Posts: 3
|
Posted: Wed Sep 07, 2011 11:16 pm Post subject: |
|
|
Yes, that sorted it. Many thanks for your help.
Chris |
|
| Back to top |
|
 |
|
|
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