解决VC中动态加载DLL时,必须将DLL文件放在EXE文件路经和系统目录下的问题

方法:使用函数SetDllDirectory,新增一个DLL搜索目录即可。

The SetDllDirectory function adds a directory to the search path used to locate DLLs for the application.

BOOL SetDllDirectory(
  LPCTSTR lpPathName
);

Parameters
lpPathName 
[in] Pointer to a null-terminated string that specifies the directory to be added to the search path. If this parameter is NULL or an empty string (""), the call effectively removes the current directory from the default DLL search order. 
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
The SetDllDirectory function affects all subsequent calls to the LoadLibrary and LoadLibraryEx functions. 

After calling SetDllDirectory, the DLL search path is:

The directory from which the application loaded. 
The directory specified by the lpPathName parameter. 
The system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32. 
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System. 
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. 
The directories that are listed in the PATH environment variable. 

To revert to the default search path used by LoadLibrary and LoadLibraryEx, call SetDllDirectory with NULL.

To compile an application that uses this function, define _WIN32_WINNT as 0x0502 or later. For more information, see Using the Windows Headers.

Requirements
Client Requires Windows Vista or Windows XP SP1. 
Server Requires Windows Server "Longhorn" or Windows Server 2003. 
Header Declared in Winbase.h; include Windows.h.

Library Link to Kernel32.lib.

DLL Requires Kernel32.dll.  
Unicode Implemented as SetDllDirectoryW (Unicode) and SetDllDirectoryA (ANSI). 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值