Passing char[] reference to unmanaged environment?

A

Albertas

Hello,

I have function written in unmanaged environment, which takes as
parameters char*. For example:
void function(char *input, char *output);
It takes two char arrays and modifies the output char array. Now when i

try to use it in managed environment, i do this:


[DllImport("dll.dll", EntryPoint="function",
ExactSpelling=false, SetLastError=true)]
public unsafe static extern void
function([MarshalAs(UnmanagedType.LPArray)] char[] input,
[MarshalAs(UnmanagedType.LPArray)] ref char[] output);


Also tried byte[] instead of char.


When i call it i use:


function (input, ref output);


The problem is when i call this function, the output char array has
only one member in it, but there should be more. What I'm doing
wrong?


The function dll is not a COM object.


Thank You
 
B

Ben Voigt

Albertas said:
Hello,

I have function written in unmanaged environment, which takes as
parameters char*. For example:
void function(char *input, char *output);
It takes two char arrays and modifies the output char array. Now when i

try to use it in managed environment, i do this:


[DllImport("dll.dll", EntryPoint="function",
ExactSpelling=false, SetLastError=true)]
public unsafe static extern void
function([MarshalAs(UnmanagedType.LPArray)] char[] input,
[MarshalAs(UnmanagedType.LPArray)] ref char[] output);


Also tried byte[] instead of char.


When i call it i use:


function (input, ref output);


The problem is when i call this function, the output char array has
only one member in it, but there should be more. What I'm doing
wrong?

Since the unmanaged code isn't aware of .NET memory allocation, it can't
resize the output array. You should therefore allocate the output array
before the call. There should also be no need to use "ref" on the output
array, as the function modifies the existing array.
 

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