Run standalone application from a SWP macro

Hi everyone,

I would like to run a standalone C# application from a VBA SWP launcher by passing the SolidWorks process id as argument in case of multiple SolidWorks instances.

macro.swp (VBA launcher)

---------------------------------------------------------------------------------------------------------

Sub Main()<br>    Shell """C:\\...\\macro.exe"" " + CStr(Application.SldWorks.GetProcessID)<br>End Sub

macro.exe (C# standalone application)

---------------------------------------------------------------------------------------------------------

static void Main(string[] args)<br> {<br>    int processId = Convert.ToInt32(args[0]);<br>    Process process = Process.GetProcessById(processId);<br>    // How to get SldWorks instance from process?<br>}

Need help

Best regards

SolidworksApi macros
Answers 7
ST
ST 2015-07-03 Best Answer

I think the AccessibleObjectFromWindow API might be what you are looking for.

AG
AG 2015-07-03

Thank you for your response,

I searched how to implement it correctly but all my tries has failed.

Can I ask you to develop your response if you know it please?

Best regards

ST
ST 2015-07-03

I have never used it myself, but I saw this on Google:

How to Use AccessibleObjectFromWindow API in VBA to Get Excel Application Object from Excel Instance Window Handle

I think it should be possible to adapt that for SolidWorks.

IK
IK 2015-07-05

you can use shellexecute ​https://support.microsoft.com/en-us/kb/238245

AG
AG 2015-07-06

Thank you for your help Ivana but I think you did not understand my question.

I am trying to run a standalone application with differents sessions of SolidWorks opened.

To target the good session, I use a VBA SWP launcher which send the Process ID to my standalone application.

So I am able to get the good process and to target the good document window by testing it with:

MessageBox.Show(process.MainWindowTitle);

But I want to find the instance of SolidWorks Document now.

I think the Simon's answer is right but all my tests failed.

Maybe there is another way?

ST
ST 2015-07-06

I don't use C# I'm afraid, but I do remember doing something similar in C++ a few  years ago.

I used GetRunningObjectTable and EnumRunning.

Googling those lead me to this page:

Accessing COM applications from the Running Object Table - AutoCAD DevBlog

AG
AG 2015-07-07

Thank you very much Simon for your help.

I finally found the solution here:

You were on the right way, so I consider your answer as correct.

Best regards.