views:

288

answers:

3

I'm using C#.NET to launch and control a Delphi-written executable. The Delphi application has a COM interface.

I've imported the Delphi executable -- we'll call it DelphiApp.exe -- into Visual Studio as a resource.

The following C# code works -- if no other instance of DelphiApp.exe is running:

DelphiApp.DelphiAppClass da = new DelphiAppClass();
da.DoStuff(1, 3, 4);

If an instance of DelphiApp.exe is running, the above code will "take over" one of the instances, which is not what I want. When I declare a new DelphiAppClass(), I always want it to be a new application instance.

I've tried several workarounds -- like launching a new DelphiApp process if one already exists -- but I can't seem to find the proper way to handle this.

Any suggestions?

A: 

There was a question asked last week that sounds like it may be the opposite of your problem (wants to reuse the application, but keeps getting new). Maybe it could get you started down the right path?

How to reuse a Delphi ole server with a second client

Scott W
+1  A: 

When you create your delphi automation server make sure that you use ciSingleInstance for the 4th parameter in the constructor of the automaton object. For example:

TAutoObjectFactory.Create(ComServer, TTestServer, Class_TestServer,
  ciSingleInstance, tmApartment);

This will require each object have a new automation object created for it.

skamradt
+1  A: 

When you create your delphi automation server make sure that you use ciSingleInstance for the 4th parameter in the constructor of the automaton object.

I had posted this in the OP comment -- In Delphi, I am using this line of code to define the COM behavior:

TTypedComObjectFactory.Create(ComServer, TDelphiApp, Class_DelphiApp, ciSingleInstance, tmSingle);

As far as I know, this is the correct way to set things up on the COM side. (If not, please tell me what I'm doing wrong!)

I'm using Delphi 7, FWIW.