473,486 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Transfer string data to unmanaged buffer

I declared a struct with fixed size buffers to send it to an unmanaged function

[StructLayout (LayoutKind.Sequential)]
unsafe struct RCSTKAS
{
public fixed Byte xValue1 [12];
public fixed Byte xValue2 [30];
...
}

Before calling the external function that takes the struct as one of its
parameters I have to load some of the members. How can I copy for instance
the data of a String or a Byte[] to xValue1? I found no other solution than
writing an unsafe routine that defines 2 pointers and does the copying. Is
there a more elegant way to do this?

BTW: The other direction, copying data from the buffer to a String is no
problem thow as string offers a constructor taking a SByte*.
---
Burkhard
Apr 29 '06 #1
9 3841
How can I copy for instance
the data of a String or a Byte[] to xValue1?


How about one element at a time in a simple loop?

for (int i = 0; i < source.Length; i++)
dest.xValue1[i] = source[i];
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 29 '06 #2
Try using Buffer.BlockCopy() for the byte arrays, it is also faster than the
unmanaged code.

For strings the various Encoding objects return a byte array for you.
System.Text.ASCIIEncoding
http://msdn2.microsoft.com/en-US/lib...iencoding.aspx as
an example. You can get a byte array using the GetBytes method

Cheers,

Greg

"Burkhard" <Bu******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
I declared a struct with fixed size buffers to send it to an unmanaged
function

[StructLayout (LayoutKind.Sequential)]
unsafe struct RCSTKAS
{
public fixed Byte xValue1 [12];
public fixed Byte xValue2 [30];
...
}

Before calling the external function that takes the struct as one of its
parameters I have to load some of the members. How can I copy for instance
the data of a String or a Byte[] to xValue1? I found no other solution
than
writing an unsafe routine that defines 2 pointers and does the copying. Is
there a more elegant way to do this?

BTW: The other direction, copying data from the buffer to a String is no
problem thow as string offers a constructor taking a SByte*.
---
Burkhard

Apr 29 '06 #3
Also for performance questions on various ways of dealing with byte data ..

http://www.eggheadcafe.com/articles/20050325.asp
and follow up ..
http://geekswithblogs.net/gyoung/arc.../23/76161.aspx

Cheers,

Greg

"Burkhard" <Bu******@discussions.microsoft.com> wrote in message
news:E5**********************************@microsof t.com...
I declared a struct with fixed size buffers to send it to an unmanaged
function

[StructLayout (LayoutKind.Sequential)]
unsafe struct RCSTKAS
{
public fixed Byte xValue1 [12];
public fixed Byte xValue2 [30];
...
}

Before calling the external function that takes the struct as one of its
parameters I have to load some of the members. How can I copy for instance
the data of a String or a Byte[] to xValue1? I found no other solution
than
writing an unsafe routine that defines 2 pointers and does the copying. Is
there a more elegant way to do this?

BTW: The other direction, copying data from the buffer to a String is no
problem thow as string offers a constructor taking a SByte*.
---
Burkhard

Apr 29 '06 #4
>Try using Buffer.BlockCopy() for the byte arrays, it is also faster than the
unmanaged code.


It wont work for fixed size buffers since they aren't proper arrays.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 29 '06 #5
Correct missed the fixed, the fastest code then is the last example on my
blog.

Cheers,

Greg
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u%****************@TK2MSFTNGP05.phx.gbl...
Try using Buffer.BlockCopy() for the byte arrays, it is also faster than
the
unmanaged code.


It wont work for fixed size buffers since they aren't proper arrays.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 29 '06 #6
Yes, I thought of that. As I have to fill the receiving buffer with trailing
spaces, the complete loop would be

Byte[] abBytes = Encoding.Default.GetBytes (strValue1);
for (Int32 i = 0; i < 12; i++)
{
if (i < abBytes.Length)
rcstkas.xValue1[i] = abBytes[i];
else
rcstkas.xValue1[i] = 0x20;
}

Instead of writing that 50 times for 50 differnt strings I decided the code
would be more readable by writing 50 times

ByteArrayToBuffer (rcstkas.xValue1, Encoding.Default.GetBytes
(strValue1), 12);

Another question arising : Is there any language construct to avoid
repeating the length of the unmanaged buffer as a literal, 12 in this example?
--
Regards
Burkhard
"Mattias Sjögren" wrote:
How can I copy for instance
the data of a String or a Byte[] to xValue1?


How about one element at a time in a simple loop?

for (int i = 0; i < source.Length; i++)
dest.xValue1[i] = source[i];
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 30 '06 #7
fixed(byte *a = destination) {
fixed(byte *b = buffer) {
while(j < TimesToRun) {
byte *current = (byte *) b;
byte *dest = a;
byte *end = current + buffer.Length;
while(current < end) {
*dest = *current;
current ++;
dest++;
}
j++;
}
End = DateTime.Now;
}
}

being the fastest example.

Cheers,

Greg

"Greg Young" wrote:
Correct missed the fixed, the fastest code then is the last example on my
blog.

Cheers,

Greg
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u%****************@TK2MSFTNGP05.phx.gbl...
Try using Buffer.BlockCopy() for the byte arrays, it is also faster than
the
unmanaged code.


It wont work for fixed size buffers since they aren't proper arrays.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


May 1 '06 #8
_DD
On Sun, 30 Apr 2006 00:53:20 +0200, Mattias Sjögren
<ma********************@mvps.org> wrote:
Try using Buffer.BlockCopy() for the byte arrays, it is also faster than the
unmanaged code.


It wont work for fixed size buffers since they aren't proper arrays.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Mattias,

Nice reference to books on interop on your site. Just wanted to let
you know about a new book from Apress by Bruce Bukovics called
..NET 2.0 Interop Recipes. ISBN 1590596692.

Links (best prices):
http://www.bookpool.com/sm/1590596692
http://www.nerdbooks.com/item.php?id=1590596692

I'm not affiliated in any way. In fact, I'm only about 60 pages into
it, but so far it's great. Very pracical, very clear, with real-world
examples.
May 8 '06 #9
>Nice reference to books on interop on your site. Just wanted to let
you know about a new book from Apress by Bruce Bukovics called
.NET 2.0 Interop Recipes. ISBN 1590596692.


Thanks, I appreciate the tip. Hadn't seen that book before. Now I just
have to find time to update the site, it's way outdated.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 10 '06 #10

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

Similar topics

3
8702
by: Alessandro | last post by:
Hi all! I'm trying to write a file transfer application using TCP, but I'm having some problem to connect the client and the server. Sniffing with Ethereal I can see the packets sent from the...
8
5190
by: KRoy | last post by:
I have a password stored in the Registry encrypted using System.Security.Cryptography DES Algorithm. I supplied it a password and a Initialization Vector. I am trying to decrypt it using the...
0
327
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
2
4184
by: Bonzol | last post by:
vb.net 2003 Windows application We have a Client/Server system set up by communicating through a TCPClient object. The server loops continuously using a tcplistener device and each time a client...
5
8212
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
4
7562
by: Andrew Jackson | last post by:
I am writing a newsgroup client. I have the protocol figured out. But I get slow transfer speeds off any of the network objects read the data from For example one of the commands for a news...
13
2857
by: Creativ | last post by:
I've looked through this thread and still have quetions. Suppose In visual studio 2005, I write the following #pragam managed class ManagedWrapper { void CallUnmanagedMethod() // The unmanaged...
1
5558
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
1
7118
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
0
7100
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
6964
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...
1
6842
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...
0
7330
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...
1
4865
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
4559
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
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1378
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 ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.