C++ 调用C#类库
本地项目 CPlusAccessCSharp
//.h
#include <msclr\marshal_cppstd.h> using namespace System; using namespace msclr::interop; using namespace std; #include <string> #using "..\\Debug\\ClassLibrary1.dll" using namespace ClassLibrary1;
// CCPlusTestProjectDlg 对话框 class CCPlusTestProjectDlg : public CDialogEx { void (*pf)(int); // 构造 public: CCPlusTestProjectDlg(CWnd* pParent = NULL); // 标准构造函数 //void OnReceiveData(int m); static void OnReceiveData(int m,ClassLibrary1::ConnectionErrorStat stat) { Console::WriteLine(L"Function1 called with value {0}{1}",m,stat.ToString()); } static void OnReceiveData2(int code, System::String^ cmd, array<unsigned char>^ data) { char* szRet = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(cmd); CString strCmd(szRet); int len = data->Length ; unsigned char *b2 = new unsigned char[data->Length + 1]; pin_ptr<unsigned char> pb1 = &data[0]; memcpy(b2,pb1,data->Length); b2[data->Length ] =0; CString strData(b2); delete b2; Console::WriteLine(L"Function1 called with value {0}",code); } // 对话框数据 enum { IDD = IDD_CPLUSTESTPROJECT_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 CString ClrStringToCString(String ^ msg); // 实现 protected: HICON m_hIcon; // 生成的消息映射函数 virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: afx_msg void OnBnClickedOk(); afx_msg void OnBnClickedCancel2(); };
//.cpp
void CCPlusTestProjectDlg::OnBnClickedOk() { ClassLibrary1::WebHttp ^ http = gcnew ClassLibrary1::WebHttp(); //申明Clr 类 ClassLibrary1::WebHttp::OnReceiveCallBack^ hander=gcnew ClassLibrary1::WebHttp::OnReceiveCallBack(HandlerClass::Fun1); ClassLibrary1::WebHttp::OnReceiveCallBack^ hander1=gcnew ClassLibrary1::WebHttp::OnReceiveCallBack(OnReceiveData); ClassLibrary1::WebHttp::OnReceiveCallBack2^ hander2=gcnew ClassLibrary1::WebHttp::OnReceiveCallBack2(OnReceiveData2); String^ strMsg1; ClassLibrary1::ReturnMsg ^ ret2 = http->Test(hander,hander2,strMsg1); //回调函数,C#委托 array<int> ^ intArray = gcnew array<int>(2); //定义数组长度与类型 intArray[0] = 5; intArray[1] =6; array<float>^ floatArray = gcnew array<float>(4); floatArray[0] = 1.1; floatArray[2] = 2.2; floatArray[3] = 3.3; array<String^>^ FrameAry = gcnew array<String^>(4); FrameAry[0] = "abc"; FrameAry[2] = "123"; String^ strRet = http->SetInfo(1, intArray, floatArray, FrameAry); //传数组参数 char* szRet1 = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(strRet); //String^ 转char * CString strRet2(szRet1); ClassLibrary1::ReturnMsg ^ ret = http->Test(hander1); //返回类 int code = ret->code; CString strMsg = ClrStringToCString(ret->msg); char* szRet = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ret->msg); CString strMsg2(szRet); code = code; }
CString CCPlusTestProjectDlg::ClrStringToCString(String^clrString) { CString strRet = ""; string sMsg; sMsg = marshal_as<std::string>(clrString); CA2T szr((sMsg).c_str()); strRet = (LPCTSTR)szr; return strRet; }
posted on 2020-03-24 17:22 strangeman 阅读(1191) 评论(0) 编辑 收藏 举报