[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Avoid instantiating multiple vb6 class.

Posted on 2012-08-31
9
Medium Priority
?
403 Views
Last Modified: 2012-12-09
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,
0
Comment
Question by:lxfdirs
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 38354054
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
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38354742
Can you show us the code that instantiates it multiple times?
0
 

Author Comment

by:lxfdirs
ID: 38357115
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
Industry Leaders: 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 50

Expert Comment

by:Martin Liss
ID: 38357238
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
 

Author Comment

by:lxfdirs
ID: 38357383
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
 
LVL 46

Expert Comment

by:aikimark
ID: 38357987
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
 

Author Comment

by:lxfdirs
ID: 38358756
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
 
LVL 46

Accepted Solution

by:
aikimark earned 1000 total points
ID: 38358973
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
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 38358990
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…
Suggested Courses

872 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