Module Problem

Posted on 2004-11-10
Last Modified: 2010-05-02
I've made a dll which i want to Create multiple times from another application (it contains information about staff members). Any public variables that i created in the modules are kept when i create the next copy of the dll. How can i keep any public variables created for the DLL unique to that DLL and not open to any other copies i make of it?

Can the DLL be created in a separate memory space each time to stop this from happening? What is happening?

Thanks in advance

Question by:JimGSY
    LVL 44

    Expert Comment

    can you show some of the code that you are using?  How are you creating multiple instances of your class?

    can you show some of the code for the DLL, as well?

    LVL 32

    Assisted Solution

    Apartment-Model Threading in Visual Basic

    Read carefully the last section: "Reentrancy"
    LVL 10

    Accepted Solution

    You cannot safely use public variables in modules inside a COM DLL. These use shared memory even with apartment model threading. The simplest way around this is to create a class from your module and instance per instance of your dll running.

    For places you can't avoid using a module, callback dlls (addressOf), You can keep a register of public variables. I have done this with a 2D array with one column as the dll reference key. To get this key, you can instantiate an object and then use the result of objptr().
    LVL 1

    Author Comment

    That's annoing... Ok, say i created a "global" class from the module, is there then an easy way to allow any other class in the project to access that "global" class? or do i have to pass it between classes using the SET statement?

    This was created to run with an intranet, basicly any time someone logged on it created a new copy of the DLL, set which user was logged on and then stored that copy of the DLL in an ASP session. When i created the latest version of the dll (which used standard modules to store the current user,) anyone logged on suddenly found themselves changing which user they were logged on as.
    LVL 10

    Assisted Solution

    >>Ok, say i created a "global" class from the module

    Hmm that's interesting. I've never tried that. I would assume that if you have a global variable in a module pointing to an instance of a class, you will use the same instance for all instances of the dll. Therefore in effect - shared memory - the same problem. Also - you might have trouble in unloading your dll (again I'm not sure without a lot more thought and possibly some prototyping).

    To summarise:
    If you want an instance of a dll to own its own properties (variables) that it can change without affecting any other instance of that dll - this data thes properties must reside in a class. That class must must have one instance per instance of the dll. If the properties can be shared across all instances of the dll -  and when you change a property, the property will be changed across all instances, use a variable in a module, scoped at the module level.

    The same is true for ActiveX controls - and because of this, subclassing (requires callbacks to a module) can be a lot of fun.
    LVL 1

    Author Comment


    I created a class to hold all the variables in the inital class, then passed that from one class to the next using the SET statement. Shame that there is not a better way to acomplish this, however i'm sure there is a valid reason, there normally is.

    Thanks for everyone's help
    LVL 10

    Expert Comment

    There might be, but without seeing your code - I have trouble visualising exactly what you're doing.
    LVL 1

    Author Comment

    I've created a Class called "GlobalProject" Alias "pwv" this contains all the variables that i want to use accross the project. I create a new instance of this in my first class then use the SET statement to pass it to any sub-classes (into a public variable of type "GlobalProject"). Is this the way you'd do it? Infact... does that make sence?

    Public Property Get CU() As StaffDetails
        If mStaffDetails Is Nothing Then
            Set mStaffDetails = New StaffDetails
            Set mStaffDetails.pwv = pwv '<------
            If pwv.AuditOn Then pwv.AddAudit "Intra", "CU", "Created new CU StaffDetails class"
            mStaffDetails.IsCU = True
            If pwv.StaffNo <> 0 Then mStaffDetails.StaffNo = pwv.StaffNo
        End If

        Set CU = mStaffDetails
    End Property

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now