MarshallAs for char* buffer

J

jacekbaz

I'm trying to use some closed source unmanaged C++ dll.

One of functions I have to call have signature as follows:

int getData(char * buffer, long * bufflen);

And this function requires that buffer is initialized to hold bufflen
chars.
bufflen after function is called will contain lenght of response
data.

If I try to marshall it using

[DllImport("mydll.dll",CharSet = CharSet.Ansi)]
public static extern int getData(
[MarshalAs(UnmanagedType.LPStr)] out StringBuilder result,
out long bufflen);

i get acces violation exception, despite that string builder is
initialized by:

long bufflen 256;

StringBuilder result = new StringBuilder((int)bufflen);

DllMapper.getData(out result, out bufflen);

What I'm doing wrong?
 
P

Peter Morris

Try this....

[DllImport("mydll.dll", CharSet = CharSet.Ansi)]
public static extern int getData(StringBuilder result, ref long bufflen);

StringBuilder result = new StringBuilder(500);
int resultLen = 500;
getData(result, ref resultLen);


Just a guess, not my area of expertise :)



Pete
 

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