简体   繁体   English

如何从dll调用函数?

[英]How can I call a function from a dll?

How could I call a function from a DLL? 如何从DLL调用函数?

I tried to declare a void pointer and to store in it the result of GetProcAddress... but didn't work. 我试图声明一个空指针,并将GetProcAddress的结果存储在其中……但是没有用。 I also wanted to declare an unsigned long int (I saw it somewhere on the internet), but then I didn't know how to continue on. 我还想声明一个unsigned long int (我在互联网上的某个地方看到了它),但是后来我不知道如何继续。 :D :d

So, would anybody mind giving me a hand? 那么,有人愿意帮我吗?

Try for something like this. 尝试这样的事情。

typedef int (*PFuncMethods)( int args );

hDLL  = LoadLibrary(L"your.dll");
if( !m_hDLL )
  return;

methods = (PFuncMethods)GetProcAddress(hDLL,"methods");
if ( !(methods) ) {
  FreeLibrary(hDLL);
  hDLL = NULL;
  methods = NULL;
  return;
} 

if( methods(1) == 0) ...

the method name is where you might be stuck aswell. 方法名称也是您可能会卡住的地方。 C++ has name mangling for overloading (even if it's not overloaded) and that depends on the compiler. C ++具有用于重载(即使未重载)的名称处理功能,并且这取决于编译器。 You can either work out the mangled name or turn off mangling for the function by using extern "C" . 您可以使用extern "C"来计算变形的名称或关闭该函数的extern "C" You might use a tool like depends.exe to see all the functions with exact name you would have to use. 您可能会使用诸如depends.exe之类的工具来查看所有具有必须使用的确切名称的功能。

It is much easier to statically link to the DLL using the (import)lib file in windows. 在Windows中使用(import)lib文件静态链接到DLL要容易得多。

您必须创建一个函数指针而不是一个空指针,并将结果存储在GetProcAddress那个函数指针中。

You need to have exact function signature, and to cast the pointer properly. 您需要具有准确的函数签名,并正确地转换指针。

For exmaple, if this is a function receiving int and returning void: 例如,如果这是一个接收int并返回void的函数:

typedef void (*funcptr)(int);
funcptr func = (funcptr)(void*)GetProcAddress(....)
func(17);

Note1: If you confuse the signature, very bad things can happen. 注意1:如果混淆了签名,则可能会发生非常糟糕的事情。 Note2: you also need to know the calling convention used (cdecl, stdcall, etc..) 注意2:您还需要知道所使用的调用约定(cdecl,stdcall等)。

If it's your DLL, better consider making an import library instead. 如果是您的DLL,则最好考虑制作一个导入库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM