匯出C++ dll中的類

來源:互聯網
上載者:User

今日做一個小程式來練手
要求:做一個dll,dll中包含一個函數,這個函數的主要作用就是顯示一個messagebox,調用者調用這個函數,來顯示一些訊息

首先在定義一個類,由於在使用時,我們應執行個體化這個類,所以在這個類之前應加上__declspec(dllexport),來確保匯出了類的建構函式
由於我們用到了mfc,所以在代碼之前加入

#ifndef __AFXWIN_H__
 #error include 'stdafx.h' before including this file for PCH
#endif

//---------------------------------------------------------------

#pragma once
#ifndef __AFXWIN_H__
    #error include 'stdafx.h' before including this file for PCH
#endif

class __declspec(dllexport) cxyMessTest
{
public:
    cxyMessTest(void);
    ~cxyMessTest(void);

    void CxyShowMess(LPCTSTR mess);
};
//類的實現

#include "StdAfx.h"
#include ".cxymesstest.h"

cxyMessTest::cxyMessTest(void)
{
}

cxyMessTest::~cxyMessTest(void)
{
}
void cxyMessTest::CxyShowMess(LPCTSTR mess)
{
    AfxMessageBox(mess,MB_OK,0);
}

 接下為在 def檔案中定義好要匯出的函數

第三步在工程上調用
第一步:C++ application
第二步:拷貝產生的dll到debug目錄下,
第三步:拷貝c++ dll的.h檔案到app的目錄下,並添加到 app中
第四步:#include "cxymesstest.h"
調用就可以了
cxyMessTest * test;
test=new cxyMessTest();
test->CxyShowMess("Hell world");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.