Solved

Convert VB6 ActiveX exe to C#

Posted on 2004-08-25
14
967 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
ID: 11898615
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
ID: 11901437
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
ID: 11917548
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 5

Expert Comment

by:Kelmen
ID: 12005077
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
ID: 12011809
[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
ID: 12019264
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
 
LVL 2

Author Comment

by:afpcos
ID: 12021458
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
ID: 12042032
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
ID: 12046610
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
ID: 12050830
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
ID: 12279152
Some good answers have been given. Deletion is not appropriate.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 12313140
PAQed - no points refunded (of 500)

GhostMod
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 64
Finally almost got it 5 48
"Emulate" TAB key when press Enter Key 3 46
C# Linq Select From List 3 10
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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