从CLR到本机c ++ dll的字符串参数

时间:2016-07-18 05:35:26

标签: visual-c++ dll clr marshalling unmanaged

嗨,我有一个原生代码dll

Native.dll:

#if defined EXPORTDLL // inside DLL
#define DLL_EXPORT __declspec(dllexport)
#else // outside DLL
#define DLL_EXPORT __declspec(dllimport)
#endif

.....

DLL_EXPORT struct1* SomeMethod(uint16_t num1, std::string str1, std::string str2)
{
....
}

然后在我的托管/ CLR中,我正在整理这个:

[DllImport("Native.dll", EntryPoint = "?SomeMethod@@YAPAVstruct1@someSpace@@GV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z", CallingConvention = CallingConvention::Cdecl)]
struct1* SomeMethod(uint16_t num1, [In, MarshalAs(UnmanagedType::LPStr)]String^  str1, [In, MarshalAs(UnmanagedType::LPStr)] String^ str2);

稍后从托管代码中调用它:

String^ str1 = gcnew String("1223568");
String^ str2 = gcnew String("1.2.3.5");
SomeMethod(1, str1, str2);

我可以在原生dll中看到,str1值是" 1.2.3.5"并且str2值是badptr。

有人可以告诉我我需要更改什么才能将值正确传递给本机dll的参数吗?

提前致谢。

0 个答案:

没有答案