Loadlibrary无法加载dll
*******************UseDll1.cpp*********************
#include <windows.h>
typedef int (*function1_ptr) ();
function1_ptr function1=NULL;
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
HMODULE myDll = LoadLibrary("Dll1.dll");
if(myDll!=NULL) {
function1 = (function1_ptr) GetProcAddress(myDll,"function1");
if(function1!=NULL)
function1();
else
exit(4);
FreeLibrary(myDll);
}
else
exit(6);
return 0;
}
这样做是为了调用 Dll1.dll,该 Dll1.dll 是通过将邮件发送到我自己的邮件服务器的功能而创建的。上面的代码运行并退出,并且没有发送邮件。
我还将 Dll1.dll 放在与 UseDll1 相同的文件夹中。
编辑: 我将 Dll1.dll 添加到 system32 文件夹中。
*******************UseDll1.cpp*********************
#include <windows.h>
typedef int (*function1_ptr) ();
function1_ptr function1=NULL;
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
HMODULE myDll = LoadLibrary("Dll1.dll");
if(myDll!=NULL) {
function1 = (function1_ptr) GetProcAddress(myDll,"function1");
if(function1!=NULL)
function1();
else
exit(4);
FreeLibrary(myDll);
}
else
exit(6);
return 0;
}
This is done in order to call Dll1.dll which was created with the functionality to send mail to my own mail server. The above code runs and exits,and no mail is sent.
And i also placed the Dll1.dll in the same folder as UseDll1.
EDIT:
I added the Dll1.dll into the system32 folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为什么不调试它并看看哪里失败了?确保您的 dll 确实正在加载(可能只是路径问题或错误的 dll(您可能没有导入/导出函数)。
首先找出问题所在。要么是加载 dll,要么是调用函数,或在函数内部
Why not debug it and see where it fails? Make sure your dll is actually being loaded(it could simply be a path issue or a bad dll(you might have not imported/exported the functions).
Find out where the issue is first. It's either in loading the dll, calling the function, or inside the function
我认为您已经检查过 dll 是否存在。
现在试试这个 ::
尝试将“任何 CPU”更改为 86 或 64。
尝试以管理员身份运行您的应用程序。
I think you have already checked existence of dll.
Now Try this ::
Try to change "Any CPU" to 86 or 64.
Try to run your application as administrator.
我有同样的问题。 此链接解决了这个问题。问题是我没有使用 _T 宏。
I had the same issue. This link resolved it. The issue was that I was not using _T macro.
您是否确认拥有“DLL1.dll”的所有外部依赖项?
即使任何间接链接库不可用,LoadLibrary 也会失败。
Have you verify that you have all external dependencies for "DLL1.dll" ?
LoadLibrary will fail even if any of the indirect linked-library is not available.
在我的 dll 调用经验中,我遇到了同样的问题。我已尽一切努力正确提供路径,但无法加载我的库。首先我以为我的dll有错误,但是一开始就没有解决我的问题。我建议那些认为自己的项目编写正确但仍然遇到同样问题的朋友执行以下步骤:
使用正确的配置构建项目后,它现在可以工作了。
In my dll calling experience, I had same problem. I did everything for giving path rightly but my library can not be loaded. Firstly I thought my dll had error, but nothing solved my problem at start. I advice doing below step for friends who think their project written rightly but still having same problem:
After building project with right configuration, it is working now.
非常感谢您,您的网页对我帮助很大:)我只需要使用 tchar.h 即可使其工作。您可以在答案的其余部分中看到它。
Thank you very much, your web page helped me a lot :) I only had to use tchar.h to make it working. You can see it in the rest of the answer.
仅仅因为代码运行并退出并不意味着它是正确的!您确定:
(myDll!=null)
指针 (function1!=null)
您应该在调试器中单步调试代码,以确保所有这些都发生并且代码确实到达了 function1()。如果确实如此,那么我的猜测是您的电子邮件功能存在错误。
Just because the code runs and exits doesn't mean it's right! Are you sure that:
(myDll!=null)
pointer (function1!=null)
You should step through the code in a debugger to make sure all this is happening and that the code does get to function1(). If it does then my guess would be that your email function has a bug in it.