views:

252

answers:

2

I need to call a function in an unmanaged .dll written in C lang from vb.net. The function declaration looks like this

LONG _stdcall ReadInfo(char *reply);

Now the behavior of this function is that it copies some data in argument "reply" and returns a numeric value which signals its pass/fail status. How do i pass it a string object so that it can copy data. Following is how i access this function.

Dim str as String obj.ReadDeviceInfo(str)

and the library is accessed this way...

Public Declare Auto Function LoadLibrary Lib "kernel32" (ByVal libFilePath As String) As Integer

Public Declare Function GetProcAddress Lib "kernel32" (ByVal ModuleHandle As Integer, ByVal ProcName As String) As Integer

Public Declare Function FreeLibrary Lib "kernel32" (ByVal ModuleHandle As Integer) As Integer


Public Function ReadDeviceInfo(ByRef reply As String) As Integer

    Dim MethodPointer As Integer
    MethodPointer = GetProcAddress(ModuleHandle, "ReadInfo")
    Dim deviceInfo As ReadInfo = Marshal.GetDelegateForFunctionPointer(MethodPointer, GetType(ReadInfo))
    Return deviceInfo.DynamicInvoke(reply)

End Function

When the call completes, returned status is absolutely fine but there is nothing in string "str". What is it that i am missing. I'm not sure about the string object that i am passing as argument. Any thoughts...

+2  A: 

Strings in .NET are immutable. Try passing a StringBuilder:

Public Function ReadDeviceInfo(ByRef reply As String) As Integer    
    ...

    Dim sb As New StringBuilder(1000)
    result = deviceInfo.DynamicInvoke(sb)

    reply = sb.ToString()
    Return result
End Function

From MSDN Magazine:

If the string parameter can be input and/or output, then use the System.StringBuilder type. The StringBuilder type is a useful class library type that helps you build strings efficiently, and it happens to be great for passing buffers to native functions that the functions fill with string data on your behalf. Once the function call has returned, you need only call ToString on the StringBuilder object to get a String object.

Alternatively, pin a character array and pass its IntPtr.

Frank Krueger
Thanks Franks, StringBuilder threw exception. This is how tried 2nd approach. Dim pnt As IntPtr = Marshal.AllocHGlobal(2000) ret = obj.ReadDeviceInfo(pnt)Private Delegate Function ReadInfo(ByRef reply As IntPtr) As IntegerPublic Function ReadDeviceInfo(ByRef reply As IntPtr) As Integer Dim MethodPointer As Integer MethodPointer = GetProcAddress(ModuleHandle, "ReadInfo") Dim deviceInfo As ReadInfo = Marshal.GetDelegateForFunctionPointer(MethodPointer, GetType(ReadInfo)) Return deviceInfo.DynamicInvoke(reply) End Function
Xience
You have to initialize the StringBuilder, use the constructor that takes the capacity.
Hans Passant
@nobugz: still didn't work with capacity initialization. My test case simply breaks, no exception is thrown.
Xience
A: 

Use the PInvoke Interop Assistant to automatically convert the C function into PInvoke declarations.

If you have a more detailed definition in C of the "data" that is returned, put that into PInvoke Assistant also. It can convert structure definitions, for example.

MarkJ