• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++调用C#类库

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
本地项目 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;
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++循环语句发布时间:2022-07-14
下一篇:
使用MySQL数据库将汉字转换成拼音的一个C语言小程序发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap