CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    Using FreeLibrary API

    Sometimes I get an error stating "The instruction at 'address' referenced memory at 'address'. The memory could not be read." when calling FreeLibrary. Could somebody show me the correct syntax for using FreeLibrary? Here is the code I wrote that sometimes generates this error:


    private Function IsDllRegisterable(byval strPath as string) as Boolean
    Dim lb as Long, pa as Long
    Dim lReturn as Long

    ' Format our string
    strPath = strPath & Chr(0)

    ' Map dll into the calling process
    lb = LoadLibrary(strPath)

    If lb = 0 then
    IsDllRegisterable = false
    Exit Function
    End If

    ' Retrieve the address of the DLLRegisterServer
    pa = GetProcAddress(lb, "DllRegisterServer")

    ' If no address found, not registereable
    If pa = 0 then
    IsDllRegisterable = false
    else
    IsDllRegisterable = true
    End If

    ' unmap the library's address
    lReturn = FreeLibrary(lb)
    End Function





  2. #2
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Using FreeLibrary API

    I can't tell what's wrong with your code but the following works:

    private Function CanOLERegister(byval sFilename as string) as Boolean

    Dim lModule as Long
    Dim lProcAddress as Long
    Dim bUnload as Boolean

    lModule = GetModuleHandle(sFilename)
    If lModule = 0 then
    lModule = LoadLibrary(sFilename)
    bUnload = true
    else
    '\\ Module handle could not be retrieved...show the error:
    If Err.LastDllError > 0 then
    MsgBox "GetModuleFilename failed for: " & sFilename & " - error code " & Err.LastDllError
    End If
    End If
    If lModule > 32 then
    '\\ Module loaded OK
    lProcAddress = GetProcAddress(lModule, RegisterProc)
    CanOLERegister = (lProcAddress <> 0)
    If bUnload then
    Call FreeLibrary(lModule)
    End If
    else
    '\\ Module library could not be loaded...show the error:
    If Err.LastDllError > 0 then
    MsgBox "LoadLibrary failed for: " & sFilename & " - error code " & Err.LastDllError
    End If
    End If

    DoEvents

    If Err.LastDllError > 0 then
    Debug.print Err.LastDllError
    End If

    End Function




    HTH,
    Duncan Jones
    Clearcode Development Ltd



    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  3. #3
    Guest

    Re: Using FreeLibrary API

    Thanks alot! I'll try your method.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured