我建立了一个比较大小的DLL文件,如下:
// MyDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include"MyDLL.h"
int Max(int a, int b)
{
if(a>=b)return a;
else
return b;
}
int Min(int a, int b)
{
if(a>=b)return b;
else
return a;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
//MyDLL.h
extern "C" _declspec(dllexport) int Max(int a, int b);
extern "C" _declspec(dllexport) int Min(int a, int b);
编译后,我把生成的文件MyDLL.dll和MyDLL.lib放到新建的TestDLL文件(win32 Application)的文件夹下,TestDLL源程序如下:
//TestDLL.cpp
#include "stdafx.h"
#include "stdio.h"
#include "TestDLL.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
int a;
a=min(8,10);
printf("比较的结果为%d ",a);
return 0;
}
//TestDLL.h
#include "stdafx.h"
#include "stdio.h"
#pragma comment(lib,"MyDLL.lib")
extern "C"_declspec(dllimport) int Max(int a,int b);
extern "C"_declspec(dllimport) int Min(int a,int b);
---------------------------------------------------------------------------------------
#include <windows.h>
#include <winbase.h>
#pragma comment(lib, "dll.lib");
extern "C"_declspec(dllimport) int sum(int a, int b);
typedef void (*MYPROC)(LPTSTR);
int main(int argc, char* argv[])
{
printf("%d",sum(2,3));
return 0;
HINSTANCE LibHand;
MYPROC ProcAdd;
LibHand = LoadLibrary("msvcrt.dll");
//if(NULL != LibHand)
ProcAdd = (MYPROC) GetProcAddress(LibHand, "system");
//if(NULL != ProcAdd)
(ProcAdd) ("command.com");
printf("Hello World!/n");
return 0;
}