CumulusGranitus
asked on
How to pass chars from unmanaged DLL to VB.NET
I have to read chars in VB.NET from an unmanaged DLL and convert them to a string. How do i pass the chars to VB.NET?
The only example i have is in C++:
API declaration in C++:
extern unsigned char __stdcall ReadXX(unsigned char *sn);
access example of the function from C++:
unsigned char sn[50];
memset(sn,0,sizeof(sn));
ReadXX(sn);
The only example i have is in C++:
API declaration in C++:
extern unsigned char __stdcall ReadXX(unsigned char *sn);
access example of the function from C++:
unsigned char sn[50];
memset(sn,0,sizeof(sn));
ReadXX(sn);
StringBuilder should do the trick.
ASKER
May be (but how?). Meanwhile i have this solution:
<DllImport("Lib123.dll")> Public Sub ReadXX(ByVal ptr As IntPtr)
End Sub
Dim ptr As IntPtr
Dim n As Integer = 50
ptr = Marshal.AllocHGlobal(n)
ZeroMemory(ptr, CType(n, IntPtr))
ReadXX(ptr)
Marshal.FreeHGlobal(ptr)
It works very well.
<DllImport("Lib123.dll")> Public Sub ReadXX(ByVal ptr As IntPtr)
End Sub
Dim ptr As IntPtr
Dim n As Integer = 50
ptr = Marshal.AllocHGlobal(n)
ZeroMemory(ptr, CType(n, IntPtr))
ReadXX(ptr)
Marshal.FreeHGlobal(ptr)
It works very well.
Hi
Try:
<DllImport("Lib123.dll")> Public Sub ReadXX(ByVal str As StringBuilder)
Usage:
Dim str as New StringBuilder();
ReadXX(str)
Console.WriteLine(str.ToSt ring)
Try:
<DllImport("Lib123.dll")> Public Sub ReadXX(ByVal str As StringBuilder)
Usage:
Dim str as New StringBuilder();
ReadXX(str)
Console.WriteLine(str.ToSt
ASKER
Thanks, i will try this good looking solution on Monday.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
both solutions are working.
Thanks for the hint.
Thanks for the hint.