本文介绍了显式加载DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图明确链接到一个DLL。除了DLL文件本身和一些关于类及其成员函数的文档,没有其他资源可用。

I'm trying to explicitly link with a DLL. No other resources is available except the DLL file itself and some documentation about the classes and its member functions.

在文档中,每个类都有自己的

From the documentation, each class comes with its own


  1. 成员typedef

    例如: typedef std :: map< std :: string,std :: string> ; Server :: KeyValueMap,typedef std :: vector< std :: string> Server :: String Array

  2. 成员枚举

    示例:枚举Server :: Role {NONE,HIGH, LOW}

  3. 成员函数

    示例: void Server :: connect(const StringArray,const KeyValueMap) void Server :: disconnect()

  1. member typedef
    example: typedef std::map<std::string,std::string> Server::KeyValueMap, typedef std::vector<std::string> Server::String Array
  2. member enumeration
    example: enum Server::Role {NONE,HIGH,LOW}
  3. member function
    example: void Server::connect(const StringArray,const KeyValueMap), void Server::disconnect()

实现google搜索的代码,我管理加载dll可以调用断开连接函数。

Implementing the codes from google search, i manage to load the dll can call the disconnect function..

dir.h

LPCSTR disconnect = "_Java_mas_com_oa_rollings_as_apiJNI_Server_1disconnect@20";
LPCSTR connect =
"_Java_mas_com_oa_rollings_as_apiJNI_Server_1connect@20";

我从depends.exe得到了上面的函数名。这是什么在C ++中被称为修饰/错乱的函数名称?

I got the function name above from depends.exe. Is this what is called decorated/mangled function names in C++?

main.cpp

#include <iostream>
#include <windows.h>
#include <tchar.h>
#include "dir.h"

typedef void (*pdisconnect)();

int main()
{
    HMODULE DLL = LoadLibrary(_T("server.dll"));
    pdisconnect _pdisconnect;`

    if(DLL)
    {
        std::cout<< "DLL loaded!" << std::endl;
        _disconnect = (pdisconnect)GetProcAddress(DLL,disconnect);


        if(_disconnect)
        {
            std::cout   << "Successful link to function in DLL!" << std::endl;
        }

        else
        {
            std::cout<< "Unable to link to function in DLL!" << std::endl;
        }
    }
    else
{
std::cout<< "DLL failed to load!" << std::endl;
}
FreeLibrary (DLL);
return 0;}

如何调用 em> connect 成员函数,它在dll中声明了参数datatype?

How do i call (for example) the connect member function which has the parameter datatype declared in the dll itself?

编辑

更多信息:


  • 该DLL带有使用Java的示例实现。 Java示例包含使用SWIG和源代码生成的Java包装器。

  • 该文档列出了所有类,其成员函数及其数据类型。根据doc,列表是从C ++源代码生成的。

  • 没有提供其他信息(没有关于使用什么编译器生成DLL的信息) li>
  • The DLL comes with an example implementation using Java. The Java example contains a Java wrapper generated using SWIG and a source code.
  • The documentation lists all the class, their member functions and also their datatypes. According to the doc, the list was generated from the C++ source codes.(??)
  • No other info was given (no info on what compiler was used to generate the DLL)

我的同事正在使用Java实现接口,基于给出的Java示例,而我被要求使用C ++实现。该DLL来自第三方公司。

My colleague is implementing the interface using Java based on the Java example given, while I was asked to implement using C++. The DLL is from a third party company.

我会问他们关于编译器。我应该从他们那里得到任何其他信息?

I'll ask them about the compiler. Any other info that i should get from them?

我快速阅读了JNI,但我不明白如何实现这种情况。

I had a quick read through about JNI but i dont understand how it's implemented in this case.

更新

我有点困惑...(OK,OK ... very confused )

i'm a little confused... (ok, ok... very confused)


  1. 每次调用(GetProcAddress)每个公共成员函数只有当我想使用它们吗?

  2. 我创建一个虚拟类来模仿dll中的类。然后在类定义里面,我从DLL调用等效的函数? (我在这里有意义吗?)fnieto,这是你在你的帖子结束时显示我吗?

  3. 可以从DLL实例化整个类吗?

使用我的第一篇文章中描述的连接功能。从Depends.exe DLL输出,

I was trying to use the connect function described in my first post. From the Depends.exe DLL output,


  • std :: map // KeyValueMap具有以下成员函数: del ,empty,get,has_1key,set

  • std :: vector // StringArray具有以下成员函数: clear,get,isEMPTY,reserve,set,size

地图和矢量在我的编译器(VS 2005)...

which is different from the member functions of map and vector in my compiler (VS 2005)...

任何想法?或者我在这里得到错误的图片...

Any idea? or am i getting the wrong picture here...

推荐答案

除非你使用反汇编器并试图找出参数类型assemly代码,你不能。这些类型的信息不存储在DLL中,而是存储在DLL中的头文件中。如果你没有它,这个DLL是不可能被你使用的。

Unless you use a disassembler and try to figure out the paramater types from assemly code, you can't. These kind of information is not stored in the DLL but in a header file coming with the DLL. If you don't have it, the DLL is propably not meant to be used by you.

这篇关于显式加载DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:12