473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

convert WCHAR in byte[] to string

hi,

I import a funtion fromm dll file:

dll function header :

DWORD WINAPI
Enumerate(
HANDLE hWDMHandle,
PNDIS_STATUS pNStatus,
PWCHAR pBuffer,
PUINT pBufferSize
)

in C# should be:
[DllImport("MyAPIdll", SetLastError=true)]
private static extern unsafe ulong Enumerate (
IntPtr g_hPCASIMHandle,
ulong* pNStatus,
void* pBuffer,
uint* pBufferSize);
und aufgerufen:

[CSHARP]
byte[] buf = new byte[2024];
uint iBytesRead = 0;
ulong ioResult;
ulong ndis_status;
uint buffsize;

unsafe
{
// create a void pointer to buf
fixed (void* pBuffer = buf)
{
ioResult=Enumerate(this.m_iHandle,
&ndis_status,pBuffer,&buffsize);
}
So how can i read buf or convert it to string ?
Nov 17 '05 #1
6 12847

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
hi,

I import a funtion fromm dll file:

dll function header :

DWORD WINAPI
Enumerate(
HANDLE hWDMHandle,
PNDIS_STATUS pNStatus,
PWCHAR pBuffer,
PUINT pBufferSize
)

in C# should be:
[DllImport("MyAPIdll", SetLastError=true)]
private static extern unsafe ulong Enumerate (
IntPtr g_hPCASIMHandle,
ulong* pNStatus,
void* pBuffer,
uint* pBufferSize);
und aufgerufen:

[CSHARP]
byte[] buf = new byte[2024];
uint iBytesRead = 0;
ulong ioResult;
ulong ndis_status;
uint buffsize;

unsafe
{
// create a void pointer to buf
fixed (void* pBuffer = buf)
{
ioResult=Enumerate(this.m_iHandle,
&ndis_status,pBuffer,&buffsize);
}
So how can i read buf or convert it to string ?


There is no need for this, but first some remarks.
1. A long in C# is a 64 bit entity in C it's 32 bit.
2. There is no need for unsafe in your declarations and your code.
3. strings in C# are w_char strings, PInvoke marshaling considers ANSI by
default, so you need to change this behavior.

Change your function declaration into:

[DllImport("MyAPIdll", SetLastError=true)]
private static extern uint Enumerate (
IntPtr g_hPCASIMHandle,
ref uint pNStatus,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pBuffer,
ref uint pBufferSize);

And use it as:

StringBuilder buf new StringBuilder( 2048);
uint iBytesRead = 0;
uintioResult;
uint ndis_status;
uint buffsize;

....
ioResult = Enumerate(this.m_iHandle, ref ndis_status, buf, ref buffsize);
string s = buf.ToString();

Willy.

Nov 17 '05 #2

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
hi,

I import a funtion fromm dll file:

dll function header :

DWORD WINAPI
Enumerate(
HANDLE hWDMHandle,
PNDIS_STATUS pNStatus,
PWCHAR pBuffer,
PUINT pBufferSize
)

in C# should be:
[DllImport("MyAPIdll", SetLastError=true)]
private static extern unsafe ulong Enumerate (
IntPtr g_hPCASIMHandle,
ulong* pNStatus,
void* pBuffer,
uint* pBufferSize);
und aufgerufen:

[CSHARP]
byte[] buf = new byte[2024];
uint iBytesRead = 0;
ulong ioResult;
ulong ndis_status;
uint buffsize;

unsafe
{
// create a void pointer to buf
fixed (void* pBuffer = buf)
{
ioResult=Enumerate(this.m_iHandle,
&ndis_status,pBuffer,&buffsize);
}
So how can i read buf or convert it to string ?


There is no need for this, but first some remarks.
1. A long in C# is a 64 bit entity in C it's 32 bit.
2. There is no need for unsafe in your declarations and your code.
3. strings in C# are w_char strings, PInvoke marshaling considers ANSI by
default, so you need to change this behavior.

Change your function declaration into:

[DllImport("MyAPIdll", SetLastError=true)]
private static extern uint Enumerate (
IntPtr g_hPCASIMHandle,
ref uint pNStatus,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pBuffer,
ref uint pBufferSize);

And use it as:

StringBuilder buf new StringBuilder( 2048);
uint iBytesRead = 0;
uintioResult;
uint ndis_status;
uint buffsize;

....
ioResult = Enumerate(this.m_iHandle, ref ndis_status, buf, ref buffsize);
string s = buf.ToString();

Willy.

Nov 17 '05 #3
Thanks Willy !
It works.
But i think i get only first vallue of pBuffer.
Maybe i need an array of;

StringBuilder[] buf;

How can i initializise it:
StringBuilder[] buf = new StringBuilder ....????

and:

[DllImport("MyAPIdll", SetLastError=true)]
private static extern uint Enumerate (
IntPtr g_hPCASIMHandle,
ref uint pNStatus,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pBuffer, // ???????
ref uint pBufferSize)
"Willy Denoyette [MVP]" wrote:

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
hi,

I import a funtion fromm dll file:

dll function header :

DWORD WINAPI
Enumerate(
HANDLE hWDMHandle,
PNDIS_STATUS pNStatus,
PWCHAR pBuffer,
PUINT pBufferSize
)

in C# should be:
[DllImport("MyAPIdll", SetLastError=true)]
private static extern unsafe ulong Enumerate (
IntPtr g_hPCASIMHandle,
ulong* pNStatus,
void* pBuffer,
uint* pBufferSize);
und aufgerufen:

[CSHARP]
byte[] buf = new byte[2024];
uint iBytesRead = 0;
ulong ioResult;
ulong ndis_status;
uint buffsize;

unsafe
{
// create a void pointer to buf
fixed (void* pBuffer = buf)
{
ioResult=Enumerate(this.m_iHandle,
&ndis_status,pBuffer,&buffsize);
}
So how can i read buf or convert it to string ?


There is no need for this, but first some remarks.
1. A long in C# is a 64 bit entity in C it's 32 bit.
2. There is no need for unsafe in your declarations and your code.
3. strings in C# are w_char strings, PInvoke marshaling considers ANSI by
default, so you need to change this behavior.

Change your function declaration into:

[DllImport("MyAPIdll", SetLastError=true)]
private static extern uint Enumerate (
IntPtr g_hPCASIMHandle,
ref uint pNStatus,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pBuffer,
ref uint pBufferSize);

And use it as:

StringBuilder buf new StringBuilder( 2048);
uint iBytesRead = 0;
uintioResult;
uint ndis_status;
uint buffsize;

....
ioResult = Enumerate(this.m_iHandle, ref ndis_status, buf, ref buffsize);
string s = buf.ToString();

Willy.


Nov 17 '05 #4

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
Thanks Willy !
It works.
But i think i get only first vallue of pBuffer.
Maybe i need an array of;

StringBuilder[] buf;

How can i initializise it:
StringBuilder[] buf = new StringBuilder ....????

and:

[DllImport("MyAPIdll", SetLastError=true)]
private static extern uint Enumerate (
IntPtr g_hPCASIMHandle,
ref uint pNStatus,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pBuffer, // ???????
ref uint pBufferSize)


No, you don't need a StringBuilder[].
What do you mean with the first value, do you mean the first character? How
are you looking at it, don't say in the debugger.

What is the length of the string in the StringBuilder?
int len = buf.ToString().Length;

Did you check the value in pBufferSize on return?

Willy.


Nov 17 '05 #5
i get:
pBufferSize: 772
pBuffer: \Device\{91E405CA-9D5E-4366-BB44-3B27E09C6C35}

i think i would get more, i don't know !?

regards
"Willy Denoyette [MVP]" wrote:

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
Thanks Willy !
It works.
But i think i get only first vallue of pBuffer.
Maybe i need an array of;

StringBuilder[] buf;

How can i initializise it:
StringBuilder[] buf = new StringBuilder ....????

and:

[DllImport("MyAPIdll", SetLastError=true)]
private static extern uint Enumerate (
IntPtr g_hPCASIMHandle,
ref uint pNStatus,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder pBuffer, // ???????
ref uint pBufferSize)


No, you don't need a StringBuilder[].
What do you mean with the first value, do you mean the first character? How
are you looking at it, don't say in the debugger.

What is the length of the string in the StringBuilder?
int len = buf.ToString().Length;

Did you check the value in pBufferSize on return?

Willy.


Nov 17 '05 #6

"centrino" <ce******@discussions.microsoft.com> wrote in message
news:D9**********************************@microsof t.com...
i get:
pBufferSize: 772
pBuffer: \Device\{91E405CA-9D5E-4366-BB44-3B27E09C6C35}

i think i would get more, i don't know !?

regards


I guess you do have more. Your pBufferSize is 772 ( don't know what exactly,
chars or bytes), the string is shorter anyway.
I guess the buffer returned contains strings delimitted with 'nulls', like
this...
\Device\{91E405CA-9D5E-4366-BB44-3B27E09C6C35}\0\0
\Device\{......................................... .................}\0\0

You can check the stringbuilders Length after return.
If I'm right, you can use ToString(int startindex, int length) to retrieve
the individual strings from the StringBuilders buffer.
Willy.
Nov 17 '05 #7

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

Similar topics

7
by: Dave Turner | last post by:
Ive used fwrite to store 4 bytes in a file: "Test" (or 0x54657374 / 1953719636). Its easy enough to use fread to get those 4 bytes, but the problem is I need to work with the data as a number and...
1
by: Swarup | last post by:
I am reading a file (txt, xml, gif, ico, bmp etc) byte by byte and filling it into a byte arry. Now i have to convert it into a string to store it in the database. I use...
8
by: FrzzMan | last post by:
How to convert back a string that have converted to byte using System.Text.Encoding.UTF8.GetBytes() string StringData = "This is a string"; byte ConvertedString =...
14
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
0
by: centrino | last post by:
hi, I import a funtion fromm dll file: dll function header : DWORD WINAPI Enumerate( HANDLE hWDMHandle, PNDIS_STATUS pNStatus,
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
1
by: Slippy27 | last post by:
How do I test a byte string in Python? I want to manually convert (no libraries or functions) a UTF-8 string into UTF-16. My basic solution is to read from the stream some number of UTF-8 bytes,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.