1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
public class Program : ApplicationContext
{
#region déclaration Message
public const int WM_DDE_REQUEST = 0x3E6;
#endregion
[DllImport("user32.dll")]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
private string[] Arguments;
private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// si elle a quelque chose à dire
if (Arguments.Length != 0)
this.Send_Message_From_ThirdApp(Arguments);
else
this.Send_Message_From_ThirdApp();
Thread.Sleep(2000);
try
{
Application.ExitThread();
Application.Exit();
}
catch { ;}
}
private void Send_Message_From_ThirdApp()
{
IntPtr handle = IntPtr.Zero;
Process[] p = Process.GetProcessesByName("MonAppli");
if (p.Length != 0)
{
handle = p[0].MainWindowHandle;
}
SendMessage(handle, (uint)Program.WM_DDE_REQUEST, 0, 0);
}
private void Send_Message_From_ThirdApp(string[] args)
{
IntPtr handle = IntPtr.Zero;
int par1 = 0;
int par2 = 0;
Process[] p = Process.GetProcessesByName("MonAppli");
if (p.Length != 0)
{
handle = p[0].MainWindowHandle;
}
if (args.Length > 0)
{
par1 = 1;
if (args.Length == 2)
par2 = 1;
else if (args.Length > 2)
{
MessageBox.Show("Trop de paramètres pour lancer l'application", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
SendMessage(handle, (uint)Program.WM_DDE_REQUEST, par1, par2);
}
public Program(string[] args)
{
Arguments = args;
try
{
Process[] p = Process.GetProcessesByName("MonAppli");
if (p.Length != 0)
{
if (args.Length != 0)
this.Send_Message_From_ThirdApp(args);
else
this.Send_Message_From_ThirdApp();
try
{
Application.ExitThread();
Application.Exit();
}
catch { ;}
}
else
{
this.Arguments = args;
// sinon on la lance
ProcessStartInfo myInfo = new System.Diagnostics.ProcessStartInfo("MonAppli.exe");
myInfo.WorkingDirectory = @"C:\Program Files\MonAppli.exe";
System.Diagnostics.Process.Start(myInfo);
// on temporise un peu pour que la boucle de traitement du GetMessage dans l'application destinataire soit lancée
System.Timers.Timer t = new System.Timers.Timer();
t.AutoReset = true;
t.Interval = 5000; // 5 secondes
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Start();
// on n'enverra le message qu'à échéance
}
}
catch (Exception e)
{
MessageBox.Show("Lancement de MonAppli impossible !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
try
{
Application.ExitThread();
Application.Exit();
}
catch { ;}
}
}
/// <summary>
/// Point d'entrée principal de l'application.
/// si 1er argument non vide alors wParam = 1, sinon = 0
/// si 2eme argument non vide alors wParam = 1, sinon = 0
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Program prog = new Program(args);
Application.Run(prog);
}
catch { ;}
}
} |
Partager