This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cpp
167 lines (144 loc) · 4.51 KB
/
Startup.cpp
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
// 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 szPathName[MAX_PATH] = TEXT("\\"), szExtName[100] = {0};
OPENFILENAME of;
TCHAR szOpen[50] = TEXT("Select file");
const LPTSTR pszOpenFilter = TEXT("Browser Files(*.exe)\0*.exe\0All Documents (*.*)\0*.*\0\0");
memset((LPVOID)&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 | // Do not display the Read-Only check box.
OFN_LONGNAMES; // Display long filenames if possible.
of.lpfnHook = NULL;
of.Flags |= 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)
{
//{{AFX_DATA_INIT(Startup)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void Startup::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Startup)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Startup, CDialog)
//{{AFX_MSG_MAP(Startup)
ON_BN_CLICKED(IDC_Remove, OnRemove)
ON_BN_CLICKED(IDC_Add, OnAdd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Startup message handlers
BOOL Startup::OnInitDialog()
{
HANDLE hSearch;
WIN32_FIND_DATA FileData;
BOOL bFinished = FALSE;
CListBox* Liste = (CListBox*) GetDlgItem(IDC_Reg);
CString c;
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();
}
HRESULT CreateShortcut(/*in*/ LPCTSTR lpszFileName,
/*in*/ LPCTSTR lpszDesc,
/*in*/ LPCTSTR lpszShortcutPath)
{
HRESULT hRes = E_FAIL;
DWORD dwRet = 0;
CComPtr<IShellLink> ipShellLink;
TCHAR szPath[MAX_PATH];
LPTSTR lpszFilePart;
WCHAR wszTemp[MAX_PATH];
dwRet = GetFullPathName(lpszFileName,
sizeof(szPath) / sizeof(TCHAR),
szPath, &lpszFilePart);
if (!dwRet)
return hRes;
hRes = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void**)&ipShellLink);
if (SUCCEEDED(hRes))
{
CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);
hRes = ipShellLink->SetPath(szPath);
if (FAILED(hRes))
return hRes;
hRes = ipShellLink->SetDescription(lpszDesc);
if (FAILED(hRes))
return hRes;
MultiByteToWideChar(CP_ACP, 0,
lpszShortcutPath, -1, wszTemp, MAX_PATH);
hRes = ipPersistFile->Save(wszTemp, TRUE);
}
return hRes;
}
void Startup::OnAdd()
{
LPCTSTR lpszShortcutDesc = _T("Add by SStartup");
LPCTSTR lpszShortcutPath = _T("\\SStartup.lnk");
CreateShortcut(szFileName, lpszShortcutDesc, lpszShortcutPath);
OpenFile((HWND)NULL);
}