BOOL CreateFileShortcut(LPCWSTR lpszFileName, LPCWSTR lpszLnkFileDir, LPCWSTR lpszLnkFileName, LPCWSTR lpszWorkDir, WORD wHotkey, LPCTSTR lpszDescription, int iShowCmd) { if (lpszLnkFileDir == NULL) return FALSE; HRESULT hr; IShellLink *pLink; //IShellLink对象指针 IPersistFile *ppf; //IPersisFil对象指针 //创建IShellLink对象 hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pLink); if (FAILED(hr)) return FALSE; //从IShellLink对象中获取IPersistFile接口 hr = pLink->QueryInterface(IID_IPersistFile, (void**)&ppf); if (FAILED(hr)) { pLink->Release(); return FALSE; } //目标 if (lpszFileName == NULL) pLink->SetPath(_wpgmptr); else pLink->SetPath(lpszFileName); //工作目录 if (lpszWorkDir != NULL) pLink->SetWorkingDirectory(lpszWorkDir); //快捷键 if (wHotkey != 0) pLink->SetHotkey(wHotkey); //备注 if (lpszDescription != NULL) pLink->SetDescription(lpszDescription); //显示方式 pLink->SetShowCmd(iShowCmd); //快捷方式的路径 + 名称 wchar_t szBuffer[MAX_PATH]; if (lpszLnkFileName != NULL) //指定了快捷方式的名称 wsprintf(szBuffer, L"%s\\%s", lpszLnkFileDir, lpszLnkFileName); //保存快捷方式到指定目录下 hr = ppf->Save(szBuffer, TRUE); ppf->Release(); pLink->Release(); return SUCCEEDED(hr); }
int WNInstall_CreateShortCut( LPCSTR pszShortcutFile, LPCSTR pszIconFile, int iconindex, LPCSTR pszExe, LPCSTR pszArg, LPCSTR workingdir, int showmode, int hotkey ) { HRESULT hres; int rv=1; IShellLink *psl; hres=OleInitialize(NULL); if (hres != S_FALSE && hres != S_OK) return rv; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl); if (SUCCEEDED(hres)) { IPersistFile *ppf; hres = psl->QueryInterface(IID_IPersistFile, (void **) &ppf); if (SUCCEEDED(hres)) { WCHAR wsz[1024]; MultiByteToWideChar(CP_ACP, 0, pszShortcutFile, -1, wsz, 1024); hres = psl->SetPath(pszExe); psl->SetWorkingDirectory(workingdir); if (showmode) psl->SetShowCmd(showmode); if (hotkey) psl->SetHotkey((unsigned short)hotkey); if (pszIconFile) psl->SetIconLocation(pszIconFile,iconindex); if (pszArg) { psl->SetArguments(pszArg); } if (SUCCEEDED(hres)) { hres=ppf->Save((const WCHAR*)wsz,TRUE); if (SUCCEEDED(hres)) rv=0; } ppf->Release(); } psl->Release(); } OleUninitialize(); if( rv!=0 ) { MessageBox( 0, pszShortcutFile, "Error creating shortcut", MB_OK ); } return rv; }
// @pymethod |PyIShellLink|SetHotkey|Sets the hot key for a shell link object. PyObject *PyIShellLink::SetHotkey(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm int|wHotkey||The virtual key code is in the low-order byte, and the modifier // flags are in the high-order byte. The modifier flags can be a combination of the // values specified in the description of the <om PyIShellLink::GetHotkey> method. WORD wHotkey; if ( !PyArg_ParseTuple(args, "i:SetHotkey", &wHotkey) ) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pISL->SetHotkey( wHotkey ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); Py_INCREF(Py_None); return Py_None; }
// Creates a shortcut in the startup folder. void SetStartupOptions() { // Required for Win95 CoInitialize(NULL); // Create the COM server IShellLink *pShellLink = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<LPVOID*>(&pShellLink)); if (FAILED(hr)) return; TCHAR szPath[MAX_PATH] = {0}; GetModuleFileName(NULL, szPath, sizeof(szPath)); GetShortPathName(szPath, szPath, sizeof(szPath)); // Set attributes pShellLink->SetPath(szPath); pShellLink->SetDescription(SZ_APPNAME); pShellLink->SetHotkey(0); pShellLink->SetIconLocation(szPath, 0); // Get the IPersistFile interface to save IPersistFile *pPF = NULL; hr = pShellLink->QueryInterface(IID_IPersistFile, reinterpret_cast<LPVOID*>(&pPF)); if (FAILED(hr)) { pShellLink->Release(); return; } LPMALLOC pMalloc; if (SUCCEEDED(SHGetMalloc(&pMalloc))) { LPITEMIDLIST pidl; SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidl); SHGetPathFromIDList(pidl, szPath); pMalloc->Free(pidl); pMalloc->Release(); } // Create a .lnk file TCHAR szLinkFile[MAX_PATH] = {0}; wsprintf(szLinkFile, "%s.lnk", SZ_APPNAME); if (szPath[lstrlen(szPath) - 1] != '\\') lstrcat(szPath, "\\"); lstrcat(szPath, szLinkFile); if (g_bStartWithWindows) { // Save Unicode LNK file WCHAR wszLinkFile[MAX_PATH] = {0}; MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1, wszLinkFile, MAX_PATH); hr = pPF->Save(wszLinkFile, TRUE); if (FAILED(hr)) ShowError(IDS_STARTUP_ERR, MB_ICONHAND); } else { DeleteFile(szPath); } // Clean up pPF->Release(); pShellLink->Release(); CoUninitialize(); }