一、創建DLL 文件
1 vs2010下選擇win32應用程序,創建DLL 工程
2 創建頭文件testdll.h
#ifndef TestDll_H_
#define TestDll_H_
#ifdef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllimport)
#else
#define MYLIBDLL extern "C" _declspec(dllexport)
#endif
MYLIBDLL int Add(int plus1, int plus2);
//You can also write like this:
//extern "C" {
//_declspec(dllexport) int Add(int plus1, int plus2);
//};
#endif
3 創建源文件
#include "stdafx.h"
#include "testdll.h"
#include <iostream>
using namespace std;
int Add(int plus1, int plus2)
{
int add_result = plus1 + plus2;
return add_result;
}
4 創建模塊文件
LIBRARY "MyDLL"
EXPORTS
Add @1
5 選擇release版本進行編譯
二、調用DLL文件中的函數
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#pragma comment (lib , "MyDll.lib" )
extern "C"_declspec (dllimport) int Add(int plus1, int plus2);
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d\n",Add(6,4));
return 0;
}