VS2010中 C++创建DLL图解
一、DLL的創(chuàng)建?
創(chuàng)建項(xiàng)目: Win32->Win32項(xiàng)目,名稱:MyDLL
選擇DLL (D) ->完成.
1、新建頭文件testdll.h
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
------------------------------------------------------
testdll.h也可以寫為:
#pragma once
#ifndef __AFXWIN_H__
? ? //........
#endif
#include<string>
using namespace std;
//數(shù)據(jù)、類型定義等
#define OUTAPI __declspec(dllexport)
OUTAPI int Add(int a, int b);
2、新建源文件testdll.cpp
testdll.cpp代碼如下:
#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;
}
3、新建模塊定義文件mydll.def
mydll.def代碼如下:
LIBRARY "MyDLL"
EXPORTS
Add ??@1
4、vs2010自動創(chuàng)建dllmain.cpp文件,它定義了DLL 應(yīng)用程序的入口點(diǎn)。
dllmain.cpp代碼如下:
// dllmain.cpp : 定義 DLL 應(yīng)用程序的入口點(diǎn)。
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
? ?? ?? ?? ?? ?? ?? ???DWORD??ul_reason_for_call,
? ?? ?? ?? ?? ?? ?? ???LPVOID lpReserved
? ?? ?)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
??break;
}
return TRUE;
}
最后,編譯生成MyDLL.dll文件和MyDLL.lib文件。
1>------ 已啟動生成: 項(xiàng)目: MyDLL, 配置: Debug Win32 ------
1> ?dllmain.cpp
========== 生成: 成功 1 個(gè),失敗 0 個(gè),最新 0 個(gè),跳過 0 個(gè) ==========
?
1>------ 已啟動生成: 項(xiàng)目: MyDLL, 配置: Debug Win32 ------
1> ?stdafx.cpp
1> ?testdll.cpp
1> ?MyDLL.cpp
1> ?正在生成代碼...
1> ? ? 正在創(chuàng)建庫 D:\Visual C++\工程\Libaray\MyDLL\Debug\MyDLL.lib 和對象 D:\Visual C++\工程\Libaray\MyDLL\Debug
【生成的DLL用DLL查看器,看到的導(dǎo)出函數(shù)名:Add,沒有多余的奇怪字符】
總結(jié)
以上是生活随笔為你收集整理的VS2010中 C++创建DLL图解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI智能语音客服机器人系统/方案/案列/
- 下一篇: QT:QObject 简单介绍