[英]Visual Studio c++ LoadLibary error: The program can't start because dll is missing from your computer
I'm trying to connect my program to a dll. 我正在尝试将程序连接到dll。
Connect code: 连接代码:
HINSTANCE lib;
lib = LoadLibrary("DLL.dll");
if (lib)
{
cout << "Libary Loaded !";
FreeLibrary(lib);
}
else
{
cout<<GetLastError();
}
DLL code(at DLL.cpp): DLL代码(位于DLL.cpp):
#include "pch.h"
#include "DLL.h"
#include<iostream>
using namespace std;
int f()
{
return 11;
}
Once I run the program I get this error: 一旦运行程序,我将收到此错误:
The program can't start because <dllname> is missing from your computer. 该程序无法启动,因为您的计算机缺少<dllname>。
After each dll request I downloaded the dll from the internet and put it into the folder where the dll and the program are. 在每个dll请求之后,我从互联网上下载了dll,并将其放入dll和程序所在的文件夹中。 Here are some of the dll I requested: 这是我要求的一些dll:
Once I get that error after clicking 'OK' button I get GetLastError() = 126
. 单击“确定”按钮后出现该错误后,我将得到GetLastError() = 126
。
I tried to link my program to the lib VCRUNTIME140_APP.dll
just to check if it is going to load the library and it did. 我试图将我的程序链接到lib VCRUNTIME140_APP.dll
只是为了检查它是否要加载该库,是否确实如此。 So the problem is probably in the library I try to connect to. 因此,问题可能出在我尝试连接的库中。
I work with Visual Studio 2015.Both the DLL and the program are compiled in x86(32bit). 我使用Visual Studio 2015.DLL和程序都在x86(32bit)中编译。 I have all redistribution packages installed. 我已经安装了所有重新分发软件包。 I tried with x64(64bit) but got same errors and the same dlls requests. 我尝试使用x64(64bit),但出现相同的错误和相同的dlls请求。 I tried to put DLL in build and in debug folders both didn't work. 我试图将DLL放入构建和调试文件夹中都无法正常工作。
I also tried using **LoadLibaryA**
and **LoadLibaryW**
but these didn't work as well. 我也尝试使用**LoadLibaryA**
和**LoadLibaryW**
但这些效果不佳。
PS: Just tried dll I created in CodeBlocks instead of Visual Studio 2015 and it connected. PS:刚尝试了我在CodeBlocks中创建的dll,而不是Visual Studio 2015,并且它已连接。 Should be a Visual Studio Compiler specific problem. 应该是Visual Studio编译器特定的问题。
If you look to the syntax of LoadLibrary function: 如果您查看LoadLibrary函数的语法:
HMODULE WINAPI LoadLibrary(
_In_ LPCTSTR lpFileName
);
You see that there is no option to specify from where to load your library 您会看到没有选项可以指定从何处加载库
Instead use the function LoadLibraryEx witch has the following syntax: 而是使用功能LoadLibraryEx来执行以下语法:
HMODULE WINAPI LoadLibraryEx(
_In_ LPCTSTR lpFileName,
_Reserved_ HANDLE hFile,
_In_ DWORD dwFlags
);
So with the use of dwFlags parameter , you can now specify from where your program can try to load your library. 因此,使用dwFlags参数 ,您现在可以指定程序从何处尝试加载库。
example of dwFlags: dwFlags的示例:
LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200: LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x00000200:
witch means: 女巫的意思是:
If this value is used, the application's installation directory is searched
for the DLL and its dependencies. Directories in the standard search path
are not searched.
So to resume, you have to use the dwFlags parameter you want in order to find your dll. 因此,要恢复,必须使用想要的dwFlags参数才能找到您的dll。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.