You'll need to download the attachment and then remove the .txt from the end of the file name. It wouldn't let me attach a .exe file here.
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace NTLogin
{
public class Program
{
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInf);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private static void ButtonClick(int x, int y)
{
SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
[STAThread]
static void Main(string[] args)
{
Process ninjaProc = null;
bool foundNinja = false;
string passwd;
string ninjaExe = @"C:\Program Files\NinjaTrader 8\bin\NinjaTrader.exe";
// Expect exactly 1 argument (password)
if (args.Length != 1)
{
return; // Silently exit if arguments are wrong
}
passwd = args[0]; // Set password from the single argument
if (string.IsNullOrEmpty(passwd))
{
return; // Silently exit if password is missing
}
RECT rctNTPosition = new RECT();
Process.Start(@ninjaExe);
while (!foundNinja)
{
Process[] processlist = Process.GetProcessesByName("NinjaTrader");
if (processlist.Length > 0)
{
ninjaProc = processlist[0];
foundNinja = true;
}
System.Threading.Thread.Sleep(100);
}
int maxRetries = 60;
int retryCount = 0;
bool windowReady = false;
while (!windowReady && retryCount < maxRetries)
{
if (ninjaProc.MainWindowHandle != IntPtr.Zero && GetWindowRect(ninjaProc.MainWindowHandle, ref rctNTPosition))
{
if (rctNTPosition.Right > rctNTPosition.Left && rctNTPosition.Bottom > rctNTPosition.Top)
{
windowReady = true;
}
}
if (!windowReady)
{
System.Threading.Thread.Sleep(500);
retryCount++;
}
}
if (!windowReady)
{
return; // Silently exit if window isn’t ready
}
SetForegroundWindow(ninjaProc.MainWindowHandle);
GetWindowRect(ninjaProc.MainWindowHandle, ref rctNTPosition);
ButtonClick(rctNTPosition.Left + 30, rctNTPosition.Top + 210);
Clipboard.SetText(passwd);
SendKeys.SendWait("^v");
SendKeys.SendWait("{ENTER}");
}
}
}


Comment