passing parameters to unmanaged C api from vb.net
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开发者_JAVA技巧 about the string object that i am passing as argument. Any thoughts...
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.
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.
These well-used functions demonstrate how to convert a string to a byte array, and pass it to a managed c++ function as a pinned IntPtr:
Public Function b_fromString(ByRef str As String) As Byte()
If Not str Is Nothing Then
Return System.Text.Encoding.Default.GetBytes(str, 0, str.Length)
Else
Return New Byte() {}
End If
End Function
Public Function cString(ByRef theString As String) As IntPtr
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(b_fromString(theString & Chr(0)), System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As IntPtr = GC.AddrOfPinnedObject
GC.Free()
Return ret
End Function
This style of function can be used in a managed c++ wrapper:
long libinterface::loadData(System::IntPtr bytes, long length)
{ return lib->functionName((char*)bytes.ToPointer(), length);
}
String length is usually optional when using a null-terminated string. Useful, if not perfect.
Submitted for reference.
extra - uploading other arrays and bitmaps:
Public Sub New(ByRef _obj As Array, ByRef read As Boolean, ByRef write As Boolean)
Me.size = Marshal.SizeOf(_obj(0))
Me.count = _obj.LongLength
Me.ptr = GCHandle.Alloc(_obj, GCHandleType.Pinned)
Me.id = lib.addBuffer(count * size, read, write, ptr.AddrOfPinnedObject)
End Sub
Public Function getimgPtr(ByRef img As Bitmap) As IntPtr
Dim f As System.Drawing.Imaging.BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), Imaging.ImageLockMode.ReadOnly, img.PixelFormat)
getimgPtr = f.Scan0
img.UnlockBits(f)
End Function
精彩评论