multi byte char * to csharp string

C

caviar

I'm trying to read in a ref parameter from a native dll, its working in vb
if i use the kernel32 functions below transforming the ref param to a vb
string:

Now, i want to skip this vb dll and use the native dll directly from c#
[DllImport("pafutw32.dll")] // CharSet = CharSet.Auto, CallingConvention
=CallingConvention.StdCall )]

public static extern short PAF_GetRecord(short listndx, short recno,
[MarshalAs(UnmanagedType.LPWStr)]ref string pafrec, ref short pafreclen );

But whatever i try i cant read this ref string pafrec out, it will keep
empty or it has the length but it doenst show anything.. So, the thrick
should be in the MultiByteToWidechar and MultiByteToUnicode function..

So does anyone have an idea how to declare the DLLImport statement so it
will succesfully Marshall this _ref string pafrec_, which seems to be a
MultiByte string.

Regards h

************original c from the .h file

PREFIX short PASFIX PAF_GetRecord(short listndx, long recno,char *pafrec,
short pafreclen );



********* the vb helper functions.

Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage
As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal
cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As
Long) As Long

Public Function MultiByteToUnicode(st As String, cpg As Long) As Variant

Dim stBuffer As String
Dim cwch As Long
Dim pwz As Long
Dim pwzBuffer As Long
Dim pwzBufferSize As Long
Dim b() As Byte

b = StrConv(st, vbFromUnicode)
pwz = VarPtr(b(0))
cwch = MultiByteToWideChar(cpg, 0, pwz, -1, ByVal 0&, ByVal 0&)

pwzBufferSize = cwch
stBuffer = String(pwzBufferSize, " ")
pwzBuffer = StrPtr(stBuffer)
cwch = MultiByteToWideChar(cpg, 0, pwz, -1, pwzBuffer,
pwzBufferSize)
MultiByteToUnicode = Left(stBuffer, cwch - 1)

End Function
 
N

Nicholas Paldino [.NET/C# MVP]

caviar,

I'm assuming that the pafrec parameter is not allocated by the function,
and is a buffer that is passed into the function. The declaration you want
is:

[DllImport("pafutw32.dll", CharSet=CharSet.Ansi)]
public static extern short PAF_GetRecord(
short listndx,
int recno,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pafrec,
short pafreclen);

Your second parameter was being marshaled incorrectly. A long in C is
an int in C# (they are both 32 bits). Also, you need to use a StringBuilder
because you are expecting the value to change and you want to see that
change reflected.

Hope this helps.
 
C

caviar

Nicholas Paldino said:
caviar,

I'm assuming that the pafrec parameter is not allocated by the function,
and is a buffer that is passed into the function. The declaration you want
is:

[DllImport("pafutw32.dll", CharSet=CharSet.Ansi)]
public static extern short PAF_GetRecord(
short listndx,
int recno,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pafrec,
short pafreclen);

Your second parameter was being marshaled incorrectly. A long in C is
an int in C# (they are both 32 bits). Also, you need to use a StringBuilder
because you are expecting the value to change and you want to see that
change reflected.

Hope this helps.

:) Yes it did help!

Thank you very much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top