I have some trouble with making a buffer in vb.net.
I use a C dll that requires me to give him a pointer to a buffer of
bytes where he is going to write bytes to, depending on the command i
give.
the import of the dll: with the C procedures in comments
' declarations for integration test.dll
' int __stdcall test_Open(char *device, long to);
<DllImport("test.dll")Private Shared Function test_Open(ByVal
device As String, ByVal timeout As Long) As Integer
End Function
' int __stdcall test_Command(int channel, int cc, char *cmdbuf,
char *rcvbuf, int *rcvlen, long to);
<DllImport("test.dll")Private Shared Function test_Command(ByVal
channel As Integer, ByVal cc As Integer, ByRef cmdbuff As String, ByRef
rcvbuf As buffer, ByRef rcvlen As Integer, ByVal timeout As Long) As
Integer
End Function
' int __stdcall test_Close(void);
<DllImport("test.dll")Private Shared Function test_Close() As
Integer
End Function
The problem i have is getting the C dll to write in rcvbuf. I tried a
lot:
1. as in this code I self declared a structure buffer that contains 500
bytes (and this works). the problem is that i had to declare 500 bytes
by using dim p000 as byte , p001 as byte, ... and so on. Not really
effective and I will be needing a buffer thats is much marger then 500
(not gonna make that large amount of structure, largest buffer will be
50000 bytes)
So i need to make this dynamic and manageable.
2. i used 1 byte. so that makes the command like this (only rcvbuf
type changed)
<DllImport("test.dll")Private Shared Function test_Command(ByVal
channel As Integer, ByVal cc As Integer, ByRef cmdbuff As String, ByRef
rcvbuf As byte, ByRef rcvlen As Integer, ByVal timeout As Long) As
Integer
End Function
This is executable but i only receive the first byte of the expected
answer. Which is useless.
3. so normally, i would think, i need an array of bytes to receive all
the bytes. But this doesnt work. System.ExecutionEngineException
occurs. Same fatal error i get when using a string.
4. Using a string i get
The runtime has encountered a fatal error. The address of the error was
at 0x79ef067e, on thread 0xdd8. The error code is 0xc0000005. This
error may be a bug in the CLR or in the unsafe or non-verifiable
portions of user code. Common sources of this bug include user
marshaling errors for COM-interop or PInvoke, which may corrupt the
stack.
Is this problem of vb.net cause vb can't reserve memory staticly and C
demands places in the memory where he will write his answer?
Is there a work around possible?