I have a DLL and I need to call it using LoadLibrary("func1.dll")
. The full path of func1.dll
is omitted from LoadLibrary
because I have to set the PATH
variable to the place where func1.dll
resides, since func1.dll
references another DLL called func2.dll
.
So, in order to get this to work I use the following code:
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Private Sub t1()
Dim lb As Long
Dim dllpath As String
dllpath = "C:\temp\DllsOffice\DLLsOffice\Debug"
SetEnvironmentVariable "PATH", dllpath
Debug.Print Environ("PATH")
lb = LoadLibrary("func1.dll")
MsgBox lb
FreeLibrary lb
End Sub
which works perfectly fine for VBA of Offices 2007, 2010, 2013, 2016 even the Office 2019. 32 and 64 bits.
The problem arises when writing the above code in VBA of the Office installed from Microsoft Store:
When running the above VBA code in Office from Microsoft Store the line:
LoadLibrary("func1.dll")
returns 0, meaning that the DLL was not loaded. So, I ran out of ideas in order to get this to work, but with no success til now.
Here are some additional information about the problem and what I've tried to do:
Copying the DLL to executable path. For instance, if I place
func1.dll
(andfunc2.dll
) inC:\Program Files (x86)\Microsoft Office\Office14
I can useLoadLibrary
without the need of usingSetEnvironmentVariable "PATH", dllpath
. But I cannot copy the DLL to the path of Office from Microsoft StoreC:\Program Files\WindowsApps\Microsoft.Office.Desktop.Word_16040.10827.20181.0_x86__8wekyb3d8bbwe\Office16
because it gives access denied;I completely sure that
SetEnvironmentVariable "PATH", dllpath
is working fine on Office from Microsoft Store. I tested this putting an executable inside theC:\temp\DllsOffice\DLLsOffice\Debug
and then callingShell "test.exe"
(without passing the full path) mytest.exe
program is opened normally.
Do you what I'm missing or have any ideas for me to follow? Thanks you all.
PATH
, and "If a DLL has dependencies, the system searches for the dependent DLLs as if they were loaded with just their module names. This is true even if the first DLL was loaded by specifying a full path". – ForeshoreLoadLibrary
first using full paths. – Foreshorefunc2.dll
first and thenfunc1.dll
worked partially. Using the functionSetDllDirectoryA
from kernel32 did the same effect (worked partially). The problem is that at some point of the application another DLL from a different path (not know previously) is called and an exception is thrown. – Nolte