vs2010下编译DLL库和使用

一、创建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;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章