X++ run a process with user credentials

When you want to run for example a command prompt from within the X++ language, you can use the following code :
Let’s assume that the following parameters were passed to the method :

  • FileName : “C:\windows\system32\cmd.exe”
  • Arguments : “”
  • RunAsAdmin : true
  • UseShellExecute : true
  • WaitForExit : false

 

static void runWindowsProcess(  FileName    _fileName
                            ,   str         _arguments
                            ,   boolean     _runAsAdmin         = false
                            ,   boolean     _useShellExecute    = false
                            ,   boolean     _waitForExit        = false)
{
    System.Diagnostics.ProcessStartInfo processStartInfo;
    System.Diagnostics.Process          process;
    System.OperatingSystem              operatingSystem;
    System.Version                      operatingSystemVersion;
    System.Exception                    ex;
    int                                 major;
    Boolean                             start;
    ;

    try
    {
        // Assert CLRInterop permission
        new InteropPermission(InteropKind::ClrInterop).assert();

        // BP deviation documented
        processStartInfo    = new System.Diagnostics.ProcessStartInfo(_fileName, _arguments);

        // BP deviation documented
        process             = new System.Diagnostics.Process();

        // Get an instance of the System.OperatingSystem class
        operatingSystem         = System.Environment::get_OSVersion();

        // Get the OS version number
        operatingSystemVersion  = operatingSystem.get_Version();

        // If admin rights asked
        if(_runAsAdmin)
        {
            major = operatingSystemVersion.get_Major();

            // Get the major part of the version number (Starting from Windows Vista)
            if(major >= 6)
            {
                // Start using the runAs property which will prompt for administrator credentials
                processStartInfo.set_Verb('runas');
            }
        }

        // Use the system shell to execute the process
        processStartInfo.set_UseShellExecute(_useShellExecute);

        // Attach the starting information to the process
        process.set_StartInfo(processStartInfo);

        // Start the process and wait for it to complete
        start = process.Start();

        // Wait for the process to finish
        if(_waitForExit)
        {
            process.WaitForExit();
        }

        // Revert permission
        CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        // BP Deviation documented
        ex = ClrInterop::getLastException();

        while (ex != null)
        {
            error(ex.ToString());

            ex = ex.get_InnerException();
        }

        throw error(strFmt("@HEL270", _fileName));
    }
}

EventHandlers In Ax 2009

It is possible to attach event listeners to objects in X++.
The next code sample shows the usage of the SysEventBroker and SysEventInfo classes to handle events :

static void SysEventTest(Args _args)
{
SysEventBroker broker = SysEventBroker::construct();
SysEventInfo info1 = new SysEventInfo("Info-1"); // catch all;
SysEventInfo info2 = new SysEventInfo("Info-2"); // catch specific class
SysEventInfo info3 = new SysEventInfo("Info-3"); // catch specific type
;
// Setup
broker.addListener(info1, classNum(Object));
broker.addListener(info2, classNum(Runbase));
broker.addListener(info3, classNum(Object), "Fire 2"); // catch Fire 2
broker.addListener(info3, classNum(Object), "Fire 4"); // and Fire 4

broker.fireEvent(classNum(CustAutoCreate), "Fire 1", "This is my life");
broker.removeListener(info2, classNum(Runbase));
broker.fireEvent(classNum(CustAutoCreate), "Fire 2", "this is my time");
broker.fireEvent(classNum(CustAutoCreate), "Fire 3", "just show me the light");
broker.removeListener(info1);
broker.fireEvent(classNum(CustAutoCreate), "Fire 4", "and I go there");
}