Running external program

Created at 10 Oct 2023, 07:21
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MM

MMII

Joined 28.08.2023

Running external program
10 Oct 2023, 07:21


Hi

if I include the following lines in visual studio then the program will open notepad as expected

using System.Diagnostics

Process process = new Process();

process.StartInfo.Filename = “notepad.exe”;

process.Start();

But if I put the same lines of code in ctrader desktop Automate let's say inside OnStart, it does not work and gives the following error

Crashed in OnStart with Win32Exception: An error occurred trying to start process 'notepad.exe' with working directory 'C:\Users\My Username\Documents\cAlgo\Data\cBots\cbot project name'. Unknown error (0x2)

notepad.exe here is just an example to simplify the question, I intend to run scripts.

Any hint and help is greatly appreciated


@MMII
Replies

firemyst
18 Mar 2024, 01:18

I'll give you a few snippets. This is what I did and have notepad launching from cbots this way:


//you need to grant full access permissions
 [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
        //These are for showing Notepad. Put them in top of your class file
        [DllImport("user32.dll", EntryPoint = "SetWindowText")]
        private static extern int SetWindowText(IntPtr hWnd, string text);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
//to actually launch notepad from your code.
//don't put this in the ontick event or you'll crash your machine trying 
//to constantly launch notepad on every tick.
//the Process class is found in System.Diagnostics
Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
if (notepad != null)
{
    notepad.WaitForInputIdle();

    if (!string.IsNullOrEmpty(title))
        SetWindowText(notepad.MainWindowHandle, title);

    if (!string.IsNullOrEmpty(message))
    {
        IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
        SendMessage(child, 0x000C, 0, message);
    }
}

@firemyst