[Jsmooth-cvs] jsmooth/skeletons/util-core SingleInstanceManager.cpp, NONE, 1.1 SingleInstanceManage
Status: Beta
Brought to you by:
reyes
From: Rodrigo R. <re...@us...> - 2007-05-17 10:57:13
|
Update of /cvsroot/jsmooth/jsmooth/skeletons/util-core In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv14160/skeletons/util-core Modified Files: Makefile.win testmain.cpp Added Files: SingleInstanceManager.cpp SingleInstanceManager.h Log Message: adds the single instance feature Index: testmain.cpp =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-core/testmain.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** testmain.cpp 4 May 2007 20:53:28 -0000 1.4 --- testmain.cpp 17 May 2007 10:56:19 -0000 1.5 *************** *** 27,30 **** --- 27,32 ---- #include "Process.h" + #include "SingleInstanceManager.h" + void _debugOutput(const std::string& text) { *************** *** 46,49 **** --- 48,60 ---- // printf("result: %s\n", buffer); + SingleInstanceManager singinst; + if (singinst.alreadyExists()) + { + printf("Already running...\n"); + exit(0); + } + else + Sleep(50000); + std::string exeline = "C:\\Program Files\\Java\\jdk1.5.0_11\\bin\\java.exe -version"; string tmpfilename = FileUtils::createTempFileName(".tmp"); Index: Makefile.win =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-core/Makefile.win,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Makefile.win 30 Apr 2007 20:54:32 -0000 1.4 --- Makefile.win 17 May 2007 10:56:18 -0000 1.5 *************** *** 8,12 **** MINGW = RES = ! OBJ = Process.o Log.o Thread.o FileUtils.o StringUtils.o DebugConsole.o $(RES) LIBS = -L"$(MINGW)/lib" -L"/lib" -lws2_32 INCS = -I"$(MINGW)/include" -I"$(JDK)/include" -I"$(JDK)/include/win32" --- 8,12 ---- MINGW = RES = ! OBJ = SingleInstanceManager.o Process.o Log.o Thread.o FileUtils.o StringUtils.o DebugConsole.o $(RES) LIBS = -L"$(MINGW)/lib" -L"/lib" -lws2_32 INCS = -I"$(MINGW)/include" -I"$(JDK)/include" -I"$(JDK)/include/win32" --- NEW FILE: SingleInstanceManager.cpp --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "SingleInstanceManager.h" #include <windows.h> #include <winbase.h> void pipe_wait_callback(void* param) { SingleInstanceManager* sim = (SingleInstanceManager*)param; while (true) { std::string msg = sim->waitPipeMessage(); printf("Got MESSAGE: %s\n", msg.c_str()); sim->processMessage(msg); fflush(stdout); } } SingleInstanceManager::SingleInstanceManager() { std::string name = "{"+FileUtils::getExecutablePath() + "::" + FileUtils::getExecutableFileName() + "}"; name = StringUtils::replace(name, "\\", ":"); init(name); } SingleInstanceManager::SingleInstanceManager(const std::string& uniqueName) { init(uniqueName); } SingleInstanceManager::~SingleInstanceManager() { if (m_mutex != NULL) CloseHandle(m_mutex); } void SingleInstanceManager::init(const std::string& uniqname) { m_name = uniqname; m_mutex = CreateMutex(NULL, FALSE, uniqname.c_str()); //do early m_lasterror = GetLastError(); //save for use later... m_alreadyexists = (m_lasterror == ERROR_ALREADY_EXISTS)?true:false; } void SingleInstanceManager::startMasterInstanceServer() { if (!m_alreadyexists) { m_pipethread.start(pipe_wait_callback, this); } } void SingleInstanceManager::sendMessageInstanceShow() { char *message = "SHOWWINDOW PLZ"; DWORD msglen = strlen(message); char bufferout[256]; DWORD lenout = 255, readlen=0; CallNamedPipe(getPipeName().c_str(), message, msglen, bufferout, lenout, &readlen, 2); } bool SingleInstanceManager::alreadyExists() { return m_alreadyexists; } std::string SingleInstanceManager::waitPipeMessage() { std::string result = ""; std::string pipename = getPipeName(); m_pipe = CreateNamedPipe(pipename.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 6, 256, 256, NMPWAIT_USE_DEFAULT_WAIT, NULL); if (m_pipe == INVALID_HANDLE_VALUE) { m_pipe = NULL; return ""; } if (ConnectNamedPipe(m_pipe, NULL)) { printf("waitPipeMessage...\n"); fflush(stdout); printf("connected to named pipe...\n"); fflush(stdout); char buffer[256]; DWORD length; bool res = ReadFile(m_pipe, buffer, sizeof(buffer), &length, NULL); buffer[length] = 0; printf("read file %s\n", buffer); fflush(stdout); if (res && (length>0)) { bool res = WriteFile(m_pipe, buffer, strlen(buffer), &length, NULL); result = buffer; } DisconnectNamedPipe(m_pipe); } CloseHandle(m_pipe); return result; } BOOL CALLBACK displayWindowForProcess(HWND hwnd, LPARAM lParam ) { DWORD myprocessid = GetCurrentProcessId(); DWORD hid; GetWindowThreadProcessId(hwnd, &hid); printf("Checking %d, %d, %d\n", hwnd, hid, myprocessid); fflush(stdout); if (hid == myprocessid) { LONG style = GetWindowLong(hwnd, GWL_STYLE); if ((style & (WS_OVERLAPPEDWINDOW|WS_TILED|WS_VISIBLE)) != 0) { ShowWindow(hwnd, SW_HIDE); //ShowWindow(hwnd, SW_SHOW); ShowWindow(hwnd, SW_SHOWMINIMIZED); ShowWindow(hwnd, SW_RESTORE); return FALSE; } } return true; } void SingleInstanceManager::processMessage(const std::string& msg) { printf("PROCESSING MESSAGE\n"); fflush(stdout); EnumWindows(displayWindowForProcess, 0); } std::string SingleInstanceManager::getPipeName() { std::string pipename = "\\\\.\\pipe\\"; if (m_name.length() > (255-9)) pipename += m_name.substr( m_name.length() - (255-9) ); else pipename += m_name; return pipename; } --- NEW FILE: SingleInstanceManager.h --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _SINGLEINSTANCEMANAGER_H_ #define _SINGLEINSTANCEMANAGER_H_ #include "windows.h" #include <string> #include "FileUtils.h" #include "Thread.h" class SingleInstanceManager { protected: std::string m_name; bool m_alreadyexists; HANDLE m_mutex; HANDLE m_pipe; DWORD m_lasterror; Thread m_pipethread; public: SingleInstanceManager(); SingleInstanceManager(const std::string& uniqueName); ~SingleInstanceManager(); bool alreadyExists(); std::string waitPipeMessage(); void processMessage(const std::string& msg); void startMasterInstanceServer(); void sendMessageInstanceShow(); private: void init(const std::string& uniqname); std::string getPipeName(); }; #endif |