Check windows client’s execution mode at runtime

If you want to check if the windows client is run in config mode at runtime, then there is an easy method to do that:

// variables
Env : DotNet : System.Environment.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
cmdArgs : DotNet : System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

// code
cmdArgs := Env.GetCommandLineArgs;
IF LOWERCASE(FORMAT(cmdArgs.GetValue(1))) = '-configure' THEN
  // do something

Method GetCommandLineArgs gives back a string array. In the sample it’s expected, that the -configure parameter is the first parameter. To check all parameters you can do with that code:

FOR i := 0 TO cmdArgs.Length - 1 DO
  IF LOWERCASE(FORMAT(cmdArgs.GetValue(i))) = '-configure' THEN
    MESSAGE('Config mode');

There is an alternative method, also working, but much longer and with low performance. 😉

// variables
cmdLine : Text;
 
// code
cmdLine := LOWERCASE(GetCmdLine);
IF (STRPOS(cmdLine, '-configure') <> 0) OR (STRPOS(cmdLine, '/configure') <> 0) THEN
  Message('Config mode');

PROCEDURE GetCmdLine : Text;
// variables
FileMgt : Codeunit 419
CurrProc : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process"
MgmtObjSearcher : DotNet "'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.Management.ManagementObjectSearcher"
MgmtObjColl : DotNet "'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.Management.ManagementObjectCollection"
MgmtObj : DotNet "'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.Management.ManagementObject"
Enum : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.IDictionaryEnumerator"
cmdLine : Text;

// code
CurrProc := CurrProc.GetCurrentProcess;
MgmtObjSearcher := MgmtObjSearcher.ManagementObjectSearcher(
  STRSUBSTNO('SELECT CommandLine FROM Win32_Process WHERE ProcessId = %1', CurrProc.Id));

MgmtObjColl := MgmtObjSearcher.Get;
Enum := MgmtObjColl.GetEnumerator;

WHILE Enum.MoveNext DO BEGIN
  MgmtObj := Enum.Current;
  cmdLine += STRSUBSTNO(' %1', MgmtObj.Item('CommandLine'));
END;

EXIT(cmdLine);

cheers

Advertisements

2 thoughts on “Check windows client’s execution mode at runtime

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s