Passing command line params to running application

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
DavOlsson
Posts: 12
Last visit: Mon Jan 22, 2024 6:20 am

Passing command line params to running application

Post by DavOlsson »

Hello!

Im trying to pass command line parameters to an already running GUI application.
Trying to figure out if there is a good way to accomplish this and if one exist, how will I go about to do it?

For example if use a textbox in my GUI application, and I would like to run something like "application.exe -textbox message" and it will add the message to the textbox without open a new instance if the application.

Another example is how in skype for business/lync you are able to use "lync.exe sip:something@domain.com" in order to launch a new chat win someone.

The solution I'm thinking about is to create a separate application which create/update a JSON/XML or textfile which is later on fetched by the GUI application. But that would not be so elegant to constantly check if that file has been updated. Is there another way?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing command line params to running application

Post by jvierra »

This is not possible in PowerShell. Programs that can be remotely automated are COM servers. PowerShell cannot be used as a COM server.
User avatar
DavOlsson
Posts: 12
Last visit: Mon Jan 22, 2024 6:20 am

Re: Passing command line params to running application

Post by DavOlsson »

jvierra wrote: Mon Aug 21, 2017 2:17 am This is not possible in PowerShell. Programs that can be remotely automated are COM servers. PowerShell cannot be used as a COM server.
Ah I see, interesting. Thanks for the info! :)
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Passing command line params to running application

Post by Alexander Riedel »

Generally this is done by finding the Window of the already running instance. When one exists, send it a message. I am not sure if that could be done in PowerShell.
You would have to insert some C# code I presume.
https://stackoverflow.com/questions/121 ... to-another
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing command line params to running application

Post by jvierra »

Actually there is a way to do this in PowerShell for programs that have messages defined such as the old DDE mechanism. PowerShell, however, is a console application and only defines specific messages.

A PowerShell script could use MSMQ to listen for messages and then act on the or it could monitor a file for commands. We can also design a COM class in C# or other Net language and use that as a go between.

My above point is that there is no built-in mechanism in PowerShell that allows the behavior seen in Skype for Business.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Passing command line params to running application

Post by Alexander Riedel »

Jim, he posted about a GUI application. So there is a window. Posting simple messages should work with some C# code.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing command line params to running application

Post by jvierra »

Unfortunately there is only one message loop and is is owned by the console application. The console app will not know what to do with custom messages.

I have tried for years to find a way to defer events from the console to a form and have not found a way to do this. Events originating in a form will be sent to the form. Events received by the PowerShell process or its Window will not be sent to the form. If we could do that we could have jobs events caught in the form which would allow better management of jobs through events rather than through polling.

I have constantly though about how this might be done and, so far, no solution. Any ideas?
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Passing command line params to running application

Post by Alexander Riedel »

My assumption was that he packages the GUI application as an exe. So there is no console.
I will see what I can come up with. Maybe we can even bake something into the host itself.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing command line params to running application

Post by jvierra »

A packaged EXE still has only one thread and its message loop is pre-empted by "ShowDialog". Best I can remember the "ShowDialog" loop is a filtered loop which discards all messages that are not allowed by "modal" dialogs. System.Windows.Forms.Form has a "MessageLoop" substitution property but I believe it cannot be used with modal dialogs.

See: https://msdn.microsoft.com/en-us/librar ... .110).aspx

We can use the debug API to "hook" the process message queue but this would require elevation which is not helpful.
This topic is 6 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked