Solved

Convert VB6 ActiveX exe to C#

Posted on 2004-08-25
14
963 Views
Last Modified: 2012-06-21
I am a VB6 programmer.  I had several VB6 programs that run on users computers which needed to use a database connection.  With over 300 users, this was not reasonable to have each program that each user started create a new connection.

So....

I created an ActiveX exe that would only let one instance of itself start and it created and held the ado connection object.  Any program the user would start would get the same reference to the connection ActiveX exe.

I need to recreate this in C#.  I would also like to convert a VB6 project that is basically a DAO pattern implementation that programs can use to get objects from.

How can I recreate the ActiveX exe environment in C# where I can pass references to public classes from out of process programs?

Can anyone help???

Thanks!!
0
Comment
Question by:afpcos
  • 4
  • 3
  • 2
  • +3
14 Comments
 
LVL 3

Expert Comment

by:gillgates
Comment Utility
You shouldn't need to recreate it.  Try just adding your existing activex component to your C# project and using it.
0
 
LVL 2

Author Comment

by:afpcos
Comment Utility
I have to recreate it.  My boss wants everything converted to C# so we can move off of VB6.
0
 
LVL 6

Expert Comment

by:DominicCronin
Comment Utility
I think what you need is the pattern usually referred to as singleton. The idea is that you can't "new" an instance of it; rather a static member is used to provide you with the means of getting the single instance. If you ask for an instance a second time, you get the same one. Here's some code to illustrate the point.

      sealed class ThereCanBeOnlyOne
      {
            private ThereCanBeOnlyOne() { }
            // Initialising this is the only time the object is ever created.
            public static readonly ThereCanBeOnlyOne ItsThisOne = new ThereCanBeOnlyOne();
            public void DoOneishStuff()
            {
                  System.Console.WriteLine("I am one! Hash:" + this.GetHashCode());
            }
      }

      class Test
      {

            static void Main()
            {
                  ThereCanBeOnlyOne one = ThereCanBeOnlyOne.ItsThisOne;
                  ThereCanBeOnlyOne two = ThereCanBeOnlyOne.ItsThisOne;

                  one.DoOneishStuff();
                  two.DoOneishStuff();

                  if (one == two)
                  {
                        System.Console.WriteLine("one == two");
                  }
                  else
                  {
                        System.Console.WriteLine("one != two");
                  }

                  return;
            }
      }

You can easily imagine how a singleton class like this could be used as a broker to hand out and manage connections etc. On the other hand, you should probably also look at ADO.NET and similar technologies, as there are connection pooling implementations that you can just use instead of having to write them yourself.
0
 
LVL 5

Expert Comment

by:Kelmen
Comment Utility
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp

refer the sample

I also attempted the VB ActiveX EXE option in C#, but turns out .NET is different beast. In short, there's no direct converting of this. You probably need to rewrite the portion of this component.

As you mentioned you worked out that ActiveXExe to cater multi-clients, you may as well worked out either a Web Service, or ASP.NET.

My last work turned out a C# COM+ (ServicedComponent) and expose to COM for VB client to call, using the Singleton approach.

PS. done a stress test later, using native VB COM is much faster than .NET COM. Unless we going to revamp the whole sys using .NET.
0
 
LVL 4

Expert Comment

by:thedude112286
Comment Utility
[ClassInterface(ClassInterfaceType.AutoDual)] // the key to exposing a C# app as a ActiveX object
public class Singleton {
      public static readonly Instance = new Singleton();

      //put all of your other methods, etc down here
}

Good info can be found at http://www.devhood.com/messages/message_view-2.aspx?thread_id=16569.
0
 
LVL 6

Expert Comment

by:DominicCronin
Comment Utility
thedude - the point of the singleton is that the only way to instantiate one is to ask singleton to do it. For that your constructor should be private. Kelman's pointer takes you to an interesting article which details a few variations on the theme. My understanding from apfcos's question was that he *doesn't* need to interop between VB6 and dotnet. Otherwise, as you point out Kelmen, your performance will depend a lot on how you implement your interop.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:afpcos
Comment Utility
Sorry if I have not been very active on this question.  I am dealing with hurricanes in florida.

I do not need to interop between VB6 and dotnet.  I completely understand the singleton pattern.  However, from what I understand of Dlls that will not help me.  I understand that DLLs are an "in process" program. If I have two seperate programs that reference the same DLL and run at the same time using the singleton pattern, they will each still get a new instance because of the inprocess issue. ???
0
 
LVL 5

Expert Comment

by:Kelmen
Comment Utility
The singleton is a private static class.

I developed a COM+ (ServicedComponent, also exposed to VB through COM) that utilize this singleton.
The COM+ is server-actived, enable object-pooling, min = 1. Max >= 1

This idea is from one of my colleague, and I found some posting suggesting this approach (from some MVP, dunno the URL liao, somewhere from Google newsgroup).

Theoritically + initially, I don't believe it work. As after the COM+ back to pooling, I assumed the singletion will ceased as well. However, when testing, it worked as expected. I dunno the inner trick of it.

The singleton only ceased when the COM+ app/package is shutdown, idle-timeout
HTH
0
 
LVL 6

Expert Comment

by:DominicCronin
Comment Utility
Of course, a COM+ app/package is just a process (assuming it's registered for server rather than library). How about something that will keep a reference at a system level?   Could you run a service simply to host the singleton?
0
 
LVL 5

Expert Comment

by:Kelmen
Comment Utility
I "guess" the COM+ pooling is the host here. As the private would ceased only if the host/caller (the COM+ component) been GC. Since its in the pool, its not yet queue into the GC, hence the singleton hosting. By using server-activated (performance BIG penalty), there's no concern of in-process, as it only run in out-of-process.

Similar as with the ActiveX EXE in a way. (Frankly, VB ActiveX EXE is run in STA, but the stuff we got is probably in MTA, so far so good...)

Since you mentioned no need Interlop, VB6 overhaul -> C#, may as well redesign the sys, using remoting (can tune as singleton) / web service, which probably can provide the feature as you need.

PS: We adopt the web service at 1st development, but as we need the Interlop, and to utilize ADO + performance, we go for the trick. ADO Recordset can't be serialize unless we work up the xml, stream, and/or serialization stuff, which hinder our timeline and  performance.
0
 
LVL 6

Expert Comment

by:DominicCronin
Comment Utility
Some good answers have been given. Deletion is not appropriate.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
Comment Utility
PAQed - no points refunded (of 500)

GhostMod
Community Support Moderator
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

10 Experts available now in Live!

Get 1:1 Help Now