page contents

C++ 调用C#类库

本文讲述了C++ 调用C#类库!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-10-WMW35Sql63437aae76fea.png

本文讲述了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;
}


更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。

想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。

attachments-2022-05-rLS4AIF8628ee5f3b7e12.jpg

  • 发表于 2022-10-10 09:51
  • 阅读 ( 433 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1470 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章