473,799 Members | 3,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("pafu tw32.dll")] // CharSet = CharSet.Auto, CallingConventi on
=CallingConvent ion.StdCall )]

public static extern short PAF_GetRecord(s hort listndx, short recno,
[MarshalAs(Unman agedType.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 MultiByteToWide char and MultiByteToUnic ode 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

************ori ginal c from the .h file

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

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

Private Declare Function MultiByteToWide Char 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 MultiByteToUnic ode(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 = MultiByteToWide Char(cpg, 0, pwz, -1, ByVal 0&, ByVal 0&)

pwzBufferSize = cwch
stBuffer = String(pwzBuffe rSize, " ")
pwzBuffer = StrPtr(stBuffer )
cwch = MultiByteToWide Char(cpg, 0, pwz, -1, pwzBuffer,
pwzBufferSize)
MultiByteToUnic ode = Left(stBuffer, cwch - 1)

End Function

Nov 15 '05 #1
2 7486
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("pafu tw32.dll", CharSet=CharSet .Ansi)]
public static extern short PAF_GetRecord(
short listndx,
int recno,
[MarshalAs(Unman agedType.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.co m

"caviar" <caviar-at-xsfourall.nl> wrote in message
news:eb******** ******@TK2MSFTN GP11.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("pafu tw32.dll")] // CharSet = CharSet.Auto, CallingConventi on
=CallingConvent ion.StdCall )]

public static extern short PAF_GetRecord(s hort listndx, short recno,
[MarshalAs(Unman agedType.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 MultiByteToWide char and MultiByteToUnic ode 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

************ori ginal c from the .h file

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

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

Private Declare Function MultiByteToWide Char 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 MultiByteToUnic ode(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 = MultiByteToWide Char(cpg, 0, pwz, -1, ByVal 0&, ByVal 0&)

pwzBufferSize = cwch
stBuffer = String(pwzBuffe rSize, " ")
pwzBuffer = StrPtr(stBuffer )
cwch = MultiByteToWide Char(cpg, 0, pwz, -1, pwzBuffer,
pwzBufferSize)
MultiByteToUnic ode = Left(stBuffer, cwch - 1)

End Function

Nov 15 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP12.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("pafu tw32.dll", CharSet=CharSet .Ansi)]
public static extern short PAF_GetRecord(
short listndx,
int recno,
[MarshalAs(Unman agedType.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
3795
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 in a string is defined by a Unicode scalar value, also called ...
5
8504
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 independent code too. I have tried with mbtowcs/wctombs but I'm not satisfied with the result. If wctombs finds a character that can't be converted it return -1, and stops. I would like to replace such of characters with some special character...
2
2200
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 C99? Thanks in advance Vir
11
30583
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 char into a hex value and place that value into the byte. Thanks for your help
4
5402
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 "System.Text.Encoding.ASCII.GetString(bytearray)" of .Net library, we found that the char (delimiters) specified above are replaced with different char.
2
4382
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. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the fields I decode tells me which the latter fields are). The code I have converts a single byte of...
4
2110
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 top of the file // Add this to the main routine MessageBox(NULL, "A", "B", MB_OK); The compiler (Visual Studio 2005) complains that it can't convert parameter
2
5555
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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 260: unsigned int VendorID = {0, 0, 0}; try // If CPUID instruction is supported {
5
1627
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 reply = _T("olleh"); but this causes many other conversion problems in the program. Is there some other conversion macro that I could use instead of _T(),
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.