Transfer string data to unmanaged buffer

G

Guest

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*.
 
M

Mattias Sjögren

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 = source;


Mattias
 
M

Mattias Sjögren

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
 
G

Greg Young

Correct missed the fixed, the fastest code then is the last example on my
blog.

Cheers,

Greg
 
G

Guest

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 = abBytes;
else
rcstkas.xValue1 = 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 said:
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 = source;


Mattias
 
G

Guest

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 said:
Correct missed the fixed, the fastest code then is the last example on my
blog.

Cheers,

Greg
 
D

_DD

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


Mattias

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.
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top