@@ -32,6 +32,7 @@ THE SOFTWARE.
3232//
3333
3434#include " stdafx.h"
35+ #include < iostream>
3536#include " cppcryptfs.h"
3637#include " crypt/cryptdefs.h"
3738#include " ui/CryptPropertySheet.h"
@@ -47,8 +48,8 @@ THE SOFTWARE.
4748#include " util/util.h"
4849#include " crypt/crypt.h"
4950#include " ui/uiutil.h"
50- #include " namedpipe /server.h"
51- #include " namedpipe /client.h"
51+ #include " ../libipc /server.h"
52+ #include " ../libipc /client.h"
5253
5354
5455#ifdef _DEBUG
@@ -90,49 +91,23 @@ CcppcryptfsApp::CcppcryptfsApp()
9091CcppcryptfsApp theApp;
9192
9293
93- static DWORD WINAPI ServerThreadProc (PVOID lpvParam)
94+
95+
96+ static void NamedPipeServerCallback (void * ctx, HANDLE hPipe)
9497{
95- // _tprintf(TEXT("\nPipe Server: Main thread awaiting client connection on %s\n"), lpszPipename);
96-
97-
98- while (true ) {
99- // Wait for the client to connect; if it succeeds,
100- // the function returns a nonzero value. If the function
101- // returns zero, GetLastError returns ERROR_PIPE_CONNECTED.
102-
103- auto hPipe = CreateNamedPipe (
104- CMD_NAMED_PIPE , // pipe name
105- PIPE_ACCESS_DUPLEX , // read/write access
106- PIPE_TYPE_MESSAGE | // message type pipe
107- PIPE_READMODE_MESSAGE | // message-read mode
108- PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS , // blocking mode, local only
109- PIPE_UNLIMITED_INSTANCES , // max. instances
110- CMD_NAMED_PIPE_BUFSIZE , // output buffer size
111- CMD_NAMED_PIPE_BUFSIZE , // input buffer size
112- 0 , // client time-out
113- NULL ); // default security attribute
114-
115- if (hPipe == INVALID_HANDLE_VALUE )
116- return 1 ;
117-
118- auto fConnected = ConnectNamedPipe (hPipe, NULL ) ?
119- TRUE : (GetLastError () == ERROR_PIPE_CONNECTED );
120-
121- if (fConnected ) {
122- theApp.SendCmdArgsToSelf (hPipe);
123- } else {
124- // The client could not connect, so close the pipe.
125- CloseHandle (hPipe);
126- }
127- }
98+ auto pApp = reinterpret_cast <CcppcryptfsApp*>(ctx);
12899
129- return 0 ;
100+ pApp-> SendCmdArgsToSelf (hPipe) ;
130101}
131102
132-
133- bool StartNamedPipeServer ()
103+ static bool StartNamedPipeServer ()
134104{
135- auto hThread = CreateThread (NULL , 0 , ServerThreadProc, NULL , 0 , NULL );
105+ static NamedPipeServerContext ctx;
106+
107+ ctx.context = &theApp;
108+ ctx.callback = NamedPipeServerCallback;
109+
110+ auto hThread = CreateThread (NULL , 0 , NamedPipeServerThreadProc, &ctx, 0 , NULL );
136111
137112 if (hThread != NULL )
138113 CloseHandle (hThread);
@@ -169,7 +144,38 @@ BOOL CcppcryptfsApp::InitInstance()
169144
170145 if (hWnd) {
171146 if (have_args ()) {
172- ::MessageBox (NULL , L" use cppcryptfsctl.exe to send commands to a running cppcryptfs" , L" cppcryptfs" , MB_OK | MB_ICONERROR );
147+
148+ bool have_console = OpenConsole (0 );
149+
150+ wstring err_mes;
151+ wstring result;
152+
153+ if (!SendArgsToRunningInstance (GetCommandLine (), result)) {
154+ err_mes = L" cppcryptfsctl: Unable to send command. Is cppcryptfs really already running?\n " ;
155+ } else {
156+ if (result.length () >= CMD_PIPE_RESPONSE_LENGTH ) {
157+ if (wcsncmp (result.c_str (), CMD_PIPE_SUCCESS_STR , CMD_PIPE_RESPONSE_LENGTH ) == 0 ) {
158+ if (have_console)
159+ wcout << wstring (result.c_str () + CMD_PIPE_RESPONSE_LENGTH );
160+ } else {
161+ err_mes = wstring (result.c_str () + CMD_PIPE_RESPONSE_LENGTH );
162+ }
163+ } else {
164+ err_mes = L" cppcryptfs: got a mal-formed response from running instance of cppcryptfs\n " ;
165+ }
166+ }
167+
168+ if (err_mes.length () > 0 ) {
169+ if (have_console) {
170+ wcerr << err_mes;
171+ } else {
172+ ::MessageBox (NULL , err_mes.c_str(), L"cppcryptfs", MB_ICONERROR | MB_OK);
173+ }
174+ }
175+
176+ if (have_console)
177+ CloseConsole ();
178+
173179 } else { // if no args, then restore window of running instance
174180 ShowWindow (hWnd, SW_SHOWNORMAL );
175181 }
0 commit comments