简单记录一下在VS2008下创建dll和lib,以及调用方法
一、生成DLL和LIB
创建控制台应用程序 、在应用程序类型中选择DLL。DLL工程创建完成。
创建一个MyDll.h
#ifndef __MYDLL_H__
#define __MYDLL_H__
#ifdef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllimport)
#else
#define MYLIBDLL extern "C" _declspec(dllexport)
#endif
MYLIBDLL int Add(int plus1, int plus2);
#endif
在创建一个MyDll.cpp
#include "stdafx.h"
#include "MyDll.h"
#include <iostream>
using namespace std;
int Add(int plus1, int plus2)
{
int add_result = plus1 + plus2;
return add_result;
}
点击生成,则dll文件就生成了,vs2008不能直接生成lib文件,这个时候就需要我们在建立dll工程的时候再新建一个def文件,默认生成然后重新生成就能够得到lib文件了。
调用动态库
静态调用
#pragma comment(lib,"..\\debug\\MyDll.lib")
动态调用......后续再添加
动态库调用实例
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "JhemrParser.lib")
typedef int (*GETEMRDATA)(string &strPath, string &strData);
int _tmain(int argc, _TCHAR* argv[])
{
string strPath = "D:\\Dtest11-32dll-to-64dll\\00020842\\00040001";
string strData = "";
//int npoint = GetEmrData(strPath, strData); //静态调用
char* strdll = (char*)(L"JhemrParser.dll"); //动态调用
HMODULE hInst = LoadLibrary((LPCWSTR)strdll);
GETEMRDATA pGetEmrData = (GETEMRDATA)GetProcAddress(hInst, "GetEmrData");
int npoint = pGetEmrData(strPath, strData);
cout<<npoint<<endl;
cout<<strData<<endl;
return 0;
}
--------------------------------------------------------------------------------------------------------
下面在资料来源于网络
(1). Dependencies (推荐使用,要求有lib源代码)
(2). 直接将lib添加到需要用的工程中(不太推荐,lib没能统一管理)
(3).
(4).