Start a desktop app from commandline with some arguments

hi,

Would it be possible to start a desktop app from within a consoleapp and pass some variables over to the desktop side?

I have a third party server app that can run external Xojo-consoleApps when certain events happen.
Those consoleapps get passed some arguments that need to used by a Xojo-desktop solution in the end.

Yes, this is possible. Implement the App.OpenDocument event handler and add code like this:

Dim params As String = item.Name break

Then you can invoke the app with parameters (When debugging, use RUN PAUSED in the IDE, and then run the executable from the command line, like this:

/private/var/folders/b0/jnshwx0x6x35tvz9ntwlnh8h0000gn/T/TemporaryItems/arguments.debug.app/Contents/MacOS/arguments.debug arg1=this,arg2=that

You can launch an app via Shell class.

In MBS Xojo Plugins we have Application.ArgumentsMBS as String() to give you the process parameters for console and desktop.
So you can even have a desktop app launched by command line, read parameters, do work and quit before opening a window.

We also have ConsoleExecuteMBS to run apps, which sometimes works better in background.

If you just want a Windows solution (Christians is nice as its cross platform), this will give you an array of strings, the first being the exe name, the rest being the args passed to the exe, call it where ever you need it:

[code]Declare Function GetCommandLineW Lib “Kernel32.dll” () As WString
Declare Function CommandLineToArgvW Lib “Shell32.dll” (lpCmdLine As WString, ByRef pNumArgs As Int32) As Ptr

Dim p As Ptr 'we need the ptr to work out offsets of things
Dim m As MemoryBlock
Dim numberOfParams As Int32

p = CommandLineToArgvW(GetCommandLineW(), numberOfParams)
m = p

Dim args() As String

For c As Integer = 0 To numberOfParams - 1
Dim offset As Integer = m.UInt64Value(c * 4) - p.Integer(0) 'calculate the offset of the string from the start of the memoryblock
Dim s As String = m.WString(offset + ((numberOfParams + 1) * 4)) 'grab the string
args.Append(s)
Next[/code]

sytstem.commandline will have the parameters

I use something like

iF InStr(System.CommandLine, "^") >0 then commandline()=split(System.CommandLine,"^") poll_freq=commandline(1) end if

thank u all for the great advice :slight_smile:

I ended up sending some sort of ‘notifications’ with IPCsocket to the desktop app (now called ‘Notification Center’) and then process them according to some rules I have set up.
After I found out about IPCsocket it just felt right for my type of application.