Convert string array from C# to C++ ?
-
Hi everyone, I have a string array in C# includes 4 strings: {"string1", "string2", "string3", "string4"} In C#, I can use List or string[] to process. But now I want to send this array to a function in C++ (may be is vector). How can I do that ? and what is the suitable type of string array in C++ Thanks and regards, Tai
-
Hi everyone, I have a string array in C# includes 4 strings: {"string1", "string2", "string3", "string4"} In C#, I can use List or string[] to process. But now I want to send this array to a function in C++ (may be is vector). How can I do that ? and what is the suitable type of string array in C++ Thanks and regards, Tai
C# string[] becomes array<String^>^ in C++. List becomes List<String^>^. Remember you need to allocate managed types with gcnew in C++. (The regular new is for unmanaged types.) The conversion is done automatically on function call. In fact it isn't even a real conversion, as the meaning/memory layout is the same, just the expression looks different. So you declare a function taking a array<String^>^ and pass in a string[] or the other way araound. Keep in mind, you can't have C# and C++ in the same module (exe/dll) so you need to separate the code into different ones.
-
C# string[] becomes array<String^>^ in C++. List becomes List<String^>^. Remember you need to allocate managed types with gcnew in C++. (The regular new is for unmanaged types.) The conversion is done automatically on function call. In fact it isn't even a real conversion, as the meaning/memory layout is the same, just the expression looks different. So you declare a function taking a array<String^>^ and pass in a string[] or the other way araound. Keep in mind, you can't have C# and C++ in the same module (exe/dll) so you need to separate the code into different ones.
Freak30's answer is good and thinking about it I realize you may want to get the string value to native C++ code instead of just to C++/CLI. If that's the case, you'll want to look into the marshal_as templates. ex.
#include
. . .
const char * message = "test";
String^ managed = marshal_as( message ); -
C# string[] becomes array<String^>^ in C++. List becomes List<String^>^. Remember you need to allocate managed types with gcnew in C++. (The regular new is for unmanaged types.) The conversion is done automatically on function call. In fact it isn't even a real conversion, as the meaning/memory layout is the same, just the expression looks different. So you declare a function taking a array<String^>^ and pass in a string[] or the other way araound. Keep in mind, you can't have C# and C++ in the same module (exe/dll) so you need to separate the code into different ones.
Thanks Freak30, I am using Visual Studio 2010. I tried to send string[] in C# to string[] in C++, but not successful In C#, I used below codes:
[DllImport("DgnShpConverter.dll")]
public static extern Boolean ConvertDgnToShp(string source, string destinationPath, string shpFileName, string [] strArray);And in C++ (file DgnShpConverter.dll), I used below codes:
__declspec(dllexport) bool ConvertDgnToShp(const char* sourceFile, const char *destinationPath, const char *shpFileName, string myArray[])
{
// do something ....
}Do you know how to fix this ? Thanks and kind regards, Tai
-
Thanks Freak30, I am using Visual Studio 2010. I tried to send string[] in C# to string[] in C++, but not successful In C#, I used below codes:
[DllImport("DgnShpConverter.dll")]
public static extern Boolean ConvertDgnToShp(string source, string destinationPath, string shpFileName, string [] strArray);And in C++ (file DgnShpConverter.dll), I used below codes:
__declspec(dllexport) bool ConvertDgnToShp(const char* sourceFile, const char *destinationPath, const char *shpFileName, string myArray[])
{
// do something ....
}Do you know how to fix this ? Thanks and kind regards, Tai
Please check this link Interoperation: C# and native Win32 C++ code; arrays of strings[^]
Do more work Make more mistakes Learn more things
-
Please check this link Interoperation: C# and native Win32 C++ code; arrays of strings[^]
Do more work Make more mistakes Learn more things