Situation:
My Application is designed to be SingleInstance -> Working
My Application is deployed via ClickOnce - > Working
New request is, that you can pass arguments to this Application. This is currently realized with ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
Now the tricky part. The Argument, which should be passed is a FileName or a Path.
Parsing this is no problem.
But since my SingleInstance is realized with PostMessage
, im in trouble getting the Argument passed to the running instance.
My Native's-Class:
internal static class NativeMethods {
public const int HwndBroadcast = 0xffff;
public static readonly int WmShowme = RegisterWindowMessage("WM_SHOWME");
public static readonly int SendArg = RegisterWindowMessage("WM_SENDARG");
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImport("user32")]
private static extern int RegisterWindowMessage(string message);
}
The MessageSender
protected override void OnStartup(StartupEventArgs e) {
// check that there is only one instance of the control panel running...
bool createdNew;
this._instanceMutex = new Mutex(true, @"{0D119AC4-0F2D-4986-B4AB-5CEC8E52D9F3}", out createdNew);
if (!createdNew) {
var ptr = Marshal.StringToHGlobalUni("Hello World");
NativeMethods.PostMessage((IntPtr)NativeMethods.HwndBroadcast, NativeMethods.WmShowme, IntPtr.Zero, IntPtr.Zero);
NativeMethods.PostMessage((IntPtr)NativeMethods.HwndBroadcast, NativeMethods.SendArg, ptr, IntPtr.Zero);
Environment.Exit(0);
}
}
The Receiver (In MainWindow):
protected override void OnSourceInitialized(EventArgs e) {
base.OnSourceInitialized(e);
var source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(this.WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
if (msg == NativeMethods.SendArg) {
var s = Marshal.PtrToStringUni(wParam);
MessageBox.Show(s);
}
if (msg == NativeMethods.WmShowme) {
this.ShowMe();
}
return IntPtr.Zero;
}
Question / Issue:
Everything compiles and runs fine in the first instance. But after starting the second instance, i'm receiving an System.AccessViolationException
.
Wierd thing: Sometimes im getting no exception, but the received string shows as for example :
䀊虐\u0090
Since im no expert with WindowMessages and Pointers, i'm a bit clueless now.
Solutions including 3rd-Party-Tools, IPC or WCF are not wanted to realize for this project