DLLs in subfolder << Back



If you ever feel like putting third party DLLs in a subfolder, you might find that VDF doesn't have a built-in way to do that. The correct way to do that is to add the path of the subfolder to the "PATH" envirnoment variable programmatically. However, you should add it to you own "PATH" environment variable (NOT the system "PATH" environment variable). Alternatively, you can call the Windows API SetDllDirectory

Every windows program inherits the enviornment variables from the parent process. Once a program is running, it has its own copy of all the enviornment variables. It is NOT RECOMMENDED to add the "DLL subfolder" to the system "PATH" environment variable as the DLLs in the subfolder will only be used by your program (That's not what the system environment variables are for). For more details, see the Microsoft docuementation of Environment Variables.

Use UI
Use WinKern.pkg
Use WinShell.pkg

External_Function SetEnvironmentVariable "SetEnvironmentVariableA" KERNEL32.DLL String sName String sValue Returns Integer
External_Function GetEnvironmentVariable "GetEnvironmentVariableA" KERNEL32.DLL String sName Integer sValue Integer iSize Returns Integer
External_Function PathAppend "PathAppendA" SHLWAPI.DLL Integer iPath Integer iFile Returns Integer

//External_Function SetDllDirectory "SetDllDirectoryA" KERNEL32.DLL String sPath Returns Integer

External_Function ThirdPartyAPI "ThirdPartyAPI" ThirdPartyVendor.dll Integer iSomeParam Returns Integer

Procedure AddRelativePath Global String sRelativePath
	Integer iVoid
	String sExePath sEnvironment
	ZeroString 260 to sExePath
	Move (GetModuleFileName(0,AddressOf(sExePath),260)) to iVoid
	Move (PathRemoveFileSpec(AddressOf(sExePath))) To iVoid
	Move (PathAppend(AddressOf(sExePath),AddressOf(sRelativePath))) to iVoid
	
	// Using "Path" enviornment to change DLL search location
	ZeroString 4096 to sEnvironment
	Move (GetEnvironmentVariable("Path",AddressOf(sEnvironment),4096)) to iVoid
	Move (CString(sExePath) + ";" + sEnvironment) to sEnvironment
	Move (SetEnvironmentVariable("Path",sEnvironment)) to iVoid
	
	//Using SetDllDirectory instead of using environment variable.
	//Move (SetDllDirectory(sExePath)) to iVoid

End_Procedure

Send AddRelativePath "MySubFolderDLL"
Move (ThirdPartyAPI(123)) to FieldIndex

In the above example, let's say your the full path of your program is "C:\MyProgram\Bin\Program.exe", and you want to put the DLLs in "C:\MyProgram\Bin\MySubFolderDLL", then make sure you call "AddRelativePath" before calling into the DLL. In case you are not clear - "AddRelativePath" can be called after the External_Function commands, as shown the in example above.






Free Web Hosting