Obtaining the Broker.Application object in C#

I'm moving from VB.NET to C#, but I need an equivalent for C# and Late Binding for:

AB = CreateObject("Broker.Application")

How can I obtain the AB object in C#? I've searched everywhere and can't find a solution.


Type BrokerExe = Type.GetTypeFromProgID("Broker.Application");
dynamic AmiBroker = Activator.CreateInstance(BrokerExe);

This is what I was looking for. Thank you!

Problem here.

In vb.net, the AB object created by CreateObject will nicely get destroyed when I exit the app. But in C#, the dynamic AB "Instance" does not get destroyed when exiting the app.

So if I run AmiBroker, the Broker.exe will normally shows up in Task Manager. Then I run my C# app, then exit. Then if I exit AmiBroker, Broker.exe doesn't shut down. This means that the AB instance that I created in my C# app is still attached to Broker.exe.

Is there any way I can destroy the AB instance before I exit the app? If I Kill() the process, it will also shut down AmiBroker. Not what I want.

I may have to go back to vb.net.

@LouB did you try to set the AmiBroker instance to null before closing your app?

Yes I set the AB instance to null.

AB = null;

@Lou see this StackOverflow suggestion.

I tried it in an old example I had (using the assembly Broker.dll generated via tlbimp.exe using the Broker5505.tlb), and it seems to work properly.

Do NOT set AB = null; in the try section, in order to always execute the finally code to release the COM object (in your case "target" is AB).

1 Like

Simple - if your code calls CreateInstance, it must call Release at the end.

Here is the clean solution when programming in C# with Visual Studio,

using System.Runtime.InteropServices;

// To open AmiBroker object

Type BrokerExe = Type.GetTypeFromProgID("Broker.Application");
dynamic AmiBroker = Activator.CreateInstance(BrokerExe);

// To close AmiBroker object

AmiBroker = null;

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.