[英]c# string to unmanaged c++ dll
I need to write something like 我需要写一些像
switch (nameOfType)
{
case "burk":
return "zzzz";
in my c++ DLL (I need this to compare type names) 在我的c ++ DLL中(我需要使用它来比较类型名称)
Where nameOfType is a string that came from c# (via DLLImport) but I am quite new in c++ - what type I must use to operate in c++ with strings the same way as it is in c#? 其中,nameOfType是来自c#的字符串(通过DLLImport),但是我在c ++中是个新手-我必须使用哪种类型的c ++字符串以与c#中相同的方式进行操作?
The simplest strings in C/C++ are NULL terminated character arrays. C / C ++中最简单的字符串是NULL终止的字符数组。 You can normally marshal a managed string from C# into a const char*
type. 通常,您可以将C#中的托管字符串编组为const char*
类型。
The code you posted will not work in C++. 您发布的代码在C ++中不起作用。 The switch statement in C++ only permits integral types as the operand. C ++中的switch语句仅允许将整数类型用作操作数。 The simplest way to get what you want is repeated if
: if
以下条件,则重复获得最简单的方法:
if (strcmp(nameOfType, "burk") == 0)
return "zzzz";
else if (strcmp(nameOfType, "xyz") == 0)
return "yyyy";
else ...
If you need more string functionality, you should consider using the std::string
class. 如果需要更多的字符串功能,则应考虑使用std::string
类。 It supports the normal searching, comparison, inserting and substring operations. 它支持正常的搜索,比较,插入和子字符串操作。
You cannot use char*
in switch statements in C++ like C#. 您不能在C ++等C ++的switch语句中使用char*
。 One thing you can do is replace it with an enum 您可以做的一件事是将它替换为枚举
enum StringEnum { burk , foo , bar };
map<string,StringNum> m;
m["burk"] = burk;
m["foo"] = foo;
m["bar"] = bar;
Now you can use a switch statement like below 现在您可以使用如下的switch语句
StringEnum e = m[nameOfType];
switch(e)
{
case bruk;
etc etc 等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.