Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygencreatelink.c
Go to the documentation of this file.
00001 /* $Id: appwiz.c 29364 2007-10-02 23:34:00Z janderwald $ 00002 * 00003 * PROJECT: ReactOS Software Control Panel 00004 * FILE: dll/cpl/appwiz/createlink.c 00005 * PURPOSE: ReactOS Software Control Panel 00006 * PROGRAMMER: Gero Kuehn (reactos.filter@gkware.com) 00007 * Dmitry Chapyshev (lentind@yandex.ru) 00008 * Johannes Anderwald 00009 * UPDATE HISTORY: 00010 * 06-17-2004 Created 00011 */ 00012 00013 #include "appwiz.h" 00014 00015 BOOL 00016 IsShortcut(HKEY hKey) 00017 { 00018 WCHAR Value[10]; 00019 DWORD Size; 00020 DWORD Type; 00021 00022 Size = sizeof(Value); 00023 if (RegQueryValueExW(hKey, L"IsShortcut", NULL, &Type, (LPBYTE)Value, &Size) != ERROR_SUCCESS) 00024 return FALSE; 00025 00026 if (Type != REG_SZ) 00027 return FALSE; 00028 00029 return (wcsicmp(Value, L"yes") == 0); 00030 } 00031 00032 BOOL 00033 IsExtensionAShortcut(LPWSTR lpExtension) 00034 { 00035 HKEY hKey; 00036 WCHAR Buffer[100]; 00037 DWORD Size; 00038 DWORD Type; 00039 00040 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, lpExtension, 0, KEY_READ, &hKey) != ERROR_SUCCESS) 00041 return FALSE; 00042 00043 if (IsShortcut(hKey)) 00044 { 00045 RegCloseKey(hKey); 00046 return TRUE; 00047 } 00048 00049 Size = sizeof(Buffer); 00050 if (RegQueryValueEx(hKey, NULL, NULL, &Type, (LPBYTE)Buffer, &Size) != ERROR_SUCCESS || Type != REG_SZ) 00051 { 00052 RegCloseKey(hKey); 00053 return FALSE; 00054 } 00055 00056 RegCloseKey(hKey); 00057 00058 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, Buffer, 0, KEY_READ, &hKey) != ERROR_SUCCESS) 00059 return FALSE; 00060 00061 if (IsShortcut(hKey)) 00062 { 00063 RegCloseKey(hKey); 00064 return TRUE; 00065 } 00066 00067 RegCloseKey(hKey); 00068 return FALSE; 00069 } 00070 00071 BOOL 00072 CreateShortcut(PCREATE_LINK_CONTEXT pContext) 00073 { 00074 IShellLinkW *pShellLink, *pSourceShellLink; 00075 IPersistFile *pPersistFile; 00076 HRESULT hr; 00077 WCHAR Path[MAX_PATH]; 00078 LPWSTR lpExtension; 00079 00080 /* get the extension */ 00081 lpExtension = wcsrchr(pContext->szTarget, '.'); 00082 00083 if (IsExtensionAShortcut(lpExtension)) 00084 { 00085 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_ALL, &IID_IShellLink, (void**)&pSourceShellLink); 00086 00087 if (hr != S_OK) 00088 return FALSE; 00089 00090 hr = pSourceShellLink->lpVtbl->QueryInterface(pSourceShellLink, &IID_IPersistFile, (void**)&pPersistFile); 00091 if (hr != S_OK) 00092 { 00093 pSourceShellLink->lpVtbl->Release(pSourceShellLink); 00094 return FALSE; 00095 } 00096 00097 hr = pPersistFile->lpVtbl->Load(pPersistFile, (LPCOLESTR)pContext->szTarget, STGM_READ); 00098 pPersistFile->lpVtbl->Release(pPersistFile); 00099 00100 if (hr != S_OK) 00101 { 00102 pSourceShellLink->lpVtbl->Release(pSourceShellLink); 00103 return FALSE; 00104 } 00105 00106 hr = pSourceShellLink->lpVtbl->GetPath(pSourceShellLink, Path, sizeof(Path) / sizeof(WCHAR), NULL, 0); 00107 pSourceShellLink->lpVtbl->Release(pSourceShellLink); 00108 00109 if (hr != S_OK) 00110 { 00111 return FALSE; 00112 } 00113 } 00114 else 00115 { 00116 wcscpy(Path, pContext->szTarget); 00117 } 00118 00119 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_ALL, 00120 &IID_IShellLink, (void**)&pShellLink); 00121 00122 if (hr != S_OK) 00123 return FALSE; 00124 00125 00126 pShellLink->lpVtbl->SetPath(pShellLink, Path); 00127 pShellLink->lpVtbl->SetDescription(pShellLink, pContext->szDescription); 00128 pShellLink->lpVtbl->SetWorkingDirectory(pShellLink, pContext->szWorkingDirectory); 00129 00130 hr = pShellLink->lpVtbl->QueryInterface(pShellLink, &IID_IPersistFile, (void**)&pPersistFile); 00131 if (hr != S_OK) 00132 { 00133 pShellLink->lpVtbl->Release(pShellLink); 00134 return FALSE; 00135 } 00136 00137 hr = pPersistFile->lpVtbl->Save(pPersistFile, pContext->szLinkName, TRUE); 00138 pPersistFile->lpVtbl->Release(pPersistFile); 00139 pShellLink->lpVtbl->Release(pShellLink); 00140 return (hr == S_OK); 00141 } 00142 00143 00144 00145 00146 INT_PTR 00147 CALLBACK 00148 WelcomeDlgProc(HWND hwndDlg, 00149 UINT uMsg, 00150 WPARAM wParam, 00151 LPARAM lParam) 00152 { 00153 LPPROPSHEETPAGEW ppsp; 00154 PCREATE_LINK_CONTEXT pContext; 00155 LPPSHNOTIFY lppsn; 00156 WCHAR szPath[MAX_PATH]; 00157 WCHAR szDesc[100]; 00158 BROWSEINFOW brws; 00159 LPITEMIDLIST pidllist; 00160 IMalloc* malloc; 00161 00162 switch(uMsg) 00163 { 00164 case WM_INITDIALOG: 00165 ppsp = (LPPROPSHEETPAGEW)lParam; 00166 pContext = (PCREATE_LINK_CONTEXT) ppsp->lParam; 00167 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pContext); 00168 PropSheet_SetWizButtons(GetParent(hwndDlg), 0); 00169 break; 00170 case WM_COMMAND: 00171 switch(HIWORD(wParam)) 00172 { 00173 case EN_CHANGE: 00174 if (SendDlgItemMessage(hwndDlg, IDC_SHORTCUT_LOCATION, WM_GETTEXTLENGTH, 0, 0)) 00175 { 00176 PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_NEXT); 00177 } 00178 else 00179 { 00180 PropSheet_SetWizButtons(GetParent(hwndDlg), 0); 00181 } 00182 break; 00183 } 00184 switch(LOWORD(wParam)) 00185 { 00186 case IDC_SHORTCUT_BROWSE: 00187 ZeroMemory(&brws, sizeof(brws)); 00188 brws.hwndOwner = hwndDlg; 00189 brws.pidlRoot = NULL; 00190 brws.pszDisplayName = szPath; 00191 brws.ulFlags = BIF_BROWSEINCLUDEFILES; 00192 brws.lpfn = NULL; 00193 pidllist = SHBrowseForFolder(&brws); 00194 if (!pidllist) 00195 break; 00196 00197 if (SHGetPathFromIDList(pidllist, szPath)) 00198 SendDlgItemMessage(hwndDlg, IDC_SHORTCUT_LOCATION, WM_SETTEXT, 0, (LPARAM)szPath); 00199 00200 /* Free memory, if possible */ 00201 if (SUCCEEDED(SHGetMalloc(&malloc))) 00202 { 00203 IMalloc_Free(malloc, pidllist); 00204 IMalloc_Release(malloc); 00205 } 00206 00207 break; 00208 } 00209 break; 00210 case WM_NOTIFY: 00211 lppsn = (LPPSHNOTIFY) lParam; 00212 if (lppsn->hdr.code == PSN_WIZNEXT) 00213 { 00214 pContext = (PCREATE_LINK_CONTEXT) GetWindowLongPtr(hwndDlg, DWLP_USER); 00215 SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_LOCATION, WM_GETTEXT, MAX_PATH, (LPARAM)pContext->szTarget); 00220 if (GetFileAttributesW(pContext->szTarget) == INVALID_FILE_ATTRIBUTES) 00221 { 00222 szDesc[0] = L'\0'; 00223 szPath[0] = L'\0'; 00224 if (LoadStringW(hApplet, IDS_CREATE_SHORTCUT, szDesc, 100) < 100 && 00225 LoadStringW(hApplet, IDS_ERROR_NOT_FOUND, szPath, MAX_PATH) < MAX_PATH) 00226 { 00227 WCHAR szError[MAX_PATH + 100]; 00228 #ifdef _MSC_VER 00229 _swprintf(szError, szPath, pContext->szTarget); 00230 #else 00231 swprintf(szError, szPath, pContext->szTarget); 00232 #endif 00233 MessageBoxW(hwndDlg, szError, szDesc, MB_ICONERROR); 00234 } 00235 SendDlgItemMessage(hwndDlg, IDC_SHORTCUT_LOCATION, EM_SETSEL, 0, -1); 00236 SetFocus(GetDlgItem(hwndDlg, IDC_SHORTCUT_LOCATION)); 00237 SetWindowLongPtr(hwndDlg, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE); 00238 return -1; 00239 } 00240 else 00241 { 00242 WCHAR * first, *last; 00243 wcscpy(pContext->szWorkingDirectory, pContext->szTarget); 00244 first = wcschr(pContext->szWorkingDirectory, L'\\'); 00245 last = wcsrchr(pContext->szWorkingDirectory, L'\\'); 00246 wcscpy(pContext->szDescription, &last[1]); 00247 00248 if (first != last) 00249 last[0] = L'\0'; 00250 else 00251 first[1] = L'\0'; 00252 00253 first = wcsrchr(pContext->szDescription, L'.'); 00254 00255 if(first) 00256 first[0] = L'\0'; 00257 } 00258 00259 } 00260 break; 00261 } 00262 return FALSE; 00263 } 00264 00265 INT_PTR 00266 CALLBACK 00267 FinishDlgProc(HWND hwndDlg, 00268 UINT uMsg, 00269 WPARAM wParam, 00270 LPARAM lParam) 00271 { 00272 LPPROPSHEETPAGEW ppsp; 00273 PCREATE_LINK_CONTEXT pContext; 00274 LPPSHNOTIFY lppsn; 00275 00276 switch(uMsg) 00277 { 00278 case WM_INITDIALOG: 00279 ppsp = (LPPROPSHEETPAGEW)lParam; 00280 pContext = (PCREATE_LINK_CONTEXT) ppsp->lParam; 00281 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pContext); 00282 SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_NAME, WM_SETTEXT, 0, (LPARAM)pContext->szDescription); 00283 PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK | PSWIZB_FINISH); 00284 break; 00285 case WM_COMMAND: 00286 switch(HIWORD(wParam)) 00287 { 00288 case EN_CHANGE: 00289 if (SendDlgItemMessage(hwndDlg, IDC_SHORTCUT_NAME, WM_GETTEXTLENGTH, 0, 0)) 00290 { 00291 PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK | PSWIZB_FINISH); 00292 } 00293 else 00294 { 00295 PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK); 00296 } 00297 break; 00298 } 00299 break; 00300 case WM_NOTIFY: 00301 lppsn = (LPPSHNOTIFY) lParam; 00302 pContext = (PCREATE_LINK_CONTEXT) GetWindowLongPtr(hwndDlg, DWLP_USER); 00303 if (lppsn->hdr.code == PSN_WIZFINISH) 00304 { 00305 SendDlgItemMessageW(hwndDlg, IDC_SHORTCUT_NAME, WM_GETTEXT, MAX_PATH, (LPARAM)pContext->szDescription); 00306 wcscat(pContext->szLinkName, pContext->szDescription); 00307 wcscat(pContext->szLinkName, L".lnk"); 00308 if (!CreateShortcut(pContext)) 00309 { 00310 MessageBox(hwndDlg, _T("Failed to create shortcut"), _T("Error"), MB_ICONERROR); 00311 } 00312 } 00313 00314 00315 } 00316 return FALSE; 00317 } 00318 00319 LONG CALLBACK 00320 ShowCreateShortcutWizard(HWND hwndCPl, LPWSTR szPath) 00321 { 00322 PROPSHEETHEADERW psh; 00323 HPROPSHEETPAGE ahpsp[2]; 00324 PROPSHEETPAGE psp; 00325 UINT nPages = 0; 00326 UINT nLength; 00327 DWORD attrs; 00328 00329 PCREATE_LINK_CONTEXT pContext = (PCREATE_LINK_CONTEXT) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CREATE_LINK_CONTEXT)); 00330 if (!pContext) 00331 { 00332 /* no memory */ 00333 return FALSE; 00334 } 00335 nLength = wcslen(szPath); 00336 if (!nLength) 00337 { 00338 HeapFree(GetProcessHeap(), 0, pContext); 00339 00340 /* no directory given */ 00341 return FALSE; 00342 } 00343 00344 attrs = GetFileAttributesW(szPath); 00345 if (attrs == INVALID_FILE_ATTRIBUTES) 00346 { 00347 HeapFree(GetProcessHeap(), 0, pContext); 00348 00349 /* invalid path */ 00350 return FALSE; 00351 } 00352 00353 wcscpy(pContext->szLinkName, szPath); 00354 if (pContext->szLinkName[nLength-1] != L'\\') 00355 { 00356 pContext->szLinkName[nLength] = L'\\'; 00357 pContext->szLinkName[nLength+1] = L'\0'; 00358 } 00359 00360 00361 /* Create the Welcome page */ 00362 psp.dwSize = sizeof(PROPSHEETPAGE); 00363 psp.dwFlags = PSP_DEFAULT | PSP_HIDEHEADER; 00364 psp.hInstance = hApplet; 00365 psp.pfnDlgProc = WelcomeDlgProc; 00366 psp.pszTemplate = MAKEINTRESOURCE(IDD_SHORTCUT_LOCATION); 00367 psp.lParam = (LPARAM)pContext; 00368 ahpsp[nPages++] = CreatePropertySheetPage(&psp); 00369 00370 /* Create the Finish page */ 00371 psp.dwFlags = PSP_DEFAULT | PSP_HIDEHEADER; 00372 psp.pfnDlgProc = FinishDlgProc; 00373 psp.pszTemplate = MAKEINTRESOURCE(IDD_SHORTCUT_FINISH); 00374 ahpsp[nPages++] = CreatePropertySheetPage(&psp); 00375 00376 00377 /* Create the property sheet */ 00378 psh.dwSize = sizeof(PROPSHEETHEADER); 00379 psh.dwFlags = PSH_WIZARD97 | PSH_WATERMARK; 00380 psh.hInstance = hApplet; 00381 psh.hwndParent = NULL; 00382 psh.nPages = nPages; 00383 psh.nStartPage = 0; 00384 psh.phpage = ahpsp; 00385 psh.pszbmWatermark = MAKEINTRESOURCE(IDB_WATERMARK); 00386 00387 /* Display the wizard */ 00388 PropertySheet(&psh); 00389 HeapFree(GetProcessHeap(), 0, pContext); 00390 return TRUE; 00391 } 00392 00393 00394 LONG 00395 CALLBACK 00396 NewLinkHere(HWND hwndCPl, UINT uMsg, LPARAM lParam1, LPARAM lParam2) 00397 { 00398 WCHAR szFile[MAX_PATH]; 00399 00400 if (MultiByteToWideChar(CP_ACP, 0, (LPSTR) lParam1, -1, szFile, MAX_PATH)) 00401 { 00402 return ShowCreateShortcutWizard(hwndCPl, szFile); 00403 } 00404 return -1; 00405 } 00406 00407 00408 LONG 00409 CALLBACK 00410 NewLinkHereW(HWND hwndCPl, UINT uMsg, LPARAM lParam1, LPARAM lParam2) 00411 { 00412 return ShowCreateShortcutWizard(hwndCPl, (LPWSTR) lParam1); 00413 } 00414 00415 LONG 00416 CALLBACK 00417 NewLinkHereA(HWND hwndCPl, UINT uMsg, LPARAM lParam1, LPARAM lParam2) 00418 { 00419 WCHAR szFile[MAX_PATH]; 00420 00421 if (MultiByteToWideChar(CP_ACP, 0, (LPSTR) lParam1, -1, szFile, MAX_PATH)) 00422 { 00423 return ShowCreateShortcutWizard(hwndCPl, szFile); 00424 } 00425 return -1; 00426 } Generated on Sun May 27 2012 04:20:50 for ReactOS by
1.7.6.1
|