Announcement

Collapse
No announcement yet.

LoadLibrary

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

  • LoadLibrary

    I'm a bit confused about LoadLibrary.
    DO I still need to declare the function?

    I have it working in one program (Not sure how) but my goal is to be able to run without the DLL being there(In case it gets deleted etc), detecting that and running an error function instead, is that possible?

    Meanwhile, this is what I have and it does not work, I get "Missing Declaration

    FreeCCSDLL = "FREECCSDLL" 'Global

    Function ProcessCreditCards() As Long
    Local x As Long
    Local hLib As Long
    Local procAddr As Long
    Local Param As Asciiz * 255

    hLib = LoadLibrary("FREECCS.DLL")
    If IsFalse hLib Then
    MsgBox FreeCCSDLL + " is missing or damaged, please verify installation", %MB_ICONSTOP,CCS
    Exit Function
    End If
    procAddr = GetProcAddress(hLib, FreeCCSDLL) 'Function Name is "FreeCCSDLL"
    Result = @FreeCCSDLL("Setup", "ATZ")'
    'etc etc

    -------------
    Scott Turchin


    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
    procAddr = GetProcAddress(hLib, FreeCCSDLL) 'Function Name is "FreeCCSDLL"
    1) FreeCCSDLL must be a null-terminated string
    2) The spelling and case of the function name must be identical
    to that in the EXPORTS statement of the source DLL.
    That is: have you declared Alias "FreeCCSDLL"



    -------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Comment


    • #3
      Here's what I have for the declare and what the author put for his:

      Declare Function FreeCCSDLL Lib "FREECCS.DLL"(Key As Asciiz, Param As Asciiz) As Long
      '-------------------------------------------------
      long FreeCCSDLL(char *key, char *param) // exports index 1
      //"key" and "param" are pointers to zero-terminated strings.
      //If the call is successful, the function returns zero, otherwise
      //the function returns one of the following error codes:
      '-------------------------------------------------

      So I put the declare back in and getting the message:
      "Function referenced before declaration"


      Ideas why?

      -------------
      Scott Turchin




      [This message has been edited by Scott Turchin (edited January 14, 2000).]
      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


      • #4
        FreeCCSDLL = "FREECCSDLL" 'Global
        Declare Function FreeCCSDLL Lib "FREECCS.DLL"(Key As Asciiz, Param As Asciiz) As Long
        This two function-names are NOT Equal.
        Your function-name is FreeCCSDLL
        You tries to get address of FREECCSDLL
        If you declare the functions in FREECCS.DLL you will explicit load
        the DLL at program-start
        This is not what you want. If the file is missing, you want to
        detect that and take some action.



        -------------
        Fred
        mailto:[email protected][email protected]</A>
        http://www.oxenby.se

        Fred
        mailto:[email protected][email protected]</A>
        http://www.oxenby.se

        Comment


        • #5
          FreeCCSDLL = "FreeCCSDLL"

          Tried that too, didn't matter.

          Basically, I want the program to be able to start whether or not the file is there, is that possible? If so, how?



          -------------
          Scott Turchin


          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


          • #6
            Scott, drop the LIB "dllname.dll" from the declaration, then proceed as you were... once you get the address of the function with GetProcAddress(), use CALL DWORD...USING... TO to call the function in the dynamically loaded DLL.



            -------------
            Lance
            PowerBASIC Support
            ( mailto:[email protected][email protected]asic.com</A> )
            Lance
            mailto:[email protected]

            Comment


            • #7
              Thanks!
              I also had a string by the same name as the function, DOH!

              So far so good!

              Here's what I have and the error message I'm getting:
              Code:
              Declare Function FreeCCSDLL(Key As Asciiz, Param As Asciiz) As Long
              
              Function ProcessCreditCards() As Long
              Local x         As Long
              Local hLib      As Long
              Local procAddr  As Long
              Local Param     As Asciiz * 255
              
              hLib = LoadLibrary("FREECCS.DLL")
              If IsFalse hLib Then
                 MsgBox "FreeCCS.DLL is missing or damaged, please verify installation", %MB_ICONSTOP,CCS
                 Exit Function
              End If
              procAddr = GetProcAddress(hLib,"FreeCCSDLL") 'Function Name is "FreeCCSDLL"
              Call Dword Using @FreeCCSDLL("Setup", "ATZ") To Result
              Should I be declaring the function DWORD?

              -------------
              Scott Turchin




              [This message has been edited by Scott Turchin (edited January 15, 2000).]
              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


              • #8
                Be sure you free it using FreeLibrary!

                Note: After this, the CALL DWORD is not valid anymore..

                Comment


                • #9
                  Yup!
                  But,
                  I'm still getting a "Variable expected"
                  Here's what I have entirely so far, it's still raw..


                  Code:
                  '------------------------------------------------------------------------------
                  Function ProcessCreditCards() As Long
                  Local x         As Long
                  Local hLib      As Long
                  Local procAddr  As Long
                  Local Param     As Asciiz * 255
                  
                  hLib = LoadLibrary("FREECCS.DLL")
                  If IsFalse hLib Then
                     MsgBox "FreeCCS.DLL is missing or damaged, please verify installation", %MB_ICONSTOP,CCS
                     Exit Function
                  End If
                  procAddr = GetProcAddress(hLib,"FreeCCSDLL") 'Function Name is "FreeCCSDLL"
                  
                  For x = 1 To wCount
                  
                     VARIABLE EXPECTED ERROR:
                  
                      Call Dword Using @FreeCCSDLL("Setup", "ATZ") To Result
                  
                      'Result = @FREECCSDLL("Port","COM" + Comport)
                  
                      'Call Dword Using @FreeCCSDLL("Port", "COM" + ComPort) To Result
                  
                  
                      'Call FreeCCSDLL("Phone", "")'
                      'Call FreeCCSDLL("SetupTime", "5000")' // 5 seconds FOR Timeouts
                      'Call FreeCCSDLL("SubmitTime", "30000")' // 30 seconds wait for CONNECT
                      '
                      'Call FreeCCSDLL("CreditCard", "1234123412341234")' // numerics only!
                      'Call FreeCCSDLL("Expires", "0899")' // August 1999
                      'Call FreeCCSDLL("Amount", "123")' // numerics only! ($1.23)
                      'Call FreeCCSDLL("Action", "Sell")' // Not Refund/Get/Post/etc.
                      'Call FreeCCSDLL("Terminal", "HC12345654321")'
                      'Call FreeCCSDLL("Port", "COM2")'
                      'Call FreeCCSDLL("Phone", "1-800-555-1212")'
                      'Call FreeCCSDLL("Submit", Param)'
                      
                      'Return returns the following:
                      'CAPTURE <approval number appears here>
                      'SALE <amount appears here>
                      'REF NO <transaction number appears here>
                      'BAL <the day's transaction balance appears here> <the transaction "batch" number appears here>
                  Next
                  
                  Result = FreeLibrary(hLib)
                  
                  End Function
                  -------------
                  Scott Turchin




                  [This message has been edited by Scott Turchin (edited January 15, 2000).]
                  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


                  • #10
                    Hi Scott

                    you are not going about this the right way - the compiler is moaning because you are telling it that the function you want to call is contained within a pointer variable called FreeCCSDLL - besides ProcAddr should be a DWORD! This is how you should go about it (you're almost there):

                    Code:
                    'Declare function prototype
                    Declare Function protoFreeCCSDLL (Key as ASCIIZ,Param as ASCIIZ) as LONG
                    
                    Function ProcessCreditCards() as LONG
                        Local x as LONG
                        Local hLib as LONG
                        Local ProcAddr as DWORD
                        local lResult as LONG
                    
                        'Retrieve DLL handle
                        hLib = LoadLibrary("FREECCS.DLL")
                        If IsFalse hLib Then
                            MsgBox "FreeCCS.DLL is missing or damaged, please verify installation", %MB_ICONSTOP,"CCS"
                            Exit Function
                        End If
                    
                        'Retrieve function address
                        ProcAddr = GetProcAddress(hLib,"FreeCCSDLL")
                        If ProcAddr = %NULL Then
                            MsgBox "Could not find function FreeCCSDLL in FreeCCS.DLL"
                            Exit Function
                        End If
                     
                        Key = "MyKey"
                        Param = "MyParam"
                    
                        'Call pointer to the function using Function prototype
                        Call DWORD ProcAddr USING protoFreeCCSDLL(Key,Param) TO lResult
                        
                        'Do some more calls/processing
                    
                        'Release Library
                        Call FreeLibrary(hLib)
                    
                        Function = lResult
                    End Function
                    Cheers

                    Florent


                    [This message has been edited by Florent Heyworth (edited January 15, 2000).]

                    Comment


                    • #11
                      Very nice!
                      That compiles nicely.
                      I'm not certain on the "prototype" thing though, is that a requirement or is that just a sample of the function?

                      Scott

                      -------------
                      Scott Turchin


                      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


                      • #12
                        Hi Scott

                        the function declaration, it could be a sub, which I called the prototype is necessary when you want to call a function pointer (as in CALL DWORD) and pass it variables to act on. You can think of the prototype as a function signature: if you have a function you want to call using CALL DWORD then you need to declare what type of variables, as well as BYVAL or BYREF, the function accepts - if the funtion does not accept any variables then you can just get away with CALL DWORD ProcAddr. That way the compiler knows what it should pop on the stack, whether to expect a return value, etc...

                        Obtaining a CODEPTR in your own BAS module is actually no different to using the LoadLibrary,GetProcAddress calls. It's just a shortcut which you can use in your own modules (not external ones).

                        Look up CALL DWORD in the PB help file for additional information.

                        Cheers

                        Florent

                        Comment


                        • #13
                          Awesome, thanks!

                          And the code works beautifully, I deleted the DLL and tested and it came up and said "Verify installation", great!!

                          Scott

                          -------------
                          Scott Turchin


                          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


                          • #14
                            I went back into my Winlog program to do this thing properly as well but met up wiht a pointer error, can you explain this?

                            Code:
                            Global procAddr     As Dword
                            Global hLib         As Long
                            'CreateShortCut
                            Declare Function protoCreateShortcut 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  ' Initial window state of program.   
                            
                                              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,CCS
                                                       Exit Function
                                                  Else
                                                      Call Dword ProcAddr Using protoCreateShortCut(_
                                                      FilePath + WinLogEXE,"", sLocation, FilePath,2)' To Result  'Com port to use
                                                      Result = FreeLibrary(hLib)
                                                  End If
                            
                                              Else
                                                  MsgBox "Unable to open DLL, file may be corrupt or missing",48, Mine
                                              End If


                            -------------
                            Scott Turchin


                            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


                            • #15
                              Can you be more precise about your pointer error? Is it a compile-time or run-time error?

                              -------------
                              Lance
                              PowerBASIC Support
                              ( mailto:[email protected][email protected]</A> )
                              Lance
                              mailto:[email protected]

                              Comment


                              • #16
                                Righht after I posted this the compiler error went away. It compiles, and then tells me it can't find the DLL...Think I can handle that piece of it
                                I'll probably take a closer look at it today...
                                THanks
                                Scott

                                -------------
                                Scott Turchin


                                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