Hi,
I'm trying to pass a string array to a third-party legacy c dll.
The C function in the dll is defined as follows:
unsigned short Init(int argc, char far * far argv);
And I've mapped this in VB.NET as
Declare Auto Function Init Lib "some.dll" Alias "Init" (ByVal int, ByVal intptr) As long
Dim args As String = m1 & Chr(0) & m2 & Chr(0)
Dim gh As GCHandle = GCHandle.Alloc(args, GCHandleType.Pinned)
Dim p As IntPtr = gh.AddrOfPinnedObject()
' Errors on the next line
Dim l As Long = Init(2, p)
gh.Free()
The error I get is:
An unhandled exception of type 'System.NullReferenceException' occurred in Test 2.exe
Additional information: Object reference not set to an instance of an object.
I can pass strings to my own c dll's but feel out of my depth when it comes to passing to these third-party legacy functions. I have tried lots of variations on this code but can't get any of them to work. Originally I user a standard String array eg dim s(1) as string, but I couldn't find a way to convert this to an IntPtr.
I'm new to VB.NET so any help would be greatly appreciated.
Regard
EC
I'm trying to pass a string array to a third-party legacy c dll.
The C function in the dll is defined as follows:
unsigned short Init(int argc, char far * far argv);
And I've mapped this in VB.NET as
Declare Auto Function Init Lib "some.dll" Alias "Init" (ByVal int, ByVal intptr) As long
Dim args As String = m1 & Chr(0) & m2 & Chr(0)
Dim gh As GCHandle = GCHandle.Alloc(args, GCHandleType.Pinned)
Dim p As IntPtr = gh.AddrOfPinnedObject()
' Errors on the next line
Dim l As Long = Init(2, p)
gh.Free()
The error I get is:
An unhandled exception of type 'System.NullReferenceException' occurred in Test 2.exe
Additional information: Object reference not set to an instance of an object.
I can pass strings to my own c dll's but feel out of my depth when it comes to passing to these third-party legacy functions. I have tried lots of variations on this code but can't get any of them to work. Originally I user a standard String array eg dim s(1) as string, but I couldn't find a way to convert this to an IntPtr.
I'm new to VB.NET so any help would be greatly appreciated.
Regard
EC