从C ++调用DLL中的函数 [英] Calling functions in a DLL from C++

查看:183
本文介绍了从C ++调用DLL中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案在VS 2008中有2个项目。一个是用C ++编写的DLL,另一个是从一个空白项目创建的一个简单的C ++控制台应用程序。我想知道如何从应用程序调用DLL中的函数。



假设我从一个空白的C ++项目开始,我想调用一个名为 int IsolatedFunction(int someParam)



如何调用它?

解决方案

有很多方法可以做到这一点,但我认为最简单的选择之一是链接应用程序到DLL链接时间,然后使用一个定义文件定义要从DLL导出的符号。



CAVEAT :定义文件方法最适合 undecorated 符号名称。如果你想导出装饰的符号,那么最好不要使用

定义文件方法。



这里是一个简单的例子,



步骤1: export.h 文件中定义函数。

  int WINAPI IsolatedFunction(const char * title,const char * test); 

步骤2: export.cpp 文件。

  #include< windows.h> 

int WINAPI IsolatedFunction(const char * title,const char * test)
{
MessageBox(0,title,test,MB_OK);
return 1;
}

步骤3:将函数定义为导出

  EXPORTS IsolatedFunction @ 1 



步骤4:创建一个DLL项目并添加 export.cpp export.def 文件到此项目。构建此项目将创建 export.dll export.lib 文件。



到链接时的DLL。如果不想在链接时定义入口点,请忽略接下来的两个步骤,并使用 LoadLibrary GetProcAddress 在运行时加载函数入口点。 / p>

步骤5:创建测试应用程序项目以通过添加 export.lib 文件到项目。将 export.dll 文件复制到与测试控制台可执行文件相同的位置。



步骤6 :从测试应用程序中调用 IsolatedFunction 函数,如下所示。

  includestdafx.h

//获取导入函数的函数原型
#include../export/export.h

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//调用dll中找到的导入函数
int result = IsolatedFunction(hello,world);

return 0;
}


I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++ and the other is a simple C++ console application created from a blank project. I would like know how to call the functions in the DLL from the application.

Assume I am starting with a blank C++ project and that I want to call a function called int IsolatedFunction(int someParam)

How do I call it?

解决方案

There are many ways to do this but I think one of the easiest options is to link the application to the DLL at link time and then use a definition file to define the symbols to be exported from the DLL.

CAVEAT: The definition file approach works bests for undecorated symbol names. If you want to export decorated symbols then it is probably better to NOT USE the definition file approach.

Here is an simple example on how this is done.

Step 1: Define the function in the export.h file.

int WINAPI IsolatedFunction(const char *title, const char *test);

Step 2: Define the function in the export.cpp file.

#include <windows.h>

int WINAPI IsolatedFunction(const char *title, const char *test)
{
    MessageBox(0, title, test, MB_OK);
    return 1;
}

Step 3: Define the function as an export in the export.def defintion file.

EXPORTS    IsolatedFunction          @1

Step 4: Create a DLL project and add the export.cpp and export.def files to this project. Building this project will create an export.dll and an export.lib file.

The following two steps link to the DLL at link time. If you don't want to define the entry points at link time, ignore the next two steps and use the LoadLibrary and GetProcAddress to load the function entry point at runtime.

Step 5: Create a Test application project to use the dll by adding the export.lib file to the project. Copy the export.dll file to ths same location as the Test console executable.

Step 6: Call the IsolatedFunction function from within the Test application as shown below.

#include "stdafx.h"

// get the function prototype of the imported function
#include "../export/export.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // call the imported function found in the dll
    int result = IsolatedFunction("hello", "world");

    return 0;
}

这篇关于从C ++调用DLL中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆