如何阻止延迟加载的DLL抛出“计算机丢失”系统错误?

时间:2016-06-12 22:03:08

标签: c winapi dll plugins teamspeak

我昨天在我的插件中设置了延迟加载:

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
  <nav class="navbar navbar-inverse">
    <div class="container-fluid">
      <div class="navbar-header">
        <a class="navbar-brand" href="index.html">Plethora</a>
      </div>
      <ul class="nav navbar-nav">
        <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown" href="#"> <img id = "menu" src="nav-icon.png"> </a>
          <ul class="dropdown-menu">
           <li><a href="#">Manage Interests </a></li>
           <li><a href="#">Account Settings </a></li>
           <li><a href="#"></a>Email Settings </li>
         </ul>
        </li>
      </ul>
    </div>
  </nav>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>

[1]

我正在初始化插件加载的DLL:

#ifdef _WIN32
#pragma warning (disable : 4100)  /* Disable Unreferenced parameter warning */
#include <windows.h>
#include <delayimp.h>
#endif

...

// Configuration Properties / Linker / Input / Additional Dependencies: ./lib/libcurl.lib;./lib/libxml2.lib;./lib/iconv.lib;./lib/zlib1.lib;%(AdditionalDependencies)
// Configuration Properties / Linker / Input /Delay Loaded Dlls: libcurl;libxml2;iconv;zlib1;%(DelayLoadDLLs)

#include "curl.h"
#include "HTMLparser.h"
#include "xpath.h"

#ifdef _WIN32
#pragma comment(lib, "libcurl")
#pragma comment(lib, "iconv")
#pragma comment(lib, "libxml2")
#pragma comment(lib, "zlib1")
#endif

[2]

添加pragma语句后,这是我的构建日志:

#ifdef _WIN32
    SetDllDirectory(L"./plugins/ts3websitepreview/");
    if (FAILED(__HrLoadAllImportsForDll("libcurl.dll"))) {
        ts3Functions.logMessage("Could not load curl.", LogLevel_ERROR, "Plugin", 0);
        return 1;
    }
    if (FAILED(__HrLoadAllImportsForDll("libxml2.dll"))) {
        ts3Functions.logMessage("Could not load libxml.", LogLevel_ERROR, "Plugin", 0);
        return 1;
    }
    if (FAILED(__HrLoadAllImportsForDll("zlib1.dll"))) {
        ts3Functions.logMessage("Could not load zlib1.", LogLevel_ERROR, "Plugin", 0);
        return 1;
    }
    if (FAILED(__HrLoadAllImportsForDll("iconv.dll"))) {
        ts3Functions.logMessage("Could not load iconv.", LogLevel_ERROR, "Plugin", 0);
        return 1;
    }
#endif

立即改为:

1>------ Build started: Project: ts3websitepreview, Configuration: Debug Win32 ------
1>Build started 2016-06-15 06:35:23 PM.
1>InitializeBuildStatus:
1>  Creating "Debug\ts3websitepreview.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  plugin.c
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>Link:
1>     Creating library .\ts3websitepreview.lib and object .\ts3websitepreview.exp
1>LINK : warning LNK4199: /DELAYLOAD:libcurl ignored; no imports found from libcurl
1>LINK : warning LNK4199: /DELAYLOAD:libxml2 ignored; no imports found from libxml2
1>LINK : warning LNK4199: /DELAYLOAD:iconv ignored; no imports found from iconv
1>LINK : warning LNK4199: /DELAYLOAD:zlib1 ignored; no imports found from zlib1
1>Manifest:
1>  All outputs are up-to-date.
1>LinkEmbedManifest:
1>  All outputs are up-to-date.
1>  ts3websitepreview.vcxproj -> .\ts3websitepreview.dll
1>FinalizeBuildStatus:
1>  Deleting file "Debug\ts3websitepreview.unsuccessfulbuild".
1>  Touching "Debug\ts3websitepreview.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:01.60
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

经过没有更改代码的其他构建。

但相反,它仍然试图在插件初始化之前加载DLL,给我一个:

1>------ Build started: Project: ts3websitepreview, Configuration: Debug Win32 ------
1>Build started 2016-06-15 06:39:54 PM.
1>InitializeBuildStatus:
1>  Creating "Debug\ts3websitepreview.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  plugin.c
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>Manifest:
1>  All outputs are up-to-date.
1>LinkEmbedManifest:
1>  All outputs are up-to-date.
1>  ts3websitepreview.vcxproj -> .\ts3websitepreview.dll
1>FinalizeBuildStatus:
1>  Deleting file "Debug\ts3websitepreview.unsuccessfulbuild".
1>  Touching "Debug\ts3websitepreview.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.71
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

我使用教程herehere来延迟加载。我还阅读了问题herehereherehere,但没有人解决我的问题。

1 个答案:

答案 0 :(得分:2)

不要忘记包含 "*.dll" 扩展名。在链接器选项中,您必须指定以下内容:

libcurl.dll;libxml2.dll;zlib1.dll;iconv.dll;

如果使用#pragma comment(lib, "libcurl"),则不必在链接器选项中再次指定它。

SetDllDirectory(L"./plugins/ts3websitepreview/");就足够了。您不需要__HrLoadAllImportsForDll

如果您想提前调用DLL的DLL_PROCESS_ATTACH,请改用LoadLibrary