近期项目中,需要访问客户数据库,客户不希望直接访问其数据库,而是以动态库接口的形式开放,还是用C#开发的。经过不断尝试,总结方法如下
//包含动态库
#using "Test.dll"
using namespace Test_LIB;
using namespace Runtime::InteropServices;
//调用代码:
Test^ obj = gcnew Test;
String^ s = System::Convert::ToString(1);
String^ sRet = obj->Mothed(s);
目前参数只用到字符,String转换为CString方法如下:
CString strMsg;
const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(sBarcode)).ToPointer();
strMsg = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
编译时增加选项/clr