How to find the SAP Business One Process Id

Hi guys,

I’ve always struggled with finding the correct process id of the SAP Business One a given add-on connects with.

This becomes a necessity when displaying window dialogs, or when we need to grab window handles from SAP Business One client (for whatever reason).

Many solutions involve using the OS API which has been unreliable in my experience and would fail when running our addon from a development environment such as Visual Studio.

For that reason, I’ve set out to find a way to find the process id our add-on connects to.

I’ve found a way which I can share with you because it has been, so far, pretty accurate.

Here is the code:

var gui = new SboGuiApi();
gui.Connect(ConnectionString);
var application = gui.GetApplication();

var appId = application.AppId;
var processes = System.Diagnostics.Process.GetProcessesByName(@"SAP Business One");

foreach (var process in processes)
{
    try
    {
        var processAppId = gui.GetAppIdFromProcessId(process.Id);
        if(appId == processAppId)
        {
            return process.Id;
        }
    }
    catch (COMException)
    {
        // GetAppIdFromProcessId will throw when the current process is not the one we are connected to
        continue;
    }
}

The code connects to SAP Business One and extracts, the appId which is the specific id of the client you connect to.

Then it asks for all processes named SAP Business One and iterates over them. It then uses the GetAppIdFromProcessId to extract the appId of that process. If the extracted appId matches our current appId then this is the process we connect to, and it returns the process id.

This way we will reliably get the process id our add-on connects to. I’ve managed to test this in single machine environments and terminal services environments.

If anyone can leave comments on Citrix environments that would be great.

I hope you’ll find this useful.

Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *