private ErrCode SendPipe(CtrlCmd param, MemoryStream send, ref MemoryStream res) { lock (thisLock) { // 接続待ち try { using (var waitEvent = System.Threading.EventWaitHandle.OpenExisting(eventName, System.Security.AccessControl.EventWaitHandleRights.Synchronize)) { if (waitEvent.WaitOne(connectTimeOut) == false) { return(ErrCode.CMD_ERR_TIMEOUT); } } } catch (System.Threading.WaitHandleCannotBeOpenedException) { return(ErrCode.CMD_ERR_CONNECT); } // 接続 using (var pipe = new System.IO.Pipes.NamedPipeClientStream(pipeName)) { pipe.Connect(0); // 送信 var head = new byte[8]; BitConverter.GetBytes((uint)param).CopyTo(head, 0); BitConverter.GetBytes((uint)(send == null ? 0 : send.Length)).CopyTo(head, 4); pipe.Write(head, 0, 8); if (send != null && send.Length != 0) { send.Close(); byte[] data = send.ToArray(); pipe.Write(data, 0, data.Length); } // 受信 if (pipe.Read(head, 0, 8) != 8) { return(ErrCode.CMD_ERR); } uint resParam = BitConverter.ToUInt32(head, 0); var resData = new byte[BitConverter.ToUInt32(head, 4)]; for (int n = 0; n < resData.Length;) { int m = pipe.Read(resData, n, resData.Length - n); if (m <= 0) { return(ErrCode.CMD_ERR); } n += m; } res = new MemoryStream(resData, false); return(Enum.IsDefined(typeof(ErrCode), resParam) ? (ErrCode)resParam : ErrCode.CMD_ERR); } } }
public void start(string pipe) { System.IO.Pipes.NamedPipeClientStream client = new System.IO.Pipes.NamedPipeClientStream(".", pipe, System.IO.Pipes.PipeDirection.Out, System.IO.Pipes.PipeOptions.None, System.Security.Principal.TokenImpersonationLevel.Delegation); client.Connect(); // Write some data (doesn't matter what) client.Write(new byte[] { 0x40, 0x5f, 0x78, 0x70, 0x6e, 0x5f }, 0, 6); }
static void Main(string[] args) { //------------------------------------ // Allowing only one instance //------------------------------------ try { // Create the mutex, other instances will not be able to create launch the app again using (Mutex mutex = new Mutex(true, @"PDS1_FileShare")) { // Wait 0 seconds, which means: just check if I could get the mutex if (mutex.WaitOne(TimeSpan.Zero, true)) { // Got the mutex? It means it wasn't owned by anybody. // Let's start the application App app = new App(); app.Run(); // Wait for the workers to finish before dying (actually, at this point they are already dead.) app.MulticastWorker.Wait(); app.SendToWorker.Wait(); app.ConnectionsListenerWorker.Wait(); // "Why setting as null if it is disposed? The app is going to die anyway!" // Well, if you don't do it, the app's taskbar icon will stay there until you hover your mouse on it. // Weird, isn't it? app.TaskbarIcon.Dispose(); app.TaskbarIcon = null; // I wrapped the mutex with using, do I really need to do this? // Doesn't .Disclose() already do this? mutex.ReleaseMutex(); } // If I didn't get the lock, it means I am a second instance. // NOTE: if you're here, it means that you are a new process, so we have to communicate // with the main process! I'll do that with a NamedPipe, see SendToWorker for details. else { // No args? if (args.Length < 1) { MessageBox.Show("Error!", "Only one instance at a time is allowed.", MessageBoxButton.OK, MessageBoxImage.Error); return; } // First arg is invalid? if (args[0].Length < 1) { MessageBox.Show("Error!", "The file is invalid.", MessageBoxButton.OK, MessageBoxImage.Error); return; } //------------------------------------ // Notify the SendToWorker //------------------------------------ // SendToWorker is waiting for the user to tell it to send something, // we are going to act as clients here. using (System.IO.Pipes.NamedPipeClientStream client = new System.IO.Pipes.NamedPipeClientStream(".", "pds_sendto", System.IO.Pipes.PipeDirection.Out)) { client.Connect(); if (client.CanWrite) { byte[] arg = Encoding.Unicode.GetBytes(args[0]); client.Write(arg, 0, arg.Length); } } } } } catch (Exception) { MessageBox.Show("Error!", "An error occurred while trying to run the app.", MessageBoxButton.OK, MessageBoxImage.Error); //Debug.WriteLine(e.Message); } }