How to call WinAPI function SetDllDirectory() in D

2020-07-11 06:23发布

I would like to prevent loading of malicious DLLs that may be possible through access of the current working directory as described in http://msdn.microsoft.com/en-us/library/ff919712(VS.85).aspx

The solution implemented in our C++ apps was to make a WinAPI call to SetDllDirectory(""), which would effectively remove the current working directory from the Windows DLL loading search path. However, it seems this solution is not available for our Delphi apps because the SetDllDirectory() function doesn't exist.

Is there an equivalent call in Delphi that does the same thing as SetDllDirectory("")? Thanks!

3条回答
姐就是有狂的资本
2楼-- · 2020-07-11 06:54
uses DSiWin32;

if not DSiSetDllDirectory('path') then
   ....

DSiSetDllDirectory will also take care of dynamic linking and will fail gracefully on pre-XP SP1 systems.

DSiWin32 is released as a freeware.

查看更多
干净又极端
3楼-- · 2020-07-11 06:57

Calling SetDllDirectory('') doesn't work? I see that it is declared properly in the latest versions of Windows.pas. If you have a version of Delphi in which it isn't declared, you can upgrade to the latest version of Delphi, or declare it yourself.

Update: And there you go... Jens just posted the declaration.

查看更多
狗以群分
4楼-- · 2020-07-11 06:58

This should do the trick:

function SetDllDirectory(lpPathName:PWideChar): Bool; stdcall; external 'kernel32.dll' name 'SetDllDirectoryW';
查看更多
登录 后发表回答