今日做一個小程式來練手
要求:做一個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");