Results 1 to 7 of 7

Thread: Loadlibrary / Declaring dll function at runtime

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Loadlibrary / Declaring dll function at runtime

    My software uses a Can serial card. I need to support three different types of card (PCMCIA, PCI, USB) each of these have there own dll. The dll's all contain the same function / function names.

    At the moment I have to get all the users to rename their dll file to 'cancard.dll' then I am using:

    VB Code:
    1. Declare Function CANPC_reset_board Lib "cancard.dll" Alias "_CANPC_reset_board@0" () As Long

    to delcare the function(s).

    I want to get away from forcing the user to rename their DLL's and automatically detect their dll and delcare it at runtime.

    I briefly looked at LoadLibrary, but it appears you need to use CallWindowProc to call the function. I still want to use the same syntax as before e.g

    VB Code:
    1. dim err_code as long
    2.  
    3. err_code = CANPC_reset_board

    Any ideas?

    Cheers
    Last edited by Rick H; Jun 26th, 2003 at 07:41 AM.

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    Create an alias for each dll and call the appropriate aliased function at runtime.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301
    Have you got an example of how to do this?

    Cheers

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    VB Code:
    1. Declare Function CANPC_reset_board_pmcia Lib "cancard.dll" Alias "_CANPC_reset_board@0" () As Long
    2. Declare Function CANPC_reset_board_pci Lib "cancrd2.dll" Alias "_CANPC_reset_board@0" () As Long
    3. Declare Function CANPC_reset_board_usb Lib "cancrd3.dll" Alias "_CANPC_reset_board@0" () As Long

    Note that I don't know what the real names of the dlls are - you'll have to edit that..

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301
    Thats an idea, though they would normally only have one of the DLLs one their PC so the other declares would cause an error.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301
    Actually it might work, the declarations don't cause an error unless I call the function and the dll doesn't exist. I can check which dll exists at runtime then use this:

    VB Code:
    1. private const PCMCIA = 1
    2. private const PCI = 2
    3. private const USB = 3
    4.  
    5. private cardtype as byte
    6.  
    7. public function CanPc_reset_board() as long
    8.  
    9. select case cardtype
    10. case PCMCIA
    11.    CanPc_reset_board=canpc_reset_board_pcmcia
    12. case PCI
    13.    CanPc_reset_board=canpc_reset_board_pci
    14. case USB
    15.    CanPc_reset_board=canpc_reset_board_usb
    16. end select
    17.  
    18. end sub

  7. #7
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Just trap the error and use it to determine that the dll doesn't exist, maybe?

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