Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When loading DLLs, "file not found" can often be misleading. It may mean that the DLL or a file it depends on is missing - but if that was the case you would have spotted the problem with Process Monitor.</p> <p>Often, the "file not found" message actually means that the DLL was found, but an error occured when loading it or calling the method.</p> <p>There are actually three steps to calling a procedure in a DLL:</p> <ol> <li>Locate and load the DLL, running the DllMain method if present.</li> <li>Locate the procedure in the DLL.</li> <li>Call the procedure.</li> </ol> <p>Errors can happen at any of these stages. VB6 does all this behind the scenes so you can't tell where the error is happening. However, you can take control of the process using Windows API functions. This should tell you where the error is happening. You can alse set breakpoints and use Process Monitor to examine your program's behaviour at each point which may give you more insights.</p> <p>The code below shows how you can call a DLL procedure using the Windows API. To run it, put the code into a new module, and set the startup object for your project to "Sub Main".</p> <pre><code>Option Explicit ' Windows API method declarations 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 GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias _ "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, _ ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) _ As Long Private Declare Function FormatMessage Lib "kernel32" Alias _ "FormatMessageA" (ByVal dwFlags As Long, lpSource As Long, _ ByVal dwMessageId As Long, ByVal dwLanguageId As Long, _ ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Any) _ As Long Const FORMAT_MESSAGE_FROM_SYSTEM = &amp;H1000 Const MyFunc As String = "MYFUNC" Const MyDll As String = "mylib.dll" Sub Main() ' Locate and load the DLL. This will run the DllMain method, if present Dim dllHandle As Long dllHandle = LoadLibrary(MyDll) If dllHandle = 0 Then MsgBox "Error loading DLL" &amp; vbCrLf &amp; ErrorText(Err.LastDllError) Exit Sub End If ' Find the procedure you want to call Dim procAddress As Long procAddress = GetProcAddress(dllHandle, MyFunc) If procAddress = 0 Then MsgBox "Error getting procedure address" &amp; vbCrLf &amp; ErrorText(Err.LastDllError) Exit Sub End If ' Finally, call the procedure CallWindowProc procAddress, 0&amp;, "Dummy message", ByVal 0&amp;, ByVal 0&amp; End Sub ' Gets the error message for a Windows error code Private Function ErrorText(errorCode As Long) As String Dim errorMessage As String Dim result As Long errorMessage = Space$(256) result = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0&amp;, errorCode, 0&amp;, errorMessage, Len(errorMessage), 0&amp;) If result &gt; 0 Then ErrorText = Left$(errorMessage, result) Else ErrorText = "Unknown error" End If End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload