本文介绍了NSIS 系统 kernel32::LoadLibrary 不搜索 Outdir 或 Path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 NSIS 安装程序中加载和调用 C 库 DLL 的函数.当我尝试加载 DLL 时,会发出错误 126 (ERROR_MOD_NOT_FOUND).

I am trying to load and call a C library DLL's function in my NSIS installer. When I try to load the DLL, an error 126 is emitted (ERROR_MOD_NOT_FOUND).

这是我用来测试的最小安装程序脚本:

This is the minimal installer script that I am using to test this:

OutFile Main.exe

ShowInstDetails show

Section
  SetOutPath "C:\Program Files (x86)\MyApp"
  System::Call 'kernel32::LoadLibraryA(m "C:\Program Files (x86)\MyApp\API.dll")i.r0 ? e'
  Pop $9
  DetailPrint $9
  DetailPrint $0

  System::Call 'kernel32::GetProcAddress(i r0,m "GetVersion")i.r1 ? e'
  Pop $9
  DetailPrint $9
  DetailPrint $1
  System::Call 'kernel32::FreeLibrary(ir0)'
SectionEnd

您可以看到我正在设置 DLL 所在位置的路径;它的所有依赖项在哪里.然而,在检查 procmon 中的进程时,我看到只有 Windows 系统目录正在搜索依赖项,而不是外路径:

You can see that I am setting my outpath to where the DLL is located; where all its dependencies are. In examining at the process in procmon, however, I see that only the Windows system directory is being searched for the dependencies, not the outpath:

Load Image             C:\Program Files (x86)\MyApp\API.dll  SUCCESS
CreateFile             C:\Program Files (x86)\MyApp\API.dll  SUCCESS
QueryBasicInformationFiC:\Program Files (x86)\MyApp\API.dll  SUCCESS
CloseFile              C:\Program Files (x86)\MyApp\API.dll  SUCCESS
CloseFile              C:\Program Files (x86)\MyApp\API.dll  SUCCESS
Thread Create                                                SUCCESS
CreateFile             C:\Windows\syswow64\DEPENDENCY_1.dll  NAME NOT FOUND
CreateFile             C:\Windows\syswow64\msvcr100.dll      SUCCESS
QueryBasicInformationFiC:\Windows\syswow64\msvcr100.dll      SUCCESS
CloseFile              C:\Windows\syswow64\msvcr100.dll      SUCCESS
CreateFile             C:\Windows\syswow64\DEPENDENCY_2.dll  NAME NOT FOUND
CreateFile             C:\Windows\syswow64\DEPENDENCY_3.dll  NAME NOT FOUND
CreateFile             C:\Windows\syswow64\msvcr100.dll      SUCCESS

如何让我的 outpath 被搜索依赖?需要注意的是C:\Program Files (x86)\MyApp"也在Path环境变量中,为什么也没有被搜索到?

How can I get my outpath to be searched for dependencies? It should be noted that "C:\Program Files (x86)\MyApp" is also in the Path environment variable, so why is that not being searched either?

推荐答案

NSIS 中最近的安全更改已锁定允许您从中加载库的位置.您可以调用 AddDllDirectory 添加其他目录:

Recent security changes in NSIS have locked down the places it allows you to load libraries from. You can call AddDllDirectory to add other directories:

Section
System::Call 'KERNEL32::AddDllDirectory(w "c:\path")' ; Note: Path must exist at this point
System::Call 'KERNEL32::LoadLibrary(t "c:\path\file.dll")p.r0'
System::Call 'KERNEL32::GetProcAddress(pr0, m "somefunction")p.r1'
${If} $1 P<> 0
  ...
${EndIf}
System::Call 'KERNEL32::FreeLibrary(pr0)'
SectionEnd

这篇关于NSIS 系统 kernel32::LoadLibrary 不搜索 Outdir 或 Path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:32