在发布版本中,将c#字符串传递到c++ dll失败
pass C# string to C++ dll fails in Release build
我有一个c++ dll与简单的函数,如
#ifdef BUILDING_THE_DLL
#define EXPORTED __declspec(dllexport)
#else
#define EXPORTED __declspec(dllimport)
#endif
...
EXPORTED void AddSetting(char *key, char *value)
和带有函数声明的c#项目:
[DllImport("pers.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void AddSetting(string key, string value);
当c#项目在调试模式下构建时,一切都工作得很好。在发布版本中,会触发一个异常:"试图以不正确的格式加载程序。"
任何想法?
UPD:在c#项目中,平台目标在Debug模式下设置为x86,在Release中设置为Any CPU。我在发布版中改成了x86,这就是解决方案。非常感谢Matt。
默认情况下需要指定使用ansi格式的字符串。它是unicode。应该是:
[DllImport("pers.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet::Ansi)]
public static extern void AddSetting(string key, string value);
同样,如果函数"AddSetting"修改字符串,你需要在c#中使用StringBuilder。
相关文章:
- 使用提升对字符串进行标记化时,将令牌转换为 char* const* 时失败
- std::p作为 std 中定义的unordered_map自定义键的字符串的空气失败并出现模板错误
- C++ TCP 服务器比较字符串失败
- 嵌套列表(字符串向量的向量)初始化失败
- C++长到字符串的转换失败
- 字符串流运算符输入失败
- AES/CFB密码模式从字符串失败中解密
- 增强精神解析报价字符串失败
- BOOST :: lexical_cast to std ::字符串失败
- 假定相同的字符串之间的比较失败' == '比较
- 如何在类中存储指向字符串到成员函数映射的智能指针,以便它不会因类型不完整而失败?
- 从文件中提取的数据中的C 搜索字符串失败
- C++:为什么这个字符串输入失败,而另一个没有
- 失败的证明转换字符串到枚举类
- 当Find_first_not_of击中字符串末尾时,为什么会失败
- C++ 使用字符串时运行失败
- 在字符串上循环以查看字符串失败的位置
- 当显式调用非默认函数时,覆盖自定义字符串失败
- DCOM中的SAFEARRAY(BSTR) as [out]参数返回字符串失败
- 从函数返回本地var字符串失败,而本地var int和char工作正常.为什么