VC6 DLL exports
//filename : Dll1.h #ifndef _DLL1_H_ #define _DLL1_H_ #ifdef DLL1_API #else #define DLL1_API extern "C" _declspec(dllimport) #endif // DLL1_API DLL1_API int _stdcall add(int a,int b); DLL1_API int _stdcall substract(int a,int b); #endif
//filename:Dll1.cpp #define DLL1_API extern "C" _declspec(dllexport) #include "Dll1.h" int _stdcall add(int a,int b) { return a+b; } int _stdcall substract(int a,int b) { return a-b; }
//MyPoint.h // MyPoint.h: interface for the MyPoint class. // ////////////////////////////////////////////////////////////////////// #include <windows.h> #include <stdio.h> #if !defined(AFX_MYPOINT_H__0FB85FE1_A0BB_4FF3_B780_79E133776F44__INCLUDED_) #define AFX_MYPOINT_H__0FB85FE1_A0BB_4FF3_B780_79E133776F44__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifdef DLL2_API #else #define DLL2_API _declspec(dllimport) #endif // DLL2_API class DLL2_API MyPoint { public: void output(); MyPoint(); virtual ~MyPoint(); public: int y; int x; }; #endif // !defined(AFX_MYPOINT_H__0FB85FE1_A0BB_4FF3_B780_79E133776F44__INCLUDED_)
// MyPoint.cpp: implementation of the MyPoint class. // ////////////////////////////////////////////////////////////////////// #define DLL2_API _declspec(dllexport) #include "MyPoint.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// MyPoint::MyPoint() { x=0; y=0; } MyPoint::~MyPoint() { } void MyPoint::output() { HWND hWnd = GetForegroundWindow(); HDC hdc = GetDC(hWnd); char buf[40]; memset(buf,0,40); sprintf(buf,"x=%d,y=%d",x,y); TextOut(hdc,0,0,buf,strlen(buf)); ReleaseDC(hWnd,hdc); }
Dll1.def
LIBRARY Dll1 EXPORTS add substract
void CDllTestDlg::OnBtnLoadLibrary() { HMODULE hModule = LoadLibrary("Dll1.dll"); if(!hModule) { MessageBox("未能加载DLL"); return; } typedef int (_stdcall *ADDPROC) (int,int); ADDPROC add=(ADDPROC) GetProcAddress(hModule,"substract"); if(!add){ MessageBox("未找到指定函数"); return; } int result=add(1,22); CString str; str.Format("%d",result); MessageBox(str); }