码迷,mamicode.com
首页 > 其他好文 > 详细

手工加载DLL

时间:2016-09-06 06:42:23      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

1.为了能找到dll的函数地址,生成dll时需要将其中的函数声明为C函数(extern "C"):

#ifndef __MYDLL_H
#define __MYDLL_H

#ifdef MYDLL_EXPORTS
#define MYDLL __declspec(dllexport)
#else
#define MYDLL __declspec(dllimport)
#endif

extern "C" MYDLL int Add(int a, int b);

#endif

 

2.调用dll中的函数时,只需要*dll文件,不需要.h和.lib

#include <stdio.h>

#include <WinSock2.h>
#include <Windows.h>

int main()
{
HINSTANCE handle = LoadLibrary("DLL_07.dll");
if (handle)
{
//定义要找的函数原型
typedef int (*DLL_FUNC_ADD) (int,int);

//找到目标函数的地址
DLL_FUNC_ADD dll_func_add = (DLL_FUNC_ADD)GetProcAddress(handle,"Add");
if (dll_func_add)
{
//调用该函数
int result = dll_func_add(10,11);
printf("result:%d\n",result);
}
//卸载
FreeLibrary(handle);
}

return 0;
}

 

3.调用dll的项目属性->常规->字符集->使用多字节字符集。

 

Demo:百度云盘(13207134391)

    DLL_07

    DLL_07_APP

 

手工加载DLL

标签:

原文地址:http://www.cnblogs.com/SZxiaochun/p/5844223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!