Results 1 to 9 of 9

Thread: Declaring VB's ActiveX DLL at Runtime - Urgent ??

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240

    Question

    Hi,
    If I created a ActiveX DLL in VB how can I load it at runtine without refrencing it.


    example:
    Code:
     
    Public Declare Function MyHello Lib "PLUGIN.dll" Alias "Hello" (ByVal Name As String) As Long
    What would the code in the ActiveX DLL and the program look like.

    Please
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Posts
    87
    Placing a reference to a dll when designing your project is called early binding. What you are after is late binding which you can do with CreateObject().

    Dim objSomething as object

    'Create the object based on the project name and
    'class name.
    Set objSomething = CreateObject("MyDll.MyObject")

    'Call a method
    objSomething.DoSomething

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240
    If I knew the path and filename of the DLL file but the DLL file was not registered, how could I create an Object for it.

    For example:
    In VB if you want to Refrence a DLL file. You could simple go to the Refrences Option and select from the list of already refrenced Objects of you could browse for the file.

    How could I write a code that retrieves the information from a DLL file that it can you when you want to use the CreatOBject()

    keep in mind that I know the path and filename of the DLL file, I just need to get the information from the DLL file so that I can use the CreateObject Function.

    Example

    dll_filepath = "c:\mydll.dll"

    If I know the path and file name, how do I retrieve the information from the file in order to create an object.


    Thanks.


    [Edited by omarswan on 06-02-2000 at 04:42 AM]
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    This will populate a list with the classes present in an ActiveX DLL

    The class names need to have been included in the resouce file for it to work

    Code:
    Option Explicit
    Private Declare Function LoadString Lib "user32" Alias "LoadStringA" (ByVal hInstance As Long, ByVal wID As Long, ByVal lpBuffer As String, ByVal nBufferMax As Long) As Long
    Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
    
    
    
    Sub FillClassNameList(FileName As String)
        ' Get the list of classnames contined with a component from the components
        ' resource data.
        
        ' Set the error handler
        'On Error GoTo GetClassNameListErr
        Dim ModuleHandle As Long
        Dim ClassNames() As String
        ModuleHandle = LoadLibrary(FileName)
    
    
        ' Start by getting the number of classnames
        Dim StringLength As Integer
        Dim Buffer As String
        Dim Length As Long
        Dim NumClassNames As Long
        Length = 255
        Buffer = String(Length, vbNullChar)
        StringLength = LoadString(ModuleHandle, 2, Buffer, Length)
        NumClassNames = Val(Left(Buffer, StringLength))
    
        ' Create the storage space
        ReDim ClassNames(NumClassNames)
    
        ' Loop thorugh all the class names adding them to the array
        Dim Counter As Long
        For Counter = 0 To NumClassNames - 1
          Buffer = String(Length, vbNullChar)
          StringLength = LoadString(ModuleHandle, Counter + 3, Buffer, Length)
          'ClassNames(Counter) = Left(Buffer, StringLength)
          List1.AddItem Left(Buffer, StringLength)
        Next Counter
    
        
        ' Free the module
        FreeLibrary ModuleHandle
    
      End Sub
    Mark
    -------------------

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240

    Unhappy It's not working Mark

    Hi,
    Mark thanks for the code but it didn't work when I tried it. What coul I be dooing wrong?
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

  6. #6
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    omarswan

    In what way didn't it work?



    Mark
    -------------------

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240

    Hi Mark, This Is What I Did

    Hi Mark,
    this what I did.
    1) I created a new exe Project with form1
    2) I added a listbox

    then...

    Code:
    Option Explicit
    Private Declare Function LoadString Lib "user32" Alias "LoadStringA" (ByVal hInstance As Long, ByVal wID As Long, ByVal lpBuffer As String, ByVal nBufferMax As Long) As Long
    Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
    Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
    
    
    
    Sub FillClassNameList(FileName As String)
       ' Get the list of classnames contined with a component from the components
       ' resource data.
    
       ' Set the error handler
       'On Error GoTo GetClassNameListErr
       Dim ModuleHandle As Long
       Dim ClassNames() As String
       ModuleHandle = LoadLibrary(FileName)
    
    
       ' Start by getting the number of classnames
       Dim StringLength As Integer
       Dim Buffer As String
       Dim Length As Long
       Dim NumClassNames As Long
       Length = 255
       Buffer = String(Length, vbNullChar)
       StringLength = LoadString(ModuleHandle, 2, Buffer, Length)
       NumClassNames = Val(Left(Buffer, StringLength))
    
       ' Create the storage space
       ReDim ClassNames(NumClassNames)
    
       ' Loop thorugh all the class names adding them to the array
       Dim Counter As Long
       For Counter = 0 To NumClassNames - 1
          Buffer = String(Length, vbNullChar)
          StringLength = LoadString(ModuleHandle, Counter + 3, Buffer, Length)
          'ClassNames(Counter) = Left(Buffer, StringLength)
          List1.AddItem Left(Buffer, StringLength)
       Next Counter
    
    
       ' Free the module
       FreeLibrary ModuleHandle
    
    End Sub
    
    Private Sub Form_Load()
      Call FillClassNameList("C:\Omar\MyTemp\vbSendMail.dll")
    End Sub
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

  8. #8
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    The code was taken from a project which consists of lots of small DLLs.

    Each DLL has a resource file containing the class names


    here's one of the resource file source-files

    It was designed by another programmer but it works fine


    resource.rc
    Code:
    // The string table
    //
    // The first entry (number 1) is the file name of the component.
    // The second entry (number 2) is the number of class names in the list
    // The next set of entries (starting at 3) is a list of all the class
    // names in the component
    // The remaining entries are application specific
    //
    // The Components installer looks at the first entries so it can get
    // the names of the classes inside the component.
    //
    STRINGTABLE LOADONCALL MOVEABLE DISCARDABLE 
    BEGIN
    	1. "INVOIC Formatter.DLL"
    	2, "1"
    	3, "INVOIC2.Format"
    END
    using the rc.exe resource compiler you create a .RES file which you then include in your project.

    Is this any help to you or do you need a more generic Look-inside-a-dll type of thing? if so repost your original question and I'll just mind my own business.

    Mark
    -------------------

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    You could register the dll first.
    For this example I created a dll named testproject1.dll containing a class named testclass1.
    Code:
    Option Explicit
    Private Declare Function DllRegisterServer Lib "c:\temp\testproject1.dll" () As Long
    
    Private Sub Command1_Click()
    Dim retVal As Long
    Dim myClass As Object
        retVal = DllRegisterServer
        Set myClass = CreateObject("testproject1.testclass1")
    End Sub

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