如何从C ++本机编译的Dll调用函数 [英] How Do I Call A Function From A Native Compiled Dll, From C++

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

问题描述

我一直在调查这个没有用。我尝试使用 ENTRYPOINT 从C ++ DLL(库)调用函数,但没有成功。它不是一个开源的.dll,但它们是一个.NET库。我目前正在尝试将调用C ++ .DLL函数的C#库转换为C ++ CLI。

(所以... C#库(使用dllimport)转换为C ++ CLI

当然DLLIMPORT不起作用,我查看了

 __ declspec(dllexport)

&

 __ declspec(dllimport)

而且我无处可去。我已经阅读了这个以尝试获得一些清晰度,但丢失并仍在阅读。



这是一个有效的(C ++ CLI)示例,用于旧的.DLL之前已经更新了...但是它没有 ENTRYPOINT ,但是较新的那个,我不知道如何添加入口点。



这就是 C ++ CLI版本(没有入口点(旧版本的DLL))的样子:

 typedef int(__ cdecl * Connectf)(const char *); 
Connectf Connect =(Connectf)GetProcAddress(LoadLibrary(LCCAPI.DLL),connectConsole);





这就是LIB的 C#版本的样子:

  private   static   extern   int  connectConsole( string  targetIP); 
[DllImport( CCAPI.dll,EntryPoint = _ ZN5CcApi17disconnectConsoleEi,CallingConvention = CallingConvention.Cdecl)]





PS:这是我的其他程序员之一的C ++ CLI库,但收效甚微: http://pastebin.com/0PTw73WZ



和原始C#Library(更新版本): http://pastebin.com/ccATzTm8



感谢您的时间。



BaSs_HaXoR

解决方案

代码:

  typedef   int  __ cdecl  * Connectf)( const   char  *); 
Connectf Connect =(Connectf)GetProcAddress(LoadLibrary(L CCAPI.DLL), connectConsole);



显示给你如何访问库。 LoadLibrary 调用将DLL加载到内存中, GetProcAddress 调用找到入口点。但是,如果这些调用中的任何一个失败,那么它将无效。您应该修改它,例如:

  typedef   int  __ cdecl  * Connectf)( const   char < /跨度> *); 
HMODULE hModule = LoadLibrary(L CCAPI.DLL);
if (hModule == NULL)
{
// 抛出错误
}
Connectf Connect =(Connectf)GetProcAddress(hModule,L connectConsole);
if (Connect == NULL)
{
// 抛出错误
}



然后你至少会知道它为什么不是工作


I've been looking into this with no avail. I've tried calling a function from a C++ dll (library) WITH an ENTRYPOINT, with no success. It's not an open source .dll, but their is a .NET library for it. I am currently trying to convert the C# library that calls the functions of the C++ .DLL into C++ CLI.
(So... C# library (using dllimport) conversion to C++CLI)
Of course DLLIMPORT doesn't work, and I've looked into

__declspec(dllexport)

&

__declspec(dllimport)

and I'm not getting anywhere. I have read on this to try and gain some clarity, but lost and still reading.

This is a working (C++ CLI) example for the older .DLL before it was updated... but this doesn't have an ENTRYPOINT in it, but the newer one does and i don't know how to add the entrypoint.

This is what the C++ CLI Version (without entrypoint (older version of DLL)) looks like:

typedef int (__cdecl* Connectf)(const char*);
Connectf Connect  = (Connectf)GetProcAddress(LoadLibrary(L"CCAPI.DLL"),"connectConsole");



This is what the C# version of the LIB looks like:

private static extern int connectConsole(string targetIP);
        [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi17disconnectConsoleEi", CallingConvention = CallingConvention.Cdecl)]



P.S.: Here's the C++ CLI Library one of my fellow programmers worked on, with little success: http://pastebin.com/0PTw73WZ

and the original C# Library (updated version): http://pastebin.com/ccATzTm8

Thanks for your time.

BaSs_HaXoR

解决方案

The code:

typedef int (__cdecl* Connectf)(const char*);
Connectf Connect  = (Connectf)GetProcAddress(LoadLibrary(L"CCAPI.DLL"),"connectConsole");


shows you how to access the library. The LoadLibrary call loads the DLL into memory and the GetProcAddress call finds the entry point. However if either of these calls fail then it will not work. You should modify it something like:

typedef int (__cdecl* Connectf)(const char*);
HMODULE hModule = LoadLibrary(L"CCAPI.DLL");
if (hModule == NULL)
{
    // throw an error
}
Connectf Connect  = (Connectf)GetProcAddress(hModule, L"connectConsole");
if (Connect == NULL)
{
    // throw an error
}


Then you will at least have some idea of why it is not working.


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

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