1. 对äºè¦å¯¼åºçæ¯ä¸ä¸ªå½æ°ï¼æ们é½éè¦ä½¿ç¨å
³é®å对å
¶å£°æï¼
ãã__declspec(dllexport)
ããå¨åºç¨ç¨åºä¸è¦æ³ä½¿ç¨è¿äºå¯¼åºå½æ°ï¼ä¹éè¦ä½¿ç¨å
³é®åæ¥å£°æè¦ä½¿ç¨çDLL导åºå½æ°ï¼
ãã__declspec(dllimport)
ä¾å¦ï¼
//dll_1.cpp
#include "stdafx.h"
#include <stdio.h>
#include "dll_1.h"
#define EXPORTING_DLL
int printmyname(bool a)
{
if(a)
printf("hi jack!");
else
printf("printfmyname need TRUE");
return 0;
}
//dll_1.h
#ifndef DLL_1_H
#define DLL_1_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) int printmyname(bool a);
#else
extern __declspec(dllimport) int printmyname(bool a);
#endif
#endif
2. æ们è¿å¯ä»¥ä½¿ç¨æ¨¡åå®ä¹æ件æ¥å£°æDLL导åºå½æ°ï¼è¿æ ·ï¼æ们就ä¸éè¦åDLL导åºå½æ°ä¸æ·»å å
³é®åäºãå¦ï¼
// myDLL.def
//
LIBRARY "myDLL"
EXPORTS
HelloWorld
示ä¾DLLååºç¨ç¨åº
ããè¿æ¯ä¸ä¸ªå¨Visual C++ä¸ç¨âWin32å¨æé¾æ¥åºâ项ç®ç±»åå建ç示ä¾ç¨åºã
// myDLL.cpp
//
#include "stdafx.h"
#define EXPORTING_DLL
#include "sampleDLL.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void HelloWorld()
{
MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);
}
// File: myDLL.h
//
#ifndef INDLL_H
#define INDLL_H
#ifdef EXPORTING_DLL
extern __declspec(dllexport) void HelloWorld() ;
#else
extern __declspec(dllimport) void HelloWorld() ;
#endif
#endif
ããä¸é¢çç¨åºæ¯ä¸ä¸ªwin32åºç¨ç¨åºï¼è¯¥ç¨åºè°ç¨myDLLä¸ç导åºå½æ°HelloWorldã
// myApp.cpp
//
#include "stdafx.h"
#include "myDLL.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HelloWorld();
return 0;
}
ãã注æï¼å¨å è½½æ¶å¨æé¾æ¥ä¸ï¼å¿
é¡»é¾æ¥å¨çæmyDLL项ç®æ¶å建çmyDLL.libå¼å
¥åºã
ããå¨è¿è¡æ¶å¨æé¾æ¥ä¸ï¼åºè¯¥è¿æ ·è°ç¨myDLL.dllä¸ç导åºå½æ°ï¼
...
typedef VOID (*DLLPROC) (LPTSTR);
...
HINSTANCE hinstDLL;
DLLPROC HelloWorld;
BOOL fFreeDLL;
hinstDLL = LoadLibrary("myDLL.dll");
if (hinstDLL != NULL)
{
HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld");
if (HelloWorld != NULL)
(HelloWorld);
fFreeDLL = FreeLibrary(hinstDLL);
}
...
ããè¿åªæ¯ä¸ªç®åçä¾åï¼ä½å¤§è´ç使ç¨æ¹æ³å°±æ¯è¿æ ·ã
温馨提示:内容为网友见解,仅供参考