Avoid instantiating multiple vb6 class.

Hi,

I need to prevent an activex dll class to be instantiated multiple times. In fact I want it to be instantiated only one time.
Is there a way to do so?

Regards,
lxfdirsAsked:
Who is Participating?
 
aikimarkConnect With a Mentor Commented:
mutex is provided through API calls

Here are good VB6 examples:
http://krunzy.com/home/Sandbox/VB6Mutex.aspx
http://allapi.mentalis.org/apilist/CreateMutex.shtml

Since you are working at the class level, you can't use app.previnstance -- you are not an APP.
0
 
HooKooDooKuCommented:
Need a bit more detail to understand what you really need.  In the mean time, I can only suggest the following:

1. Create one instance of it at a global level (i.e. declare it public in a BAS file).
2. NEVER NEVER NEVER code VB6 like this:
DIM X as new Class1

Open in new window

When you code like that, you loose control over exactly when objects in VB6 get created and destroyed.  Instead, always code like this:
DIM X as Class1
Set X = new Class1

Open in new window

That way you can decide at what moment in time the 'new' gets executed AND when you call
Sex X = nothing

Open in new window

the object doesn't regenerate itself.
0
 
Martin LissOlder than dirtCommented:
Can you show us the code that instantiates it multiple times?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
lxfdirsAuthor Commented:
Thanks for your suggestions.
Now, my exe build on VB .NET is instantiating the dll class interface using

    Private WithEvents x As COM.COMInterface
    x = New COM.COMInterface

If I run a second or third or ... copies of my exe it will create I believe a new object of the same class in the system. What I need is to prevent not from the exe, but from the class interface itself that a new object is created and as a result prevents the exe to come up. I mean does not matter how many times I want to instantiate the class it will allow only one copy of itself in the system at all times.

Regards
0
 
Martin LissOlder than dirtCommented:
Thanks for your suggestions.
Now, my exe build on VB .NET is instantiating the dll class interface...

Your original question Title mentions VB6. Has that been answered? Are you now asking a second question?
0
 
lxfdirsAuthor Commented:
No, it is that same question. My dll was build with vb6, but my exe which instantiates the vb6 developed class is built with VB .NET.

Regards,
0
 
aikimarkCommented:
You can create a Mutex to prevent multiple instances of programs and object in a system.  When you use App.Previnstance, it is checking the existence of the mutex created when the application started.
0
 
lxfdirsAuthor Commented:
Hi, aikimark:

Please, expand on mutex. New for me. Could this be developed in the activex dll class interface?

Do this means that it will reject the instantiation returning a kind of a nothing or null or after checking with app.previnstance I will have to do logic. I can not use an End statement in a dll to terminate it. In this last case is there a way to return a nothing or null to the caller?

I have used app.previnstance before on the dll interface, buf did not work. Probably because I have not implemented the mutex.

Regards,
0
 
HooKooDooKuCommented:
I'm not sure where a mutex is going to help.  Typically, a mutex is used to prevent multiple threads from accessing the same resource at the same time.  But I'm assuming the question here is how to prevent muliple instance of this dll from loading, yet each time the dll is needed to get a reference to the one that has been created.

Now I don't have ANY experience in dealling the VB.net accessing things built in VB6.  But if your project was strictly a VB6 project, a class has a property where it can be declared 'global multiuse".  Don't recall what was the exact name of the property, but classes have few enough properties you should find it easily.

From what I recall, when you set a class to global-multi-use, when the vb6 application starts, you will already have an instance of this class and everything in the application can access this one instance of the class without calling 'new' on it.

Not sure if it will help building the VB6 dll to call from .net, but look into the VB6 side and see if this global multi-use setting will work for you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.