📄 shortcut.cpp
字号:
szShortcutPathA = szTmp1 ;
}
// 僼傽僀儖柤傪僼儖僷僗柤偵
if ( ! IsFullPathNameA ( szShortcutPathA ) ) {
szShortcutPathA = szTmp2 = GetFullPathNameAllocA ( szShortcutPathA ) ;
if ( ! szTmp2 ) { nResult = 3 ; goto CAUGHT_ERROR ; }
}
// 僼傽僀儖柤傪儚僀僪暥帤偵
szShortcutPathW = a2wdup ( szShortcutPathA ) ;
if ( ! szShortcutPathW ) { nResult = 3 ; goto CAUGHT_ERROR ; }
// URL 偺応崌
if ( IsLnkOrUrl == IS_LINK_URL ) {
IUniformResourceLocatorA *pIShellLink ;
IPersistFile *pIPersistFile ;
if ( SUCCEEDED ( CoInitialize ( NULL ) ) ) {
if ( SUCCEEDED ( CoCreateInstance ( CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocatorA, (void**) & pIShellLink ) ) ) {
if ( SUCCEEDED ( pIShellLink->QueryInterface ( IID_IPersistFile, (void**) & pIPersistFile ) ) ) {
if ( SUCCEEDED ( pIPersistFile->Load ( szShortcutPathW, STGM_READ ) ) ) {
nResult = 0 ;
if ( szDestinationPath ) GetUrlDestinationPathA ( pIShellLink, szDestinationPath ) ;
if ( szArguments ) *szArguments = 0 ;
if ( szDirectory ) *szDirectory = 0 ;
if ( szDescription ) *szDescription = 0 ;
if ( szIconPath ) *szIconPath = 0 ;
if ( pnShowCommand ) *pnShowCommand = 0 ;
if ( pwHotKey ) *pwHotKey = 0 ;
if ( ppItemIdList ) *ppItemIdList = NULL ;
if ( szSavedShortcutPath ) strcpy ( szSavedShortcutPath, szShortcutPathA ) ;
}
}
pIShellLink->Release () ;
}
CoUninitialize () ;
}
}
// 僼傽僀儖偺応崌
else {
IShellLinkA *pIShellLink ;
IPersistFile *pIPersistFile ;
WIN32_FIND_DATAA fData ;
if ( SUCCEEDED ( CoInitialize ( NULL ) ) ) {
if ( SUCCEEDED ( CoCreateInstance ( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (void**) & pIShellLink ) ) ) {
if ( SUCCEEDED ( pIShellLink->QueryInterface ( IID_IPersistFile, (void**) & pIPersistFile ) ) ) {
if ( SUCCEEDED ( pIPersistFile->Load ( szShortcutPathW, STGM_READ ) ) ) {
nResult = 0 ;
int nIconIndexTmp = 0 ;
if ( szDestinationPath ) pIShellLink->GetPath ( szDestinationPath, MAX_PATH, & fData, SLGP_UNCPRIORITY ) ;
if ( szArguments ) pIShellLink->GetArguments ( szArguments, MAX_PATH ) ;
if ( szDirectory ) pIShellLink->GetWorkingDirectory ( szDirectory, MAX_PATH ) ;
if ( szDescription ) pIShellLink->GetDescription ( szDescription, MAX_PATH ) ;
if ( szIconPath ) pIShellLink->GetIconLocation ( szIconPath, MAX_PATH, & nIconIndexTmp ) ;
if ( pnIconIndex ) *pnIconIndex = nIconIndexTmp ;
if ( pnShowCommand ) pIShellLink->GetShowCmd ( pnShowCommand ) ;
if ( pwHotKey ) pIShellLink->GetHotkey ( pwHotKey ) ;
if ( ppItemIdList ) pIShellLink->GetIDList ( ppItemIdList ) ;
if ( szSavedShortcutPath ) strcpy ( szSavedShortcutPath, szShortcutPathA ) ;
}
pIPersistFile->Release () ;
}
pIShellLink->Release () ;
}
CoUninitialize () ;
}
if ( szDestinationPath ) {
char szDstTmp [ MAX_PATH ] ;
if ( ! GetMsiShortcutA ( szShortcutPathA, szDstTmp, NULL, NULL, NULL ) ) strcpy ( szDestinationPath, szDstTmp ) ;
}
}
}
CAUGHT_ERROR :
free ( szTmp1 ) ;
free ( szTmp2 ) ;
free ( szShortcutPathW ) ;
return nResult ;
}
static int GetUrlDestinationPathA ( IUniformResourceLocatorA *pIShellLink, char *szPath ) {
char *pTmp ;
if ( SUCCEEDED ( pIShellLink->GetURL ( & pTmp ) ) ) {
strcpy ( szPath, pTmp ) ;
ShFree ( pTmp ) ;
return 0 ;
}
return 1 ;
}
/////////////////////////////////////////////
// 僔儑乕僩僇僢僩偺嶌惉乮UNICODE斉乯 //
/////////////////////////////////////////////
static int GetSavedShortcurPathW ( IPersistFile *pIPersistFile, wchar_t *szPath ) ;
// 僔儑乕僩僇僢僩傪嶌惉偡傞乮UNICODE斉乯
// 堷偒悢偺偆偪嵟弶偺俀偮埲奜偼 NULL 偱傕壜
// 僼傽僀儖柤偼憡懳僷僗柤偱傕壜
// 僔儑乕僩僇僢僩柤偺奼挘巕 .lnk/.pif/.url 偼桳柍偳偪傜傕壜
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪傪曉偡
int WINAPI CreateShortcutW ( const wchar_t *szShortcutPathW,
const wchar_t *szDestinationPath,
const wchar_t *szArguments,
const wchar_t *szDirectory,
const wchar_t *szDescription,
const wchar_t *szIconPath,
int nIconIndex,
int nShowCommand,
unsigned short wHotKey,
const ITEMIDLIST *pItemIdList,
wchar_t *szSavedShortcutPath ) {
// 堷偒悢専嵏
if ( ! szShortcutPathW ) return 1 ;
if ( ! szDestinationPath && ! pItemIdList ) return 1 ;
int nResult = 1 ;
wchar_t *szTmp1 = NULL, *szTmp2 = NULL, *szTmp3 = NULL ;
if ( 1 ) {
// 奼挘巕傪悇應
int IsLnkOrUrl = IS_LINK_FILESYSTEM ;
if ( ! pItemIdList && IsFileNameUrlW ( szDestinationPath ) ) IsLnkOrUrl = IS_LINK_URL ;
const wchar_t *szExtPreferable ; // 僪僢僩晅偒
if ( IsLnkOrUrl == IS_LINK_URL ) szExtPreferable = szExtensionUrlW ;
else {
const wchar_t *szExtension = GetExtensionW ( szDestinationPath ) ;
if ( szExtension && ! wcsicmp ( szExtension, L"com" ) ) szExtPreferable = szExtensionPifW ;
else szExtPreferable = szExtensionLnkW ;
}
// 奼挘巕傪専嵏
const wchar_t *szExtension = GetExtensionW ( szShortcutPathW ) ;
if ( ! ( szExtension && ! wcsicmp ( szExtension, szExtPreferable + 1 ) ) ) {
if ( ! ( szTmp1 = (wchar_t*) malloc ( ( wcslen ( szShortcutPathW ) + wcslen ( szExtPreferable ) + 1 ) * sizeof(wchar_t) ) ) ) { nResult = 3 ; goto CAUGHT_ERROR ; }
wcscpy ( szTmp1, szShortcutPathW ) ;
wcscat ( szTmp1, szExtPreferable ) ;
szShortcutPathW = szTmp1 ;
}
// 僼傽僀儖偺懚嵼傪専嵏
unsigned long dwFileAttributes = GetFileAttributesW ( szShortcutPathW ) ;
if ( dwFileAttributes != (unsigned long) -1 ) {
if ( dwFileAttributes & FILE_ATTRIBUTE_READONLY ) { nResult = 2 ; goto CAUGHT_ERROR ; }
if ( ! DeleteFileW ( szShortcutPathW ) ) { nResult = 2 ; goto CAUGHT_ERROR ; }
}
// 僼傽僀儖柤傪僼儖僷僗柤偵
if ( ! IsFullPathNameW ( szShortcutPathW ) ) {
szShortcutPathW = szTmp2 = GetFullPathNameAllocW ( szShortcutPathW ) ;
if ( ! szTmp2 ) { nResult = 3 ; goto CAUGHT_ERROR ; }
}
// 儕儞僋愭傪僼儖僷僗柤偵
if ( IsLnkOrUrl == IS_LINK_FILESYSTEM && ! pItemIdList && ! IsFullPathNameW ( szDestinationPath ) ) {
szDestinationPath = szTmp3 = GetFullPathNameAllocW ( szDestinationPath ) ;
if ( ! szTmp3 ) { nResult = 3 ; goto CAUGHT_ERROR ; }
}
// URL 偺応崌
if ( IsLnkOrUrl == IS_LINK_URL ) {
IUniformResourceLocatorW *pIShellLink ;
IPersistFile *pIPersistFile ;
if ( SUCCEEDED ( CoInitialize ( NULL ) ) ) {
if ( SUCCEEDED ( CoCreateInstance ( CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocatorW, (void**) & pIShellLink ) ) ) {
if ( SUCCEEDED ( pIShellLink->QueryInterface ( IID_IPersistFile, (void**) & pIPersistFile ) ) ) {
pIShellLink->SetURL ( szDestinationPath, 0 ) ;
if ( SUCCEEDED ( pIPersistFile->Save ( szShortcutPathW, TRUE ) ) ) {
nResult = 0 ;
if ( szSavedShortcutPath ) GetSavedShortcurPathW ( pIPersistFile, szSavedShortcutPath ) ;
}
pIPersistFile->Release () ;
}
pIShellLink->Release () ;
}
CoUninitialize () ;
}
}
// 僼傽僀儖偺応崌
else {
IShellLinkW *pIShellLink ;
IPersistFile *pIPersistFile ;
if ( SUCCEEDED ( CoInitialize ( NULL ) ) ) {
if ( SUCCEEDED ( CoCreateInstance ( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW, (void**) & pIShellLink ) ) ) {
if ( SUCCEEDED ( pIShellLink->QueryInterface ( IID_IPersistFile, (void**) & pIPersistFile ) ) ) {
if ( pItemIdList ) pIShellLink->SetIDList ( pItemIdList ) ;
else pIShellLink->SetPath ( szDestinationPath ) ;
if ( szArguments ) pIShellLink->SetArguments ( szArguments ) ;
if ( szDirectory ) pIShellLink->SetWorkingDirectory ( szDirectory ) ;
if ( szDescription ) pIShellLink->SetDescription ( szDescription ) ;
if ( szIconPath ) pIShellLink->SetIconLocation ( szIconPath, nIconIndex ) ;
if ( nShowCommand ) pIShellLink->SetShowCmd ( nShowCommand ) ;
if ( wHotKey ) pIShellLink->SetHotkey ( wHotKey ) ;
if ( SUCCEEDED ( pIPersistFile->Save ( szShortcutPathW, TRUE ) ) ) {
nResult = 0 ;
if ( szSavedShortcutPath ) GetSavedShortcurPathW ( pIPersistFile, szSavedShortcutPath ) ;
}
pIPersistFile->Release () ;
}
pIShellLink->Release () ;
}
CoUninitialize () ;
}
}
}
CAUGHT_ERROR :
free ( szTmp1 ) ;
free ( szTmp2 ) ;
free ( szTmp3 ) ;
return nResult ;
}
static int GetSavedShortcurPathW ( IPersistFile *pIPersistFile, wchar_t *szPath ) {
wchar_t *pTmp ;
if ( SUCCEEDED ( pIPersistFile->GetCurFile ( & pTmp ) ) ) {
wcscpy ( szPath, pTmp ) ;
ShFree ( pTmp ) ;
return 0 ;
}
return 1 ;
}
/////////////////////////////////////////////
// 僔儑乕僩僇僢僩偺庢摼乮UNICODE斉乯 //
/////////////////////////////////////////////
static int GetUrlDestinationPathW ( IUniformResourceLocatorW *pIShellLink, wchar_t *szPath ) ;
static int GetMsiShortcutW ( const wchar_t *szShortcutPath, wchar_t *szDestinationPath, wchar_t *szProductCode, wchar_t *szFeatureId, wchar_t *szComponentCode ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -