473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

multi byte char * to csharp string


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

Nov 15 '05 #1
2 7404
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"caviar" <caviar-at-xsfourall.nl> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...

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

Nov 15 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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!
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
5
by: PEK | last post by:
I need some code that convert a multi-byte string to a Unicode string, and Unicode to multi-byte. I work mostly in Windows and know how to solve it there, but I would like to have some platform...
2
by: Victor Nazarov | last post by:
Assuming my locale has enough info about codepage and multi-byte charecters, how should I compare (collate) multi-byte strings (strings of multi-byte charecters with zero-byte at the end) in ISO...
11
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
4
by: Bob Altman | last post by:
If I create a new Win32 Console project (unmanaged C++, Visual Studio 2005), and add the following to the main program: // Add this above the main routine #include <windows.h // Add this at the...
2
by: Mirco Wahab | last post by:
After reading through some (open) Intel (CPU detection) C++ source (www.intel.com/cd/ids/developer/asmo-na/eng/276611.htm) I stumbled upon a sketchy use of multibyte characters - - - - - - - - -...
5
by: =?Utf-8?B?amM=?= | last post by:
Hello, This compiles OK using Multi-Byte character set, but when I switch to Unicode I get an error. char reply = _T("olleh"); I know this will fix the error with the Unicode compile, wchar_t...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.