473,322 Members | 1,610 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,322 software developers and data experts.

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 12827

"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.