使用VC6建立一個名為MySum的MFC Extension DLL,並Key入如下程式碼:
MySum.cpp
#include "stdafx.h"
#include <afxdllx.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static AFX_EXTENSION_MODULE MySumDLL = { NULL, NULL };
void APIENTRY MySum(int a, int b)
{
CTime t;
printf("Time : %04d-%02d-%02d %02d:%02d:%02d\n", t.GetYear(), t.GetMonth(), t.GetDay(),t.GetHour(), t.GetMinute(), t.GetSecond());
printf("a + b = %d\n", a + b);
}
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("MYSUM.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(MySumDLL, hInstance))
return 0;
new CDynLinkLibrary(MySumDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("MYSUM.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(MySumDLL);
}
return 1; // ok
}
MySum.def; MySum.def : Declares the module parameters for the DLL. LIBRARY "MySum" DESCRIPTION 'MySum Windows Dynamic Link Library' EXPORTS ; Explicit exports can go here MySum @1
使用VC6建立一個名為TestSum的Application去呼叫MySum.dll,輸入以下代碼:
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { typedef void (CALLBACK * _MySum) (int , int ); HINSTANCE hLibModule; _MySum MySum = NULL; hLibModule = LoadLibrary("MySum.dll"); MySum = (_MySum) GetProcAddress(hLibModule, "MySum"); if(MySum == NULL) return FALSE; MySum(15, 20); FreeLibrary(hLibModule); return TRUE; }
設定MySum.dll的Project參數,點選工具列中Project/Settings:
1. 在Debug標籤頁,General選項下的"Executable for debug session"填入TestSum.exe所存在的絕對路徑(含EXE檔名)。
2. 在Link標籤頁,General選項下的"Output file name"填入MySum.dll要輸出至TestSum.exe所存在的資料夾絕對路徑(含DLL檔名)。
接下來只要在MySum.dll的Project中,設好需要Debug的中斷點位置就可以直接對DLL進行Debug。
執行結果:
沒有留言:
張貼留言