-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
111 lines (92 loc) · 2.71 KB
/
main.cpp
File metadata and controls
111 lines (92 loc) · 2.71 KB
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
#include "pcloudapp.h"
#include "mylogger.h"
#include "psynclib.h"
#include <QApplication>
#include <QProcess>
#ifdef Q_OS_WIN
#include <windows.h>
#include <tlhelp32.h>
BOOL isMainWindowd(HWND handle)
{
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lParam)
{
if (hwnd == NULL)
{
return FALSE;
}
DWORD processId = 0;
GetWindowThreadProcessId(hwnd,&processId);
HWND parent = NULL;
//pCloud Sync Main
if(processId == (DWORD)lParam)
{
char wn[255];
int iLen = GetWindowTextA(hwnd, wn, 255);
if ( iLen > 0)
qDebug()<<"first"<<wn;
SendMessageW(hwnd,WM_USER+1,0,666);
//SendMessage(hwnd,WM_SHOWWINDOW,0,0);
while(parent = GetParent(hwnd))
hwnd = parent;
qDebug()<<"Qt: second launch" << GetLastError();
//ShowWindow(hwnd,SW_SHOW);
if (isMainWindowd(hwnd))
{
char wn[255];
int iLen = GetWindowTextA(hwnd, wn, 255);
if ( iLen > 0)
qDebug()<<"second"<<wn;
//SendMessage(hwnd,WM_SYSCOMMAND,0,0);
//SendMessageA
// SendMessageW(hwnd,WM_NOTIFY,0,0);
//PostMessageW(hwnd,WM_COMMAND,0,0);
}
}
return TRUE;
}
bool isRunning(){
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
DWORD id = GetCurrentProcessId();
WCHAR processName[MAX_PATH] = {0};
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(snapshot, &entry)){
while (Process32Next(snapshot, &entry)){
if (entry.th32ProcessID == id){
wcscpy(processName, entry.szExeFile);
break;
}
}
}
if (processName[0] && Process32First(snapshot, &entry)){
while (Process32Next(snapshot, &entry)){
if (entry.th32ProcessID != id && !wcsicmp(processName, entry.szExeFile)){
EnumWindows(EnumProc,entry.th32ProcessID);
CloseHandle(snapshot);
return true;
}
}
}
CloseHandle(snapshot);
return false;
}
#endif
int main(int argc, char *argv[])
{
QByteArray appSysInfo("pCloud (Qt) ");
appSysInfo += APP_VERSION;
psync_set_software_string(appSysInfo.constData());
if (psync_init() == -1)
{
qDebug()<<"Init sync returned failed. It may be already running! "<<psync_get_last_error();
#ifdef Q_OS_WIN
MyLogger logger;
if (isRunning())
MessageBoxA(NULL, "pCloud Drive is already running.", "Already running", MB_OK);
#endif
return 1;
}
return PCloudApp(argc, argv).exec();
}