Skip to main content
gamedev.net
🔒 Locked

[.net] char*

Started by Greg K Nov 15, 2004 at 9:13 PM 2 replies 450+ views
Original Post
Greg K
Greg K
I am trying to pass a pointer to a char* to an unmanaged c++ dll function. The function allocates a string (ASCII) and sets the parameter to point to it. It is the callers responsibility to free that string. How do I do this in c#? bool myFunc( char** myNewString ) { *myNewString = new char[32]; return true; }
TheBluMage
TheBluMage
I'd have to know what you're going to use this buffer for to give you a more specific answer, but with out any marshalling conversions thrown in it should be something like this to import it:

using System.Runtime.InteropServices;

public class MyDllClass {

[DllImport("mydll.dll")]
public extern unsafe static bool myFunc(ref byte* buffer);

}


And make sure you have the unsafe compiler option on. Sorry, I can't get much more specific unless I know how you're going to use the buffer.
TheBluMage
TheBluMage
So, this DLL function is passing a string back to the caller? Okay, If that's the case, you can import it like this:

[DllImport("yourdllname.dll")]
public static extern bool myFunc([MarshalAs(UnmanagedType.LPStr)] out string str);


Then call it like this:

string str;
myFunc(out str); //Now str is assigned the data from the buffer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.