title: Accessing dynamically loaded DLL (with LoadLibrary) in Visual Basic 6
body: <p>I have a need to create a wrapper for a DLL, loading and unloading it as needed (for those interested in the background of this question, see <a href="https://stackoverflow.com/questions/1662075/how-to-work-around-memory-leaking-3rd-party-dll-no-source-code-accessed-by-tomc"><a href="https://stackoverflow.com/questions/1662075/how-to-work-around-memory-leaking-3rd-party-dll-no-source-code-accessed-by-tomc">How to work around memory-leaking 3rd party DLL (no source code) accessed by Tomcat application?</a></a>) . I'm doing it in Visual Basic 6, and the loading and unloading with the following example works:</p>
  
  <pre><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 Sub cmdTestLoadingDLL_Click()
  
      Dim lb As Long, pa As Long    
      lb = LoadLibrary("D:\projects\other\VB_DLLs\TestDLL\TestDLL.dll")    
  
      Msgbox "Library address: " + lb
  
      FreeLibrary lb    
  
  End Sub
  </code></pre>
  
  <p>I can see using <a href="http://technet.microsoft.com/de-de/sysinternals/bb896653.aspx" rel="nofollow noreferrer">Process Explorer</a> that the DLL is loaded to memory when the messagebox is displayed, and is discarded afterwards. However, calling the method is naturally not enough - I need to access the methods within the dynamically loaded DLL. </p>
  
  <p>How can I achieve this? I would like to call the method getVersion in class mainClass, which is in TestDLL, like this:</p>
  
  <pre><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 Sub cmdTestLoadingDLL_Click()
  
      Dim lb As Long, pa As Long
      Dim versionString As String
  
      lb = LoadLibrary("D:\projects\other\VB_DLLs\TestDLL\TestDLL.dll")    
  
      versionString = "- From DLL: " + mainClass.getVersion
      MsgBox versionString
  
      FreeLibrary lb    
  
  End Sub
  </code></pre>
  
  <p>However, the line </p>
  
  <pre><code>versionString = "- From DLL: " + mainClass.getVersion
  </code></pre>
  
  <p>throws an error "Object required".</p>