Loadlibrary无法加载dll

发布于 2024-10-12 10:36:20 字数 723 浏览 2 评论 0原文

    *******************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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

執念 2024-10-19 10:36:21

为什么不调试它并看看哪里失败了?确保您的 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

浊酒尽余欢 2024-10-19 10:36:21

我认为您已经检查过 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.

半边脸i 2024-10-19 10:36:21

我有同样的问题。 链接解决了这个问题。问题是我没有使用 _T 宏。

I had the same issue. This link resolved it. The issue was that I was not using _T macro.

入怼 2024-10-19 10:36:20

您是否确认拥有“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.

一曲琵琶半遮面シ 2024-10-19 10:36:20

在我的 dll 调用经验中,我遇到了同样的问题。我已尽一切努力正确提供路径,但无法加载我的库。首先我以为我的dll有错误,但是一开始就没有解决我的问题。我建议那些认为自己的项目编写正确但仍然遇到同样问题的朋友执行以下步骤:

  1. 转到您的 DLL 项目并确保您选择了多线程调试而不是多线程(调试)DLL(不要使用 dll,因为它使用 dll 中的一些函数而不是将它们嵌入到内部)作为运行时库(特别是用于调试模式)。您可以在属性>配置属性>C/C++>代码生成下看到运行时库选择。
  2. 当我们的DLL文件没问题后,我们必须确保我们的项目使用正确的平台。如果我们的dll使用x64,我们的项目就必须使用x64。您可以从配置管理器控制它,并且可以从平台框中选择正确的一个。

使用正确的配置构建项目后,它现在可以工作了。

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:

  1. Go to your DLL project and assure that you selected Multi-threaded Debug instead of Multi-thread(Debug) DLL(don't use dll because it uses some function from dll instead of embedding them inside) as runtime library(specially for debug mode). You can see runtime library selection under Property>Configuration Property>C/C++>Code Generation.
  2. After our DLL file is ok, we must ensure that our project uses right platform. If our dll uses x64, our project have to use x64. You can control it from configuration manager and you can select right one from platform box.

After building project with right configuration, it is working now.

草莓味的萝莉 2024-10-19 10:36:20

非常感谢您,您的网页对我帮助很大:)我只需要使用 tchar.h 即可使其工作。您可以在答案的其余部分中看到它。

#pragma once
#include <windows.h>
#include "spinapi.h"
#include <tchar.h>

typedef int (*count_boards_ptr)(void);

int x = 0;
HINSTANCE hinstDLL;
hinstDLL = LoadLibrary(_T("C:\\Smajdalf\\doucko_C\\DLLProblem\\DLLProblem\\spinapi.dll"));
count_boards_ptr count_boards = NULL;
count_boards = (count_boards_ptr) GetProcAddress(hinstDLL, "pb_count_boards");

if(count_boards != NULL) {
    x = count_boards();
}

FreeLibrary(hinstDLL);

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.

#pragma once
#include <windows.h>
#include "spinapi.h"
#include <tchar.h>

typedef int (*count_boards_ptr)(void);

int x = 0;
HINSTANCE hinstDLL;
hinstDLL = LoadLibrary(_T("C:\\Smajdalf\\doucko_C\\DLLProblem\\DLLProblem\\spinapi.dll"));
count_boards_ptr count_boards = NULL;
count_boards = (count_boards_ptr) GetProcAddress(hinstDLL, "pb_count_boards");

if(count_boards != NULL) {
    x = count_boards();
}

FreeLibrary(hinstDLL);
梦初启 2024-10-19 10:36:20

仅仅因为代码运行并退出并不意味着它是正确的!您确定:

  • DLL 已正确加载
    (myDll!=null)
  • GetProcAddress 返回一个有效的
    指针 (function1!=null)

您应该在调试器中单步调试代码,以确保所有这些都发生并且代码确实到达了 function1()。如果确实如此,那么我的猜测是您的电子邮件功能存在错误。

Just because the code runs and exits doesn't mean it's right! Are you sure that:

  • The DLL is being correctly loaded
    (myDll!=null)
  • GetProcAddress is returning a valid
    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文