Hi
We have some non-microsoft Dlls, both homemade and from 3rd part, which we need to call from VBA's inside Microstation.
Until now we have placed them in C:\windows\system32 but that i little 'dirty' and new policies on our network makes that a 'no-go' solution. So where to place thense DLLs ? Are there some MicroStation configuration variable that we can make point to the folder where the DLLs are?
Any suggestions?
Put them in a folder and add that folder to your system PATH variable. I would recommend createing a folder under c:\program files\common files\MSVBA Shared Files.
Then right click on My Computer
Choose Properties
Click on the Advanced Tab
Click on the Environment Variables button.
go to the System Environment Variables (this will make it work for whomever logs on the machine)
find the Path variable, hilight it and click Edit.
add ;c:\program files\common files\MSVBA Shared Files\; (the path between the simicolons needs to match the exact path where you put the dll files and the semicolons are important, they separate each path)
Then click Apply, Ok, Ok until all the dialogs are dismissed. Do not hit cancel or it will not save.
If you want to add this path to many machines you may want to invest in a program to create an install.
Hope this helps.
wirring: We have some non-Microsoft Dlls, both homemade and from 3rd part, which we need to call from VBA's inside Microstation. So where to place thense DLLs ?
There are two types of DLL. Depending on your Microsoft Dlls, either or both of these may be relevant:
Plain old libraries must be in a folder that Windows can find. The search order is defined by Microsoft: if you want to know more search the Microsoft web site.
COM servers are registered with your computer. The registration process involves the Registry. Once a COM server is registered, any COM client can find the DLL and use its methods and properties.
wirring: Are there some MicroStation configuration variable that we can make point to the folder where the DLLs are?
MicroStation relies on Windows to find a DLL.
Regards, Jon Summers LA Solutions
Assuming you are talking about plain old libraries, then the final piece of this puzzle is to understand what PATH will be when VBA tries to load the DLL.
At startup MicroStation sets PATH to the value defined by MS_LIBRARY_PATH. As you can see by running ustation -debug, MS_LIBRARY_PATH is set to the various paths that include MicroStation DLL's. The original value of PATH is appended to MS_LIBRARY_PATH.
So the options are:
- Store your DLL's in one of the directories listed in MS_LIBRARY_PATH, - Add a configuration file that appends your desired DLL location to MS_LIBRARY_PATH - Use SetEnvironmentVariable in your OnProjectLoad method to augment PATH to include the location of your DLL's.
Here is the code to change the environment variable. I found I couldn't use VBA's Environ function because it didn't report any of the changes made via SetEnvironmentVariable.
Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long Function GetEV(strName As String) As String Dim theSize As Long Dim strValue As String
theSize = GetEnvironmentVariable(strName, vbNullString, 0) GetEV = Space(theSize) GetEnvironmentVariable strName, GetEV, theSize GetEV = Left(GetEV, theSize - 1) End Function Sub AppendToVar(strEnvVarName As String, strSuffix As String) Dim strValue As String
strValue = GetEV("PATH") strValue = strValue & strSuffix
SetEnvironmentVariable strEnvVarName, strValue End Sub Sub Test1() AppendToVar "PATH", ";c:\ustation\vba\" Debug.Print "The new path is " Debug.Print GetEV("PATH") End Sub
Regards, John
-> Any suggestions?
One 'dirty' suggestion which may still work.
HTH Todd
==================== Declare Function MyDllFunction Lib "MyExternalDll.dll" (ByRef ReturnStatus As String, ByVal InputVal As String) As Boolean
Sub MyMain() Dim bRet As Boolean Dim szRetString As String Dim szInputVal As String
‘ Place this in project code before making a call to the MyDllFunction function ' Check if the external MyExternalDll.dll file is located with the current project Set fs = CreateObject("Scripting.FileSystemObject") If True = fs.FileExists(Application.CurrentProject.Path + "\MyExternalDll.dll") Then ChDir (Application.CurrentProject.Path) End If
szInputVal = "Test" bRet = MyDllFunction(szRetString, szInputVal)
End Sub
Todd: One 'dirty' suggestion which may still work. A VB developer once gave this to me to try if I was delivering DLLs with my VBA applications. It worked for this one application where the DLL was kept in same directory as the VBA. Declare Function MyDllFunction Lib "MyExternalDll.dll" (ByRef ReturnStatus As String, ByVal InputVal As String) As Boolean Sub MyMain() ‘ Place this in project code before making a call to the MyDllFunction function ' Check if the external MyExternalDll.dll file is located with the current project ... End Sub
Declare Function MyDllFunction Lib "MyExternalDll.dll" (ByRef ReturnStatus As String, ByVal InputVal As String) As Boolean Sub MyMain() ‘ Place this in project code before making a call to the MyDllFunction function ' Check if the external MyExternalDll.dll file is located with the current project ... End Sub
I guess that code may have worked in some other application that provides VBA. Unfortunately MicroStation VBA doesn't have an Application.CurrentProject property (although it does have a Application.Path property).
The application that hosts MicroStation VBA projects is MicroStation. MicroStation is a Windows application, and it looks for files in the Windows search paths. In addition, MicroStation searches additional folders as described by John Gooding.
You don't need extra VBA code to find a DLL. MicroStation will find it for you: just follow the rules.