forked from jmfrouin/stweaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cpp
More file actions
159 lines (128 loc) · 4.04 KB
/
Startup.cpp
File metadata and controls
159 lines (128 loc) · 4.04 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
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
// Startup.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "Startup.h"
#include <Atlbase.h>
TCHAR szFileName[MAX_PATH];
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Startup dialog
bool OpenFile(HWND hOwner)
{
BOOL bSuccess = FALSE;
TCHAR szExtName[100] = {0};
OPENFILENAME of;
TCHAR szOpen[50] = TEXT("Select file");
const LPTSTR pszOpenFilter =
TEXT("Executable Files (*.exe)\0*.exe\0All Files (*.*)\0*.*\0\0");
memset(&of, 0, sizeof(OPENFILENAME));
of.lStructSize = sizeof(OPENFILENAME);
of.hwndOwner = hOwner;
of.lpstrFile = szFileName;
of.lpstrInitialDir = NULL;
of.nMaxFile = sizeof(szFileName) / sizeof(TCHAR);
of.lpstrDefExt = szExtName;
of.Flags = OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_FILEMUSTEXIST;
of.lpstrTitle = szOpen;
of.lpstrFilter = pszOpenFilter;
bSuccess = GetOpenFileName(&of);
if (bSuccess)
{
MessageBox(hOwner, szFileName, _T("Notice"), MB_OK | MB_ICONINFORMATION);
}
else
{
MessageBox(hOwner, szFileName, _T("Notice3"), MB_OK | MB_ICONINFORMATION);
}
return true;
}
Startup::Startup(CWnd* pParent /*=NULL*/)
: CDialog(Startup::IDD, pParent)
{
}
void Startup::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(Startup, CDialog)
ON_BN_CLICKED(IDC_Remove, OnRemove)
ON_BN_CLICKED(IDC_Add, OnAdd)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Startup message handlers
BOOL Startup::OnInitDialog()
{
HANDLE hSearch;
WIN32_FIND_DATA FileData;
BOOL bFinished = FALSE;
CListBox* Liste = (CListBox*)GetDlgItem(IDC_Reg);
CDialog::OnInitDialog();
Liste->ResetContent();
hSearch = FindFirstFile(TEXT("\\Windows\\Startup\\*"), &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return FALSE;
}
while (!bFinished)
{
Liste->AddString(FileData.cFileName);
if (!FindNextFile(hSearch, &FileData))
{
bFinished = TRUE;
}
}
return TRUE;
}
void Startup::OnRemove()
{
CListBox* Liste = (CListBox*)GetDlgItem(IDC_Reg);
CString str, c;
int nIndex = Liste->GetCurSel();
Liste->GetText(nIndex, str);
c.Format(_T("\\Windows\\Startup\\%s"), str);
if (DeleteFile(c))
MessageBox(_T("Deleted from \\Windows\\Startup"), _T("SStartup"), MB_OK);
OnInitDialog();
}
/////////////////////////////////////////////////////////////////////////////
// Windows CE Shortcut Creation (NO COM)
HRESULT CreateShortcut(LPCTSTR lpszFileName,
LPCTSTR lpszDesc,
LPCTSTR lpszShortcutPath)
{
// Windows CE uses simple .lnk files, not COM ShellLink objects.
// Format: <length>#<path> <args>
CString target = lpszFileName;
CString linkPath = lpszShortcutPath;
CString content;
content.Format(_T("%d#%s"), target.GetLength(), target);
HANDLE hFile = CreateFile(linkPath,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return E_FAIL;
DWORD written = 0;
WriteFile(hFile,
content, // CE-compatible: CString ? LPCTSTR
content.GetLength() * sizeof(TCHAR),
&written,
NULL);
CloseHandle(hFile);
return S_OK;
}
void Startup::OnAdd()
{
LPCTSTR lpszShortcutDesc = _T("Added by SStartup");
LPCTSTR lpszShortcutPath = _T("\\Windows\\Startup\\SStartup.lnk");
CreateShortcut(szFileName, lpszShortcutDesc, lpszShortcutPath);
OpenFile((HWND)NULL);
}