954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Loading a win32 native dll in C#: char* to byte[]

Greetings!

I have a win32 native dll (perhaps that's not the correct term - it's basically a C compiled dll) that I'm accessing in C#. The method signature of the C code (not the actual signature in the dll):

char* TestMethod()


I'd like to access thechar* as a byte[] in C#. I know how to access the char* as a string in C#:

[DllImport("DLLTest.dll")]
public static extern String ImportedTestMethod();


The main reason I want to do this is because thechar* output is encoded in UTF-8. If I get the actual bytes in C# then it's a simple matter to interpret them correctly and store it in the usual UTF-16 C# string.

Any ideas or workarounds? I can convert the C# string (from the above code) to a byte[] and interpret the encoding correctly but that seems very inefficient.

Thank you for your time and assistance :)

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
Solved Threads: 11
 

I have investigated using an IntPtr: I have no way of knowing the length of the data coming out of the dll method so I can't successfully copy the data to a byte[]. If there was a way to iterate over the data IntPtr provides access to then I would be able to find the string null terminator and copy to content to a byte[].

Is there a way to manipulate IntPtr to get a byte[] of the method output?

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
Solved Threads: 11
 

The very few times I've needed to do this, I've used an intermediary (written in managed C++).
I used the managed C++ to call and expose the unmanaged function and then call that from C#.

Here is an earlier post describing it.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

I have investigated using an IntPtr: I have no way of knowing the length of the data coming out of the dll method so I can't successfully copy the data to a byte[]. If there was a way to iterate over the data IntPtr provides access to then I would be able to find the string null terminator and copy to content to a byte[].

Is there a way to manipulate IntPtr to get a byte[] of the method output?

You can use Marshal.ReadByte to locate the string terminator, along these lines:

int length = -1;
while(Marshal.ReadByte(ptr, ++length) != 0);


Then you can use Marshal.Copy to get the array:

byte[] bytes = new byte[length];
Marshal.Copy(ptr, bytes, 0, length);


And there you have it.

Extra credit: Turn this into a custom marshaler for UTF-8 strings.

gusano79
Posting Pro
521 posts since May 2004
Reputation Points: 182
Solved Threads: 77
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: