?
Solved

Calling DLL At Runtime From VB

Posted on 2003-02-19
11
Medium Priority
?
1,892 Views
Last Modified: 2012-06-27
Hi,

I have the need to write an application that needs to have a certain part that has an interface that can be different for different states/countries.  

So I thought I could have multiple DLL's that contain the different Interfaces.  Then call the required DLL for the current State/Country.

I tried creating Multiple DLL's with the same name and simply replacing one with the other, however this does not work.

All DLL's will be written in VB and Accessed from VB...

Thanks In Advance,

Marcus
0
Comment
Question by:MarcusW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 7985193
Create only one DLL. Within the DLL create a Public function that will accept parameters from you related to different states and countries. Execute respctive private memebers in the DLL according to different parameters.
0
 
LVL 1

Expert Comment

by:Munim
ID: 7985781
Use the API function GetProcAddress, and thats all... You can access DLL entries from a library at run-time...
0
 
LVL 1

Expert Comment

by:Munim
ID: 7985783
Oooooooops, if you need source code... Ask me for it...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Hornet241
ID: 7985914
Access the DLLs like this

Dim MyDLL as Object
Select Case Country
    Case is = "USA"
        Set MyDLL = Createobject("USA.DLL")
    Case is = "Canada"
        Set MyDLL = Createobject("Canada.DLL")
    Case is = "UK"
        Set MyDLL = Createobject("UK.DLL")
End Select

ans = MyDLL.FunctionName(Para1, Para2)
0
 

Author Comment

by:MarcusW
ID: 7989953
In reply to Hornet241, I get the error "ActiveX Component Can't Create Object".  Does the DLL need to be registered and does it need to have any special requirements to be able to call it this way?

In reply to Munim, It would be great if you could post some source code on how to use GetProcAddress ...

Thanks

Marcus
0
 
LVL 1

Expert Comment

by:Munim
ID: 7993499
I guess this will help you:

--begin of code--
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 Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)
Private mlngParameters() As Long 'list of parameters
Private mlngAddress As Long 'address of function to call
Private mbytCode() As Byte 'buffer for assembly code
Private mlngCP As Long 'used to keep track of latest byte added to code

Public Function CallApiByName(libName As String, funcName As String, ParamArray FuncParams()) As Long
   Dim lb As Long, i As Integer
   ReDim mlngParameters(0)
   ReDim mbytCode(0)
   mlngAddress = 0
   lb = LoadLibrary(ByVal libName)
   If lb = 0 Then
      MsgBox "DLL not found", vbCritical
      Exit Function
   End If
   mlngAddress = GetProcAddress(lb, ByVal funcName)
   If mlngAddress = 0 Then
      MsgBox "Function entry not found", vbCritical
      FreeLibrary lb
      Exit Function
   End If
   ReDim mlngParameters(UBound(FuncParams) + 1)
   For i = 1 To UBound(mlngParameters)
      mlngParameters(i) = CLng(FuncParams(i - 1))
   Next i
   CallApiByName = CallWindowProc(PrepareCode, 0, 0, 0, 0)
   FreeLibrary lb
End Function

Private Function PrepareCode() As Long
    Dim lngX As Long, codeStart As Long
    ReDim mbytCode(18 + 32 + 6 * UBound(mlngParameters))
    codeStart = GetAlignedCodeStart(VarPtr(mbytCode(0)))
    mlngCP = codeStart - VarPtr(mbytCode(0))
    For lngX = 0 To mlngCP - 1
        mbytCode(lngX) = &HCC
    Next
    AddByteToCode &H58 'pop eax
    AddByteToCode &H59 'pop ecx
    AddByteToCode &H59 'pop ecx
    AddByteToCode &H59 'pop ecx
    AddByteToCode &H59 'pop ecx
    AddByteToCode &H50 'push eax
    For lngX = UBound(mlngParameters) To 1 Step -1
        AddByteToCode &H68 'push wwxxyyzz
        AddLongToCode mlngParameters(lngX)
    Next
    AddCallToCode mlngAddress
    AddByteToCode &HC3
    AddByteToCode &HCC
    PrepareCode = codeStart
End Function

Private Sub AddCallToCode(lngAddress As Long)
    AddByteToCode &HE8
    AddLongToCode lngAddress - VarPtr(mbytCode(mlngCP)) - 4
End Sub

Private Sub AddLongToCode(lng As Long)
    Dim intX As Integer
    Dim byt(3) As Byte
    CopyMemory byt(0), lng, 4
    For intX = 0 To 3
        AddByteToCode byt(intX)
    Next
End Sub

Private Sub AddByteToCode(byt As Byte)
    mbytCode(mlngCP) = byt
    mlngCP = mlngCP + 1
End Sub

Private Function GetAlignedCodeStart(lngAddress As Long) As Long
    GetAlignedCodeStart = lngAddress + (15 - (lngAddress - 1) Mod 16)
    If (15 - (lngAddress - 1) Mod 16) = 0 Then GetAlignedCodeStart = GetAlignedCodeStart + 16
End Function

--end of code--
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8002225
>>> Does the DLL need to be registered and does it need to have any special requirements to be able to call it this way?


A DLL has to be registered to use it in any way.

The  "ActiveX Component Can't Create Object".  error is caused by the DLL not being registered.


0
 

Author Comment

by:MarcusW
ID: 8005177
I have found the solution, I used Hornet241's answer to get to my solution but it still wasn't exectly what I needed...  So can Hornet241 reply again referencing my solution and I will award the points to you...

My Solution:
-------------------------------------------------
Dim MyDLL as Object

Select Case Country
   Case is = "USA"
       Set MyDLL = Createobject("USA.ClassName")
   Case is = "Canada"
       Set MyDLL = Createobject("Canada.ClassName")
   Case is = "UK"
       Set MyDLL = Createobject("UK.ClassName")
End Select

ans = MyDLL.FunctionName(Para1, Para2)
-------------------------------------------------

Notice I added the ClassName after the DLL name. This now works great.

Thanks to Hornet241 for pointing me in the right direction...

Marcus
0
 
LVL 3

Accepted Solution

by:
Hornet241 earned 1200 total points
ID: 8011637
Forgot to put the classname in there I must have been tired, I even put the file extension.  I'm glad that I could help you figured it out

You can also do this

Select Case Country
  Case is = "USA"
      Set MyDLL = Createobject("USA.ClassName", "USAServer")
  Case is = "Canada"
      Set MyDLL = Createobject("Canada.ClassName", "CDNServer")
  Case is = "UK"
      Set MyDLL = Createobject("UK.ClassName", "UKServer")
End Select

If the Objects are in COM+ on the servers they can be referenced from different machines using the Server Names in the Createobject function.  They sitll have to be registered on the machine calling the DLLs but the DLLs themselves don't have to be on the machine.
0
 

Author Comment

by:MarcusW
ID: 8020633
Thanks for your help Hornet241...  Greatly Appreciated... Increased Points to 300...
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8023485
Any time, you didn't need to increase the points though.  There're generally the last thing I notice, but the it is much appreciated as an indication of the help.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question