MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / FileCreateShortcut

Method FileCreateShortcut

source/script_autoit.cpp:1246–1302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1244
1245
1246ResultType Line::FileCreateShortcut(LPTSTR aTargetFile, LPTSTR aShortcutFile, LPTSTR aWorkingDir, LPTSTR aArgs
1247 , LPTSTR aDescription, LPTSTR aIconFile, LPTSTR aHotkey, LPTSTR aIconNumber, LPTSTR aRunState)
1248{
1249 bool bSucceeded = false;
1250 CoInitialize(NULL);
1251 IShellLink *psl;
1252
1253 if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&psl)))
1254 {
1255 psl->SetPath(aTargetFile);
1256 if (*aWorkingDir)
1257 psl->SetWorkingDirectory(aWorkingDir);
1258 if (*aArgs)
1259 psl->SetArguments(aArgs);
1260 if (*aDescription)
1261 psl->SetDescription(aDescription);
1262 int icon_index = *aIconNumber ? ATOI(aIconNumber) : 0;
1263 if (*aIconFile)
1264 psl->SetIconLocation(aIconFile, icon_index - (icon_index > 0 ? 1 : 0)); // Convert 1-based index to 0-based, but leave negative resource IDs as-is.
1265 if (*aHotkey)
1266 {
1267 // If badly formatted, it's not a critical error, just continue.
1268 // Currently, only shortcuts with a CTRL+ALT are supported.
1269 // AutoIt3 note: Make sure that CTRL+ALT is selected (otherwise invalid)
1270 vk_type vk = TextToVK(aHotkey);
1271 if (vk)
1272 // Vk in low 8 bits, mods in high 8:
1273 psl->SetHotkey( (WORD)vk | ((WORD)(HOTKEYF_CONTROL | HOTKEYF_ALT) << 8) );
1274 }
1275 if (*aRunState)
1276 psl->SetShowCmd(ATOI(aRunState)); // No validation is done since there's a chance other numbers might be valid now or in the future.
1277
1278 IPersistFile *ppf;
1279 if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile,(LPVOID *)&ppf)))
1280 {
1281#ifndef UNICODE
1282 WCHAR wsz[MAX_PATH];
1283 ToWideChar(aShortcutFile, wsz, MAX_PATH); // Dest. size is in wchars, not bytes.
1284#else
1285 LPCWSTR wsz = aShortcutFile;
1286#endif
1287 // MSDN says to pass "The absolute path of the file". Windows 10 requires it.
1288 WCHAR full_path[MAX_PATH];
1289 GetFullPathNameW(wsz, _countof(full_path), full_path, NULL);
1290 if (SUCCEEDED(ppf->Save(full_path, TRUE)))
1291 bSucceeded = true;
1292 ppf->Release();
1293 }
1294 psl->Release();
1295 }
1296
1297 CoUninitialize();
1298 if (bSucceeded)
1299 return OK;
1300 else
1301 return Throw();
1302}
1303

Callers

nothing calls this directly

Calls 4

ATOIFunction · 0.85
TextToVKFunction · 0.85
QueryInterfaceMethod · 0.80
ReleaseMethod · 0.45

Tested by

no test coverage detected