Dynamic Loading of DLL in VB
-
We have a project that will run on standalone workstations as well as on an NT terminal server. We need to be able to call the WTS API in the latter case, so we will use the LoadLibrary function to load the WTSapi32.dll. I can then use GetProcAddr to get the address of the function that I want from the dll. In VB, how can I then call a function from the dll? Easy in C, but for VB? Thanks Pls reply to bbuckley@trilan.com
-
We have a project that will run on standalone workstations as well as on an NT terminal server. We need to be able to call the WTS API in the latter case, so we will use the LoadLibrary function to load the WTSapi32.dll. I can then use GetProcAddr to get the address of the function that I want from the dll. In VB, how can I then call a function from the dll? Easy in C, but for VB? Thanks Pls reply to bbuckley@trilan.com
I don't think that VB can use function pointers to call functions (although I'm not 100% positive). You can Declare a function and use it in VB (see docs on the Declare keyword). However your app will fail to load if wtsapi32.dll isn't on the system. A solution for this would be to encapsulate all your WTS calls in a COM object in a separate DLL. Then, if your app wants to make some WTS calls, it can create an instance of this COM object and do what needs to be done. When your app isn't on a terminal server, it won't ever create this COM object. Another solution would be to create a "stub" DLL, in C, that wrapped each function in wtsapi32 you wanted to call. Use the Declare keyword in your VB app to import each of these functions from your stub DLL. Then, when you wanted to call a certain wtsapi32 function, you'd instead call one of your stub dll's functions to do the dirty work (GetProcAddress(), call function, package return values and other info, then pass back to the calling VB function). If your project is mainly VB, I'd take solution #1. If your project is enough of a mix of C and VB that you don't mind maintaining the C stub as part of the project, go for #2.