Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Ext2Mgr.cpp « Ext2Mgr - github.com/matt-wu/Ext3Fsd.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b7dd603a6fd43d21db82aa22c1442bbc8317452 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Ext2Mgr.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Ext2Mgr.h"
#include "Ext2MgrDlg.h"
#include "Splash.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExt2MgrApp

BEGIN_MESSAGE_MAP(CExt2MgrApp, CWinApp)
    //{{AFX_MSG_MAP(CExt2MgrApp)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG
    ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExt2MgrApp construction

CExt2MgrApp::CExt2MgrApp()
{
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CExt2MgrApp object

CExt2MgrApp theApp;

/////////////////////////////////////////////////////////////////////////////
// globals

DWORD Checkpoint = 1;

SERVICE_STATUS          ServiceStatus;
SERVICE_STATUS_HANDLE   ServiceHandle;

/////////////////////////////////////////////////////////////////////////////
// CExt2MgrApp initialization

#if 0
void WINAPI ManagerServiceEntry(DWORD argc, char **argv);
void ManagerServiceThread(void *arg);
void ManagerStopService();
void WINAPI ManagerServiceCtrl(DWORD ctrlcode);
#endif


BOOLEAN
ManagerReportStatus(
    SERVICE_STATUS_HANDLE Handle,
    SERVICE_STATUS* Status,
    DWORD           State,
    DWORD           Exitcode,
    DWORD           Timeout
)
{
    // If we're in the start state then we don't want the control manager
    // sending us control messages because they'll confuse us.
    if (State == SERVICE_START_PENDING) {
        Status->dwControlsAccepted = 0;
    } else {
        Status->dwControlsAccepted = SERVICE_ACCEPT_STOP;
    }

    // Save the new status we've been given
    Status->dwCurrentState = State;
    Status->dwWin32ExitCode = Exitcode;
    Status->dwWaitHint = Timeout;

    // Update the checkpoint variable to let the SCM know that we
    // haven't died if requests take a long time
    if ((State == SERVICE_RUNNING) || (State == SERVICE_STOPPED)) {
        Status->dwCheckPoint = 0;
    } else {
        Status->dwCheckPoint = Checkpoint++;
    }

    // Tell the SCM our new status
    return SetServiceStatus(Handle, Status);
}

void ManagerStopService()
{
    ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    theApp.m_pMainWnd->SendMessage(WM_TERMINATE_PROGRAM, 0, 0x1234);
}

void WINAPI ManagerCtrlService(DWORD ctrlcode)
{
    switch (ctrlcode)
    {

    case SERVICE_CONTROL_STOP:
        // STOP : The service must stop
        ManagerStopService();
        break;

    case SERVICE_CONTROL_INTERROGATE:
        // QUERY : Service control manager just wants to know our state
        break;

    default:
        // Control code not recognised
        break;

    }

    // Tell the control manager what we're up to.
    ManagerReportStatus(
        ServiceHandle,
        &ServiceStatus,
        ServiceStatus.dwCurrentState,
        NO_ERROR, 0);
}

VOID
ManagerStartMain(VOID * arg)
{
    BOOLEAN isService = (BOOLEAN) arg;
    CExt2MgrDlg* dlg = (CExt2MgrDlg*)theApp.m_pMainWnd;

    if (dlg) {

        if (!dlg->m_bQuiet) {
            CSplash* splash = new CSplash(IDB_ABOUT_SCREEN, RGB(128, 128, 128));
            splash->ShowSplash();
            dlg->m_splash = splash;
        }

        if (isService) {
            ManagerReportStatus(
                ServiceHandle,
                &ServiceStatus,
                SERVICE_RUNNING,
                NO_ERROR, 0   );
        }

        dlg->DoModal();
    }

    if (isService) {
        ManagerReportStatus(
            ServiceHandle,
            &ServiceStatus,
            SERVICE_STOPPED,
            NO_ERROR, 0   );
    }
}

void WINAPI
ManagerServiceEntry(DWORD argc, char**argv)
{
    // register the service control handler
    ServiceHandle =
        RegisterServiceCtrlHandler(
            "Ext2Mgr",
            ManagerCtrlService   );

    if (ServiceHandle == 0) {
        return;
    }

    // setup standard service state values
    ServiceStatus.dwServiceType = SERVICE_WIN32 | SERVICE_INTERACTIVE_PROCESS;
    ServiceStatus.dwServiceSpecificExitCode = 0;

    // report our status to the SCM
    if (!ManagerReportStatus(
                ServiceHandle,
                &ServiceStatus,
                SERVICE_START_PENDING,
                NO_ERROR,
                600000
            ))
    {
        ManagerReportStatus(
            ServiceHandle,
            &ServiceStatus,
            SERVICE_STOPPED,
            NO_ERROR,
            0);
        return;
    }

    // Now start the service for real
    _beginthread(ManagerStartMain, 0, (PVOID)TRUE);
    return;
}


VOID
ManagerStartService(VOID *arg)
{
    SERVICE_TABLE_ENTRY ManagerSeriveTable[] =
    {
        {"Ext2Mgr", (LPSERVICE_MAIN_FUNCTION)ManagerServiceEntry},
        {NULL, NULL}
    };

    // let service control dispatcher start Ext2Mgr
    StartServiceCtrlDispatcher(ManagerSeriveTable);
}

BOOL CExt2MgrApp::InitInstance()
{
    AfxEnableControlContainer();

    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.

#ifdef _AFXDLL
    Enable3dControls();			// Call this when using MFC in a shared DLL
#else
    Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

    HWND hWnd = ::FindWindow(NULL, "Ext2 Volume Manager");
    if (hWnd) {
        if (::GetWindowLong(hWnd, DWL_USER) == EXT2_DIALOG_MAGIC) {
            ::ShowWindow(hWnd, SW_SHOW);
            ::SetForegroundWindow(hWnd);
            return FALSE;
        }
    }

    BOOLEAN bHide = FALSE;
    BOOLEAN bQuiet = FALSE;
    BOOLEAN bService = FALSE;
    BOOLEAN bInstall = FALSE;
    BOOLEAN bRemove = FALSE;
    BOOLEAN bStat = FALSE;

    char * cmds = GetCommandLine();
    int i = (int)strlen(cmds);
    while (--i > 0) {
        if (cmds[i] == (char)' ') {
            cmds[i] = 0;
            if ( strlen(&cmds[i+1]) == 5 &&
                    _stricmp(&cmds[i+2], "hide") == 0 &&
                    (cmds[i+1] == '/' || cmds[i+1] == '-')) {
                bHide = TRUE;
            } else if (strlen(&cmds[i+1]) == 6 &&
                       _stricmp(&cmds[i+2], "quiet") == 0 &&
                       (cmds[i+1] == '/' || cmds[i+1] == '-')) {
                bHide = bQuiet = TRUE;
            } else if (strlen(&cmds[i+1]) == 8 &&
                       _stricmp(&cmds[i+2], "install") == 0 &&
                       (cmds[i+1] == '/' || cmds[i+1] == '-')) {
                bInstall = TRUE;
            } else if (strlen(&cmds[i+1]) == 7 &&
                       _stricmp(&cmds[i+2], "remove") == 0 &&
                       (cmds[i+1] == '/' || cmds[i+1] == '-')) {
                bRemove = TRUE;
            } else if  (strlen(&cmds[i+1]) == 8 &&
                        _stricmp(&cmds[i+2], "service") == 0 &&
                        (cmds[i+1] == '/' || cmds[i+1] == '-')) {
                bHide = bService = TRUE;
            } else if  (strlen(&cmds[i+1]) == 5 &&
                        _stricmp(&cmds[i+2], "stat") == 0 &&
                        (cmds[i+1] == '/' || cmds[i+1] == '-')) {
                bHide = bStat = TRUE;
            }
        }
    }

    if (bInstall) {
        Ext2SetManagerAsService(TRUE);
        return FALSE;
    }

    if (bRemove) {
        Ext2SetManagerAsService(FALSE);
        return FALSE;
    }

    CExt2MgrDlg theDlg;

    theDlg.m_bHide = bHide;
    theDlg.m_bQuiet = bQuiet = bHide;
    theDlg.m_bService = bService;
    theDlg.m_bStat = bStat;

    m_pMainWnd = &theDlg;

    if (bService) {
        ManagerStartService(NULL);
    } else {
        ManagerStartMain(NULL);
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
}