C++函数的导出与导入
DLL使用
(1)隐式链接到 DLL 的可运行文件在生成时链接到导入库(.lib文件)。
(2)採用显式连接(LoadLibrary和GetProcAddress)时,不须要.lib文件。
函数导出方式
(1)源码中的 __declspec(dllexport) keyword
(2).def 文件里的 EXPORTS 语句
(3)LINK 命令中的 /EXPORT 规范
全部这三种方法能够用在同一个程序中。
LINK 在生成包括导出的程序时还创建导入库,除非生成中使用了 .exp 文件。
导出
CppDynamicLinkLibrary.cpp
#include "CppDynamicLinkLibrary.h"
#include <strsafe.h> #pragma region DLLMain
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;
}
#pragma endregion #pragma region Global Data // An exported/imported global data using a DEF file
// Initialize it to be 1
int g_nVal1 = 1; // An exported/imported global data using __declspec(dllexport/dllimport)
// Initialize it to be 2
SYMBOL_DECLSPEC int g_nVal2 = 2; #pragma endregion #pragma region Ordinary Functions // An exported/imported cdecl(default) function using a DEF file
int /*__cdecl*/ GetStringLength1(PCWSTR pszString)
{
return static_cast<int>(wcslen(pszString));
} // An exported/imported stdcall function using __declspec(dllexport/dllimport)
SYMBOL_DECLSPEC int __stdcall GetStringLength2(PCWSTR pszString)
{
return static_cast<int>(wcslen(pszString));
} #pragma endregion #pragma region Callback Function // An exported/imported stdcall function using a DEF file
// It requires a callback function as one of the arguments
int __stdcall CompareInts(int a, int b, PFN_COMPARE cmpFunc)
{
// Make the callback to the comparison function // If a is greater than b, return a;
// If b is greater than or equal to a, return b.
return ((*cmpFunc)(a, b) > 0) ? a : b;
} #pragma endregion #pragma region Class // Constructor of the simple C++ class
CSimpleObject::CSimpleObject(void) : m_fField(0.0f)
{
} // Destructor of the simple C++ class
CSimpleObject::~CSimpleObject(void)
{
} float CSimpleObject::get_FloatProperty(void)
{
return this->m_fField;
} void CSimpleObject::set_FloatProperty(float newVal)
{
this->m_fField = newVal;
} HRESULT CSimpleObject::ToString(PWSTR pszBuffer, DWORD dwSize)
{
return StringCchPrintf(pszBuffer, dwSize, L"%.2f", this->m_fField);
} int CSimpleObject::GetStringLength(PCWSTR pszString)
{
return static_cast<int>(wcslen(pszString));
} #pragma endregion
CppDynamicLinkLibrary.def
LIBRARY CppDynamicLinkLibrary
EXPORTS
GetStringLength1 @1
CompareInts @2
g_nVal1 DATA
导入
CppLoadLibrary.cpp
#pragma region Includes
#include <stdio.h>
#include <windows.h>
#pragma endregion // Function pointer types for functions exported from the DLL module typedef int (_cdecl* LPFNGETSTRINGLENGTH1) (PCWSTR); // CALLBACK, aka __stdcall, can only be used for stdcall methods. If it is
// used for __cdecl methods, this error will be thrown in runtime: The value
// of ESP was not properly saved across a function call. This is usually a
// result of calling a function declared with one calling convention with a
// function pointer declared with a different calling convention.
typedef int (CALLBACK* LPFNGETSTRINGLENGTH2) (PCWSTR); // Type-definition of the 'PFN_COMPARE' callback function, and the CompareInts
// function that requires the callback as one of the arguments.
typedef int (CALLBACK* PFN_COMPARE) (int, int);
typedef int (CALLBACK* LPFNMAX) (int, int, PFN_COMPARE); //
// FUNCTION: IsModuleLoaded(PCWSTR)
//
// PURPOSE: Check whether or not the specified module is loaded in the
// current process.
//
// PARAMETERS:
// * pszModuleName - the module name
//
// RETURN VALUE: The function returns TRUE if the specified module is
// loaded in the current process. If the module is not loaded, the function
// returns FALSE.
//
BOOL IsModuleLoaded(PCWSTR pszModuleName)
{
// Get the module in the process according to the module name.
HMODULE hMod = GetModuleHandle(pszModuleName);
return (hMod != NULL);
} //
// FUNCTION: Max(int, int)
//
// PURPOSE: This is the callback function for the method Max exported from
// CppDynamicLinkLibrary.dll
//
// PARAMETERS:
// * a - the first integer
// * b - the second integer
//
// RETURN VALUE: The function returns a positive number if a > b, returns 0
// if a equals b, and returns a negative number if a < b.
//
int CALLBACK Max(int a, int b)
{
return (a - b);
} int wmain(int argc, wchar_t *argv[])
{
BOOL fLoaded = FALSE;
HINSTANCE hModule = NULL; // The name of the module to be dynamically-loaded.
PCWSTR pszModuleName = L"CppDynamicLinkLibrary"; // Check whether or not the module is loaded.
fLoaded = IsModuleLoaded(pszModuleName);
wprintf(L"Module \"%s\" is %sloaded\n", pszModuleName, fLoaded ? L"" : L"not "); // Dynamically load the library.
wprintf(L"Load the library\n");
hModule = LoadLibrary(pszModuleName);
if (hModule == NULL)
{
wprintf(L"LoadLibrary failed w/err 0x%08lx\n", GetLastError());
goto Cleanup;
} // Check whether or not the module is loaded.
fLoaded = IsModuleLoaded(pszModuleName);
wprintf(L"Module \"%s\" is %sloaded\n", pszModuleName, fLoaded ? L"" : L"not "); //
// Access the global data exported from a module.
// // Dynamically-loaded DLL does not allow you to access the global data
// exported from the DLL. //
// Call the functions exported from a module.
// PCWSTR pszString = L"HelloWorld";
int nLength; // Call int /*__cdecl*/ GetStringLength1(PWSTR pszString);
LPFNGETSTRINGLENGTH1 lpfnGetStringLength1 = (LPFNGETSTRINGLENGTH1)GetProcAddress(hModule, "GetStringLength1");
if (lpfnGetStringLength1 == NULL)
{
wprintf(L"GetStringLength1 cannot be found (Error: 0x%08lx)\n",
GetLastError());
goto Cleanup;
}
nLength = lpfnGetStringLength1(pszString);
wprintf(L"Function: GetStringLength1(\"%s\") => %d\n", pszString, nLength); // Call int __stdcall GetStringLength2(PWSTR pszString);
LPFNGETSTRINGLENGTH2 lpfnGetStringLength2 = (LPFNGETSTRINGLENGTH2)GetProcAddress(hModule, "_GetStringLength2@4");
if (lpfnGetStringLength2 == NULL)
{
wprintf(L"GetStringLength2 cannot be found (Error: 0x%08lx)\n",
GetLastError());
goto Cleanup;
}
nLength = lpfnGetStringLength2(pszString);
wprintf(L"Function: GetStringLength2(\"%s\") => %d\n", pszString, nLength); //
// Call the callback functions exported from a module.
// // Call int __stdcall CompareInts(int a, int b, PFN_COMPARE cmpFunc);
LPFNMAX lpfnMax = (LPFNMAX)GetProcAddress(hModule, "CompareInts");
if (lpfnMax == NULL)
{
wprintf(L"CompareInts cannot be found (Error: 0x%08lx)\n", GetLastError());
goto Cleanup;
}
int max = lpfnMax(2, 3, &Max);
wprintf(L"Function: CompareInts(2, 3,Max) => %d\n", max); //
// Use the class exported from a module.
// // Dynamically-loaded DLL does not allow you to use the class exported
// from the DLL. Cleanup: if (hModule)
{
// Attempt to free and unload the library.
wprintf(L"Unload the dynamically-loaded DLL\n");
if (!FreeLibrary(hModule))
{
wprintf(L"FreeLibrary failed w/err 0x%08lx\n", GetLastError());
}
} // Check whether or not the module is loaded.
fLoaded = IsModuleLoaded(pszModuleName);
wprintf(L"Module \"%s\" is %sloaded\n", pszModuleName, fLoaded ? L"" : L"not "); return 0;
}
演示样例project地址:http://download.csdn.net/detail/x356982611/8071757
C++函数的导出与导入的更多相关文章
- ES5和ES6对象导出和导入(转载,待整理)
1.import ... form...替代 require() //不接收对象 require:require('s.css'); //(es5) improt 's.css' //(es6) // ...
- Oracle简单的备份和恢复-导出和导入(1)
ylbtech-Oracle:Oracle简单的备份和恢复-导出和导入(1) Oracle简单的备份和恢复-导出和导入 1. 用户导出自己的表(emp,dept)返回顶部 1.1, 我们启动Oracl ...
- Oracle简单的备份和恢复-导出和导入(2)
ylbtech-Oracle:Oracle简单的备份和恢复-导出和导入(2) 简单的备份和恢复-导出和导入(2) 1. 用户导入导出文件中的一张表(emp)返回顶部 0.1, 我们在sql plus中 ...
- windows下 Mysql 8.0.x 数据库简单的导出和导入!!!
1.首先需要进入到mysql安装目录下的bin目录,执行cmd进入命令窗口. 2.导出(导出某个数据库,也可以针对某张表导出)2.1导出数据结构以及数据的命令: mysqldump -u root - ...
- [moka同学笔记]PHPexcel之excel导出和导入
原案例来自http://www.sucaihuo.com/有修改 1.目录结构(文件不用解释,应该都可以看得懂,直接看代码)
- ORACLE 导出(exp) & 导入(imp)
导出(exp) & 导入(imp) 利用Export可将数据从数据库中提取出来,就是将select的结果存到一个FS二进制文件上 利用Import则可将提取出来的数据送回到Ora ...
- OCR磁盘的导出和导入、备份和恢复以及移动(ocrconfig命令的应用)
数据库版本:10.2.0.1 一,使用导出.导入进行备份和恢复 Oracle推荐在对集群做调整时,比如增加.删除节点之前,应该对OCR做一个备份,可以使用export 备份到指定文件.如果做了repl ...
- paip 自定义输入法多多输入法词库的备份导出以及导入
paip 自定义输入法词库的备份导出以及导入 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/ ...
- mysql如何利用Navicat 导出和导入数据库
MySql是我们经常用到的数据,无论是开发人员用来练习,还是小型私服游戏服务器,或者是个人软件使用,都十分方便.对于做一些个人辅助软件, 选择mysql数据库是个明智的选择,有一个好的工具更是事半功倍 ...
随机推荐
- Android开发之——编码规范
1. 前言 这份文档参考了 Google Java 编程风格规范和 Google 官方 Android 编码风格规范.该文档仅供参考,只要形成一个统一的风格,见量知其意就可. 2. 源文件基础 2.1 ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- Dalvik和ART简单介绍
1.classes.dex文件初识 我们先把QQ_236.apk后缀改为QQ_236.zip,然后解压.发现有一个classes.dex文件,这个classes.dex是java源代码编译后生 ...
- 受 SQLite 多年青睐,C 语言到底好在哪儿?
SQLite 近日发表了一篇博文,解释了为什么多年来 SQLite 一直坚持用 C 语言来实现,以下是正文内容: C 语言是最佳选择 从2000年5月29日发布至今,SQLite 一直都是用 C 语言 ...
- page template in kentico
Ad-hoc templates are used for one page only, for which they were created - this is why they are not ...
- ES内存持续上升问题定位
https://discuss.elastic.co/t/memory-usage-of-the-machine-with-es-is-continuously-increasing/23537/ ...
- ActiveX插件
C#制作ActiveX插件 首先新建项目--->类库,取名:ActiveXDemo 右键项目属性:应用属性==>程序集信息=>使程序集Com可见, 生成==>输出==>为 ...
- 对于NAS,IP SAN以及iSCSCI SAN存储的一些认识和理解
一直以来用户对于在选购存储产品上有许多不清楚,市场上有NAS, FC SAN,IP SAN和iSCSCI SAN产品,到底哪种类型的产品更适合支撑企业的应用系统呢? 我们经常可以听到用户讲: “NAS ...
- 1. Two Sum[E]两数之和
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- Core篇——初探依赖注入
目录 1.DI&&IOC简单介绍 2.UML类图中六种关联关系 3..net core 中DI的使用 4..net core DI初始化源码初窥 DI&&IOC简单介绍 ...