Results 1 to 6 of 6

Thread: [RESOLVED] DLL + VB6 + Global variables + ByRef

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    31

    Resolved [RESOLVED] DLL + VB6 + Global variables + ByRef

    Hi everyone,

    I have declared some global variables in one of my class module of my DLL ActiveX VB6 project.

    I can access those global variables in a client VB6 project referencing the DLL but the "ByRef" mechanism does not work. I mean that a global variable from the DLL passed by reference in a function of the client VB6 project does not keep value affected in the function after its execution.

    Could someone help me please ?

  2. #2
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: DLL + VB6 + Global variables + ByRef

    Sure. Post your code.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: DLL + VB6 + Global variables + ByRef

    First of all, you cannot have a 'Global' variable in a class module. If you meant
    a Public variable in a class module, it is the same as a Public Property, without
    any error checking. In other words, in a class module:
    Code:
    Public Foo As Long
    is the same as:
    Code:
    Private mFoo As Long
    Public Property Get Foo() As Long
     Foo = mFoo
    End Property
    Public Property Let Foo(NewVal As Long)
     mFoo = NewVal
    End Property
    Although VB allows Global variables in a Bas Module, it is the same as Public.

    Perhaps you could show some code in the relevant modules.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    31

    Re: DLL + VB6 + Global variables + ByRef

    Ok.

    My DLL ActiveX VB6 project is made up of class modules including a class module containing only global variables. Here is a declaration of one of those global variables :

    Code:
    Public glngSociete              As Long
    For information, the class module Instancing property is GlobalMultiUse.

    My Exe standard VB6 project references the DLL created from the DLL ActiveX VB6 project. My Exe standard VB6 project contains forms, modules and class modules. In one module, I call a function with the global variable mentionned above as a "ByRef" parameter. Here is the call of the function :

    Code:
    intResult = Decryptage_Cle_Validation(strCleValid, _
                                              glngNumeroLicence, _
                                              glngNumeroPilote, _
                                              dtmDateValid, _
                                              dtmDateValidTemp, _
                                              gintConnexionsMaxi, _
                                              gblnLicenceEducation, _
                                              gblnMaintenance, _
                                              glngSociete, _
                                              tblnOptions(), _
                                              lngOptions, _
                                              lngCleControle)
    Here is the prototype of Decryptage_Cle_Validation :

    Code:
    Public Function Decryptage_Cle_Validation(ByVal vstrCleValid As String, _
                                              ByRef lngLicence As Long, _
                                              ByRef lngPilote As Long, _
                                              ByRef dtmDateValid As Date, _
                                              ByRef dtmDateValidTemp As Date, _
                                              ByRef intConnexions As Integer, _
                                              ByRef blnEducation As Boolean, _
                                              ByRef blnMaintenance As Boolean, _
                                              ByRef lngSociete As Long, _
                                              ByRef tblnOptions() As Boolean, _
                                              ByRef lngOptions As Long, _
                                              ByRef lngCleControle As Long) As Integer
    The parameter lngSociete matches with the global variable glngSociete. In Decryptage_Cle_Validation, I assign a value to lngSociete as following :

    Code:
    lngSociete = lngValeur
    Let consider that glngSociete is equal to 5 before the call to the Decryptage_Cle_Validation function and we assign lngSociete to 10 (i.e. the value contained in lngValeur) then glngSociete is still equal to 5 when the execution goes back to the calling function.

    Before I created the DLL, the global variable glngSociete was present in the Exe standard VB6 project and there was no problem.

    Thanks in advance for your help.

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: DLL + VB6 + Global variables + ByRef

    Sounds like you have two variables called glngSociete. One in the exe and one in the dll. I bet you will solve your problem if you change Public glngSociete to a property.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: DLL + VB6 + Global variables + ByRef

    Before I created the DLL, the global variable glngSociete was present in the Exe standard VB6 project and there was no problem.
    Present where? In a module, not a class? Correct?

    As mentioned by VBClassicRocks, public variables in classes are actually properties. When code runs, VB creates a Public Let & Public Get statement, behind the scenes, for those variables. Understanding that, when you pass the variable to a function, you are passing it ByVal in reality. If you want the values to change, pass the class to your function, then within the function, use/update the class public variable.

    If the class you created has its instancing property set to GlobalMultiUse then don't pass the class or public variables at all. Just use them, directly, within the function. GlobalMultiUse classes are public to the entire project.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Tags for this Thread

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