Announcement

Collapse
No announcement yet.

LoadLibrary

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • LoadLibrary

    Think I got a grasp on this, I took working code and converted this for a sample, anyone find anything wrong with this?

    Scott

    Code:
    #Compile Exe
    #Dim All
    #Option Version4
    #Register None
    #Include "Win32api.inc"
    
    
    Declare Function ProcessMyFunction(DLvar1 As Long, var2 As String) As Long
    Declare Function MyFunction Lib "MY.DLL"(var1 As Long, var2 As String) As Long
    
    Function PbMain() As Long
    Local wUser As String
    wUser = "UserName:"
    
    
    'These variables are ONLY samples, don't even have to be there
    'If they don't change, set them in ProcessMyFunction as local variables
    'vs passing them through the "Thunk" type DLL.
    If IsTrue ProcessMyFunction(SizeOf(wUser), wUser) Then
       MsgBox "It worked!!", %MB_ICONINFORMATION,"Wahoooo!"
    Else
       MsgBox "It didn't work, sorry charlie..", %MB_ICONINFORMATION,":("
    End If
    End Function
    '===================================================================================
    Function ProcessMyFunction(var1 As Long, var2 As String) As Long
    Local hLib As Long
    Local procAddr As Dword
    
    hLib = LoadLibrary("MY.DLL")
    'Where "MYFUNCTION" is the function name in the DLL, IN UPPER CASE!
    procaddr = GetProcAddress(hLib,"MYFUNCTION")
    If ProcAddr = %NULL Then
        'Failed to find the function. Do Msgbox or error out
        MsgBox "Failed to find function MYFUNCTION in MY.DLL" , %MB_ICONSTOP, "Error, failed to find procedure"
        Exit Function ' Function is already false here
    Else
      'Found the function, process it now
       Call Dword ProcAddr Using MyFunction(var1,var2) 'Adjust function input accordingly, may have ot pass variables
       Function = %TRUE 'or a return from the function itself, suggestions?
    End If
    End Function

    -------------
    Scott
    mailto:[email protected][email protected]</A>
    Scott Turchin
    MCSE, MCP+I
    http://www.tngbbs.com
    ----------------------
    True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

  • #2
    Hi Scott,

    I've been working on a project in PB which allows the use of "Plug in" tools, in the form of other DLL's.

    I use GetProcAddress() and LoadLibrary like you have, but with one major difference:

    If your sample below was changed to look like how I do it, it would be:

    Declare Function MyFunction (var1 As Long, var2 As String) As Long


    Notice how the lib "my.dll" is missing. The Declare Function merely tells PB how the parameters are specified, and the name of the function could be anything, it does not need to match the name of the function in my.dll

    Once you have the proc address of "MYFUNCTION" (as in your code), you then use the CALL DWORD line and use the above declaration.

    e.g.

    Declare Function Anything (var1 As Long, var2 As String) As Long

    ...
    ...
    procaddr = GetProcAddress(hLib,"MYFUNCTION")
    CALL DWORD ProcAddr Using Anything(var1,var2) TO lFunctionResult

    As you can see, the declared function name is irrelevant, but the parameters are.

    The return value of the function call will be placed into lFunctionResult.

    Hope this helps....

    Dorian.

    ------------------


    [This message has been edited by DorianDarby (edited April 04, 2000).]

    Comment


    • #3
      Thanks and Hi
      Question is, if you run the application and the DLL is not in the path or is not on the hard drive, with my method the program should continue, testing now...

      Yes, this here works perfectly now!! I just tested this and if the DLL was missing (I renamed it) the program would say "DLL Not found, missing or corrupt" etc....
      And when the DLL was there it created a shortcut on my desktop.....


      Also, I don't have the source to the DLL in case anyone is wondering...people ask me all the time Wish I did, but I don't...

      Code:
      Declare Function CreateShortcut _  Alias "CREATESHORTCUT"_
                       (sPath As String, _               ' Path of the file the shortcut refers too.
                        sArguments As String, _          ' Program command line args.
                        sLocation As String, _           ' Destination path for the shortcut.
                        sWorkingDir As String, _         ' Working directory.
                        ByVal nCmdShow As Long) As Long
      
      
      Function CreateTheShortCut() As Long
      
          Local procAddr  As Dword
          Local hLib      As Long
          Local IsNt      As Long
          Local sLocation As String          'For placing icon
      
          If Instr(Environ$("OS"),"Windows_NT") Then IsNT = 1
      
          If IsNT Then
              sLocation = Environ$("SystemRoot") & "\Profiles\All Users\Desktop\" & g_sWinLog & " For Windows NT.LNK"
          Else
              sLocation = Environ$("windir") & "\Desktop\" & g_sWinLog & " For Windows 98.LNK"
          End If
          
          hLib = LoadLibrary("SHORTCUT.DLL")
          If hLib Then
              procAddr = GetProcAddress(hLib, "CREATESHORTCUT")
              If ProcAddr = %NULL Then
                  MsgBox "Could not find function ""CreateShortcut"" in SHORTCUT.DLL",%MB_ICONSTOP,g_szCCS
                  'But continue anyway
              Else
                  Call Dword ProcAddr Using CreateShortCut(_
                  g_sWinLogFilePath & g_sWinLogEXE,"", sLocation, g_sWinLogFilePath,2)' To g_Result
                  g_Result = FreeLibrary(hLib)
      '            If IsTrue Exist("Shortcut.dll") Then Kill "Shortcut.dll"
              End If
          Else
              MsgBox "Unable to open Shortcut.dll, file may be corrupt or missing",48, g_szMINE
          End If
      
      End Function

      ------------------
      Scott
      mailto:[email protected][email protected]</A>
      Scott Turchin
      MCSE, MCP+I
      http://www.tngbbs.com
      ----------------------
      True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

      Comment

      Working...
      X