緣由有兩點:
1. 寒假時上C++教學DLL的部份有同學問我此問題。
2. 最近和 remmurds在討論這兩套IDE產生出來的DLL如何共用。
Using BCB6 to create a DLL Project
FileèNewèOther,選DLL Wizard

Wizard Option 要記得勾 VC++ Style DLL

分別建立 Header File 和 Source File如下:
//DLLTest.h
#ifndef DLLTEST_H
#define DLLTEST_H
extern "C" __declspec(dllexport) long Count(int n, int m);
#endif
//DLLTest.cpp
#include <windows.h>
#include "DLLTest.h"
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
extern "C" __declspec(dllexport) long Count(int n, int m)
{
return n + m;
}
檔案編輯好後,從主選單的Project去Build Project,在專案的資料夾會產生一個DLL檔,此檔會在[Using VC++ 2005 to build a DLL Demo Project]用到。

Using VC++ 2005 to build a DLL Demo Project
FileèNewèAdd a New Project,選擇空專案
將在[Using BCB6 to create a DLL Project]中所建立的 Header File加到專案裡,並建立一測試用的Source File,Code如下:
#include <iostream>
#include <windows.h>
#include "DLLTest.h"
typedef long (__cdecl *CountProc)(int, int);
int main(void)
{
long (*Count)(int, int);
HINSTANCE hInst = LoadLibrary(L"DLLTest.dll");
Count = (CountProc)GetProcAddress(hInst, "_Count"); // 要加上底線,見觀念講解
long result = Count(5, 5);
std::cout << result << std::endl;
system("PAUSE");
FreeLibrary(hInst);
return 0;
}
產生出來的執行檔需和[Using BCB6 to create a DLL Project]中所Build出來的DLL放在同一資料夾,成功執行的畫面如下:


重要觀念講解:
GetProcAddress函式簡短說明,它的原型如下
FARPROC WINAPI GetProcAddress(
__in HMODULE hModule,
__in LPCSTR lpProcName
);
hModule:DLL模組的Handle。
lpProcName:函式或變數名稱,在不使用C++ mangled name的情況下,也得在函式或變數名稱上,根據不同的Call Convention做變動(記得要保持兩套 Compiler的Call Convention一致),下表為四種不同Calling Convention的對照表
|
Calling Convention
|
函式名稱
|
DLL內函式名稱
|
|
__stdcall
|
Count
|
Count 不變
|
|
__cdecl
|
Count
|
_Count 加底線
|
|
__pascal
|
Count
|
COUNT 全大寫
|
|
__fastcall
|
Count
|
Count 不變
|
參考資料:
動態鏈結函式庫
Using Run-Time Dynamic Linking



Programming (11)
我是用BCB6作Dll檔,然後給VC++ 2003 .Net利用。
雖然可以run,但是我不是很清楚原理,因為我也覺得應該要有.h檔才對...