如何dllimport在Microsoft Visual C ++ [英] How to dllimport in Microsoft Visual C++

查看:194
本文介绍了如何dllimport在Microsoft Visual C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DLL,我想使用它的一些功能。

I have a DLL and I would like to use some of its functions.

#include <iostream>

using namespace std;

extern "C" __declspec(dllimport) int Initialize(char* localPort, char* adminServerName, int rpcTimeout);


int main()
{
    int res = Initialize("7864", "6000@kabc", 10000);

    return 0;
}

我没有DLL的.lib文件,可以链接到它。我想到的一件事是使用LoadLibrary函数,然后使用GetProcAddress()。是否有其他方法?

I don't have the DLL's .lib file, so is there anyway I can link to it. One thing that comes to my mind is to use the LoadLibrary function and then use the GetProcAddress(). Is there any other way?

当我编译以下代码


  • p> error LNK2019:未解析的外部符号_ imp _Initialize在函数_main中引用
  • error LNK2019: unresolved external symbol _imp_Initialize referenced in function _main

致命错误LNK1120:1 unresolved externals

fatal error LNK1120: 1 unresolved externals

我收到上述错误

Visual Studio 2008

I'm using Windows and Visual Studio 2008

推荐答案

从你的措辞,很清楚你使用Dev Studio一些或其他版本。

From your phrasing it is pretty clear you are using Dev Studio some or other version.

为了隐式链接一个dll,VC ++ 需要一个.lib文件,没有问题。没有.lib,你只能使用LoadLibrary和GetProcAddress显式加载dll。

In order to implicitly link against a dll, VC++ needs a .lib file, no questions. Without a .lib you can only explicitly load the dll using LoadLibrary and GetProcAddress.

幸运的是,导入库不包含DLL的导出符号,所以完全合法在VC ++中创建一个dll项目,导出相同的符号,构建它,然后使用生成的.lib文件访问目标dll。

Fortunately import libraries contain nothing other than the dll's exported symbols, so it is perfectly legitimate to simply create a dll project in VC++ that exports the identical symbols, build it, and then use the resulting .lib file to access the target dll.

符号右:根据原来的dll是如何创建的可能有一些装饰来处理。

The trick really is, getting the symbols right: Depending on how the original dll was created there might be some decoration to deal with.

初始化可以从dll中导出几种方式,当查看依赖行为者

"Initialize" could be exported from a dll in several ways when viewed with dependencywalker


  • 初始化 - 通过.DEF文件或通过 externC__declspec(dllexport)int __cdecl Initialize(...

  • _Initalize @ 16 - 导出使用: externC__declspec(dllexport)int __stdcall Initialize(...

  • ?Initialize @@ YAHPADOH @ Z - `__declspec(dllexport)int Initialize(char *,char *,int);

  • ?Initialize @@ YGHPADOH @ Z - `__declspec dllexport)int __stdcall Initialize(char *,char *,int);

  • "Initialize" - was either exported via a .DEF file, or via extern "C" __declspec(dllexport) int __cdecl Initialize(...
  • "_Initalize@16" - was exported using: extern "C" __declspec(dllexport) int __stdcall Initialize(...
  • ?Initialize@@YAHPADOH@Z" - `__declspec(dllexport) int Initialize(char*,char*,int);
  • ?Initialize@@YGHPADOH@Z" - `__declspec(dllexport) int __stdcall Initialize(char*,char*,int);

问题真的是第一种情况 - 不是__cdecl(和大多数dll api的实际上是__stdcall - 所有Windows DLL是__stdcall),那么你必须使用.def文件导出未修饰的名称。

The problem really is the first case - if the calling convention isn't __cdecl (and most dll api's are actually __stdcall - all windows dlls are __stdcall) then you must use a .def file to export the undecorated names.

这篇关于如何dllimport在Microsoft Visual C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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