Executable started from the IDE
Posted: Tue Mar 07, 2017 1:58 pm
For me one of the important functions in Visual Objects is
I'm using this for special debugging output or other specific behavior.
Unfortunately this is not so easy in X# as there is no special executable when started from XIDE or Visual Studio.
The solution is to check whether the executable was started from XIDE or devenv (Visual Studio's executable).
This is the code (and you'll find a complete XIDE export attached to this message):
Code: Select all
IsStartedFromIDE()
Unfortunately this is not so easy in X# as there is no special executable when started from XIDE or Visual Studio.
The solution is to check whether the executable was started from XIDE or devenv (Visual Studio's executable).
This is the code (and you'll find a complete XIDE export attached to this message):
Code: Select all
// Source (C#):
// http://stackoverflow.com/questions/2531837/how-can-i-get-the-pid-of-the-parent-process-of-my-application
// translated to X# and adapted by Wolfgang Riedmann wolfgang@riedmann.it
using System
using System.Diagnostics
using System.Management
function Start( ) as void
local oProcess as Process
local oParent as Process
local nId as int
oProcess := Process.GetCurrentProcess()
nId := oProcess:Id
oParent := ProcessUtil.GetParentProcess( nId )
if oParent != null
if oParent:ProcessName:Tolower() == "cmd"
oParent := ProcessUtil.GetParentProcess( oParent:Id )
endif
if oParent:ProcessName:ToLower() == "xide"
System.Console.WriteLine( String.Format( "Executable was launched from development environment {0}", oParent:ProcessName ) )
else
System.Console.WriteLine( String.Format( "Executable was launched by {0}", oParent:ProcessName ) )
endif
else
System.Console.WriteLine( "No parent process!" )
endif
return
static class ProcessUtil
static method GetParentProcess( nProcessId as int ) as Process
local oParent as Process
local nParentId as dword
local cQuery as string
local oSearch as ManagementObjectSearcher
local oResults as ManagementObjectCollection
local oEnumerator as ManagementObjectCollection.ManagementObjectEnumerator
local oQueryObj as ManagementBaseObject
oParent := null
try
cQuery := String.Format( "SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", nProcessId )
oSearch := ManagementObjectSearcher{ "rootCIMV2", cQuery }
oResults := oSearch:Get()
oEnumerator := oResults:GetEnumerator()
oEnumerator:MoveNext()
oQueryObj := oEnumerator:Current
nParentId := ( dword ) oQueryObj["ParentProcessId"]
if nParentId > 0
oParent := Process.GetProcessById( int( nParentId ) )
endif
catch oEx as Exception
Debug.WriteLine( String.Format( e"Error occurred: {0} n{1}", oEx:Message, oEx:StackTrace ) )
end try
return oParent
end class