Converting .NET String to something usable by old unsafe C/C++ Code
-
I have a class that has been written in plain C++ that makes calls into a library that exports many functions that take char array parameters. What I want to know is how I can get / convert the data in a .NET String object into a plain char array so that I can pass the value into the function call? Basically any help converting the new .NET types into older style "unsafe" types would be helpful. Thanks.
-
I have a class that has been written in plain C++ that makes calls into a library that exports many functions that take char array parameters. What I want to know is how I can get / convert the data in a .NET String object into a plain char array so that I can pass the value into the function call? Basically any help converting the new .NET types into older style "unsafe" types would be helpful. Thanks.
You can use one of two things, either the StringBuilder or you can marshal the string data. If you need a buffer of some sort, like you need for getting a special folder you need to use StringBuilder Code courtesy Tomas Restrepo
[ DllImport("shell32.dll") ]
private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, int nFolder, bool fCreate);StringBuilder dirPath = new StringBuilder(256,1024);
bool res = SHGetSpecialFolderPath(IntPtr.Zero, dirPath, CSIDL_APPDATA, true);If you just need to pass a string in to a function but do not modify it use marshalling. I don't know what will happen if you try to modify it, it could reflect the change, or it could blow up on you. From Inside C# by Tom Archer
[DllImport("user32.dll", CharSet=CharSet.Unicode)]
static extern int MessageBox(
IntPtr hWnd,
[MarshalAs(UnmanagedType.LPWStr)]
string msg,
[MarshalAs(UnmanagerType.LPWStr)]
string caption,
int type
);MessageBox(0, "Hello, World!", "This is called from a C# app!", 0);
Both code snippets are untested, but should demonstrate how to do it. I made a minor change in Tom Archer's code and that was to reflect that handles, such as HWNDs should be IntPtr's instead of int's. HTH, James Sonork ID: 100.11138 - Hasaki and a digital cookie (not chocolate chip, its computer chip) goes to whoever can be the first to tell me what Hasaki means. I know someone registered on here can tell me :)
-
You can use one of two things, either the StringBuilder or you can marshal the string data. If you need a buffer of some sort, like you need for getting a special folder you need to use StringBuilder Code courtesy Tomas Restrepo
[ DllImport("shell32.dll") ]
private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, StringBuilder lpszPath, int nFolder, bool fCreate);StringBuilder dirPath = new StringBuilder(256,1024);
bool res = SHGetSpecialFolderPath(IntPtr.Zero, dirPath, CSIDL_APPDATA, true);If you just need to pass a string in to a function but do not modify it use marshalling. I don't know what will happen if you try to modify it, it could reflect the change, or it could blow up on you. From Inside C# by Tom Archer
[DllImport("user32.dll", CharSet=CharSet.Unicode)]
static extern int MessageBox(
IntPtr hWnd,
[MarshalAs(UnmanagedType.LPWStr)]
string msg,
[MarshalAs(UnmanagerType.LPWStr)]
string caption,
int type
);MessageBox(0, "Hello, World!", "This is called from a C# app!", 0);
Both code snippets are untested, but should demonstrate how to do it. I made a minor change in Tom Archer's code and that was to reflect that handles, such as HWNDs should be IntPtr's instead of int's. HTH, James Sonork ID: 100.11138 - Hasaki and a digital cookie (not chocolate chip, its computer chip) goes to whoever can be the first to tell me what Hasaki means. I know someone registered on here can tell me :)
Thanks for the good information... but is there a way I can convert to an unsafe type BEFORE actually passing the data into a function? If I've got a String* or Stringbuilder* in Managed C++ and want to convert it into an unmanged type like char* or LPStr, how is that possible? -dvryce
-
Thanks for the good information... but is there a way I can convert to an unsafe type BEFORE actually passing the data into a function? If I've got a String* or Stringbuilder* in Managed C++ and want to convert it into an unmanged type like char* or LPStr, how is that possible? -dvryce
Can't say I know how. I haven't looked at MC++ too much, only because I don't have any old code I have to work with that would warrant MC++ to me. Perhaps the DOTNET mailing list could be of some help? Its been a great resource for myself and many others. It is a high volume mailing list though so you'll probably want to subscribe to the digest version, or use the web-based interface. Sorry I couldn't be of more help, James Sonork ID: 100.11138 - Hasaki and a digital cookie (not chocolate chip, its computer chip) goes to whoever can be the first to tell me what Hasaki means. I know someone registered on here can tell me :)