⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shortcut.cpp

📁 This software performs code conversion of Chinese characters, including GB2312/GBK and BIG5. It a
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// shortcut.cpp

#include <windows.h>
#include <shlobj.h>
#include <intshcut.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "msc.h"
#include "filename.h"
#include "shortcut.h"
#include "stringex.h"


// for IsLnkOrUrl
#define IS_LINK_UNKNOWN       0
#define IS_LINK_FILESYSTEM    1
#define IS_LINK_URL           2


// IMalloc
#define ShAlloc   CoTaskMemAlloc
#define ShFree    CoTaskMemFree


// 婛掕奼挘巕
#define szExtensionLnkA     ".lnk"
#define szExtensionLnkW    L".lnk"
#define szExtensionPifA     ".pif"
#define szExtensionPifW    L".pif"
#define szExtensionUrlA     ".url"
#define szExtensionUrlW    L".url"



// 儂僢僩僉乕偺巜掕偼
// 忋埵僶僀僩偵摿庩僉乕僐乕僪 HOTKEYF_ALT 側偳 乯
// 壓埵僶僀僩偵壖憐僉乕僐乕僪 VK_UP 側偳乯



/////////////////////////////////////////////
//      僔儑乕僩僇僢僩偺嶌惉乮ANSI斉乯     //
/////////////////////////////////////////////



static int GetSavedShortcurPathA ( IPersistFile *pIPersistFile, char *szPath ) ;



// 僔儑乕僩僇僢僩傪嶌惉偡傞乮ANSI斉乯
// 堷偒悢偺偆偪嵟弶偺俀偮埲奜偼 NULL 偱傕壜
// 僼傽僀儖柤偼憡懳僷僗柤偱傕壜
// 僔儑乕僩僇僢僩柤偺奼挘巕 .lnk/.pif/.url 偼桳柍偳偪傜傕壜
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪傪曉偡
int WINAPI CreateShortcutA ( const char *szShortcutPathA,
                             const char *szDestinationPath,
                             const char *szArguments,
                             const char *szDirectory,
                             const char *szDescription,
                             const char *szIconPath,
                             int  nIconIndex,
                             int  nShowCommand,
                             unsigned short wHotKey,
                             const ITEMIDLIST *pItemIdList,
                             char *szSavedShortcutPath ) {

   // 堷偒悢専嵏
   if ( ! szShortcutPathA ) return 1 ;
   if ( ! szDestinationPath && ! pItemIdList ) return 1 ;


   int nResult = 1 ;
   char *szTmp1 = NULL, *szTmp2 = NULL, *szTmp3 = NULL ;
   wchar_t *szShortcutPathW = NULL ;


   if ( 1 ) {

      // 奼挘巕傪悇應
      int IsLnkOrUrl = IS_LINK_FILESYSTEM ;
      if ( ! pItemIdList && IsFileNameUrlA ( szDestinationPath ) ) IsLnkOrUrl = IS_LINK_URL ;

      const char *szExtPreferable ; // 僪僢僩晅偒
      if ( IsLnkOrUrl == IS_LINK_URL ) szExtPreferable = szExtensionUrlA ;
      else {
         const char *szExtension = GetExtensionA ( szDestinationPath ) ;
         if ( szExtension && ! stricmp ( szExtension, "com" ) ) szExtPreferable = szExtensionPifA ;
         else                                                   szExtPreferable = szExtensionLnkA ;
      }

      // 奼挘巕傪専嵏
      const char *szExtension = GetExtensionA ( szShortcutPathA ) ;
      if ( ! ( szExtension && ! stricmp ( szExtension, szExtPreferable + 1 ) ) ) {
         if ( ! ( szTmp1 = (char*) malloc ( ( strlen ( szShortcutPathA ) + strlen ( szExtPreferable ) + 1 ) * sizeof(char) ) ) ) { nResult = 3 ; goto CAUGHT_ERROR ; }
         strcpy ( szTmp1, szShortcutPathA ) ;
         strcat ( szTmp1, szExtPreferable ) ;
         szShortcutPathA = szTmp1 ;
      }

      // 僼傽僀儖偺懚嵼傪専嵏
      unsigned long dwFileAttributes = GetFileAttributesA ( szShortcutPathA ) ;
      if ( dwFileAttributes != (unsigned long) -1 ) {
         if ( dwFileAttributes & FILE_ATTRIBUTE_READONLY ) { nResult = 2 ; goto CAUGHT_ERROR ; }
         if ( ! DeleteFileA ( szShortcutPathA ) ) { nResult = 2 ; goto CAUGHT_ERROR ; }
      }

      // 僼傽僀儖柤傪僼儖僷僗柤偵
      if ( ! IsFullPathNameA ( szShortcutPathA ) ) {
         szShortcutPathA = szTmp2 = GetFullPathNameAllocA ( szShortcutPathA ) ;
         if ( ! szTmp2 ) { nResult = 3 ; goto CAUGHT_ERROR ; }
      }

      // 儕儞僋愭傪僼儖僷僗柤偵
      if ( IsLnkOrUrl == IS_LINK_FILESYSTEM && ! pItemIdList && ! IsFullPathNameA ( szDestinationPath ) ) {
         szDestinationPath = szTmp3 = GetFullPathNameAllocA ( szDestinationPath ) ;
         if ( ! szTmp3 ) { 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 ) ) ) {
                  pIShellLink->SetURL ( szDestinationPath, 0 ) ;
                  if ( SUCCEEDED ( pIPersistFile->Save ( szShortcutPathW, TRUE ) ) ) {
                     nResult = 0 ;
                     if ( szSavedShortcutPath ) GetSavedShortcurPathA ( pIPersistFile, szSavedShortcutPath ) ;
                  }
                  pIPersistFile->Release () ;
               }
               pIShellLink->Release () ;
            }
            CoUninitialize () ;
         }
      }
      // 僼傽僀儖偺応崌
      else {

         IShellLinkA *pIShellLink ;
         IPersistFile *pIPersistFile ;

         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 ( 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 ) GetSavedShortcurPathA ( pIPersistFile, szSavedShortcutPath ) ;
                  }
                  pIPersistFile->Release () ;
               }
               pIShellLink->Release () ;
            }
            CoUninitialize () ;
         }
      }

   }

   CAUGHT_ERROR :

   free ( szTmp1 ) ;
   free ( szTmp2 ) ;
   free ( szTmp3 ) ;
   free ( szShortcutPathW ) ;

   return nResult ;
}



static int GetSavedShortcurPathA ( IPersistFile *pIPersistFile, char *szPath ) {
   wchar_t *pTmp ;
   if ( SUCCEEDED ( pIPersistFile->GetCurFile ( & pTmp ) ) ) {
      WideCharToMultiByte ( CP_ACP, 0, pTmp, -1, szPath, MAX_PATH, NULL, NULL ) ;
      ShFree ( pTmp ) ;
      return 0 ;
   }
   return 1 ;
}



/////////////////////////////////////////////
//      僔儑乕僩僇僢僩偺庢摼乮ANSI斉乯     //
/////////////////////////////////////////////



static int GetUrlDestinationPathA ( IUniformResourceLocatorA *pIShellLink, char *szPath ) ;
static int GetMsiShortcutA ( const char *szShortcutPath, char *szDestinationPath, char *szProductCode, char *szFeatureId, char *szComponentCode ) ;



// 僔儑乕僩僇僢僩偺撪梕傪庢摼偡傞乮ANSI斉乯
// 堷偒悢偺偆偪 szShortcutPathA 埲奜偼 NULL 偱傕壜
// 僔儑乕僩僇僢僩柤偺奼挘巕 .lnk/.pif/.url 偼桳柍偳偪傜傕壜
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪曉偡
int WINAPI GetShortcutA ( const char *szShortcutPathA,
                          char *szDestinationPath,
                          char *szArguments,
                          char *szDirectory,
                          char *szDescription,
                          char *szIconPath,
                          int  *pnIconIndex,
                          int  *pnShowCommand,
                          unsigned short *pwHotKey,
                          ITEMIDLIST **ppItemIdList,
                          char *szSavedShortcutPath ) {

   // 堷偒悢専嵏
   if ( ! szShortcutPathA ) return 1 ;


   int nResult = 1 ;
   char *szTmp1 = NULL, *szTmp2 = NULL ;
   wchar_t *szShortcutPathW = NULL ;


   if ( 1 ) {

      // 僼傽僀儖懚嵼専嵏
      int IsLnkOrUrl = IS_LINK_UNKNOWN ;
      const char *szExtension = GetExtensionA ( szShortcutPathA ) ;
      if ( szExtension && GetFileAttributesA ( szShortcutPathA ) != (unsigned long) -1 ) {
         if ( ! stricmp ( szExtension, szExtensionLnkA + 1 ) ) IsLnkOrUrl = IS_LINK_FILESYSTEM ;
         if ( ! stricmp ( szExtension, szExtensionPifA + 1 ) ) IsLnkOrUrl = IS_LINK_FILESYSTEM ;
         if ( ! stricmp ( szExtension, szExtensionUrlA + 1 ) ) IsLnkOrUrl = IS_LINK_URL ;
      }
      if ( IsLnkOrUrl == IS_LINK_UNKNOWN ) {
         if ( ! ( szTmp1 = (char*) malloc ( ( strlen ( szShortcutPathA ) + 0x10 ) * sizeof(char) ) ) ) { nResult = 3 ; goto CAUGHT_ERROR ; }
         if ( IsLnkOrUrl == IS_LINK_UNKNOWN ) {
            strcpy ( szTmp1, szShortcutPathA ) ;
            strcat ( szTmp1, szExtensionLnkA ) ;
            if ( GetFileAttributesA ( szTmp1 ) != (unsigned long) -1 ) IsLnkOrUrl = IS_LINK_FILESYSTEM ;
         }
         if ( IsLnkOrUrl == IS_LINK_UNKNOWN ) {
            strcpy ( szTmp1, szShortcutPathA ) ;
            strcat ( szTmp1, szExtensionPifA ) ;
            if ( GetFileAttributesA ( szTmp1 ) != (unsigned long) -1 ) IsLnkOrUrl = IS_LINK_FILESYSTEM ;
         }
         if ( IsLnkOrUrl == IS_LINK_UNKNOWN ) {
            strcpy ( szTmp1, szShortcutPathA ) ;
            strcat ( szTmp1, szExtensionUrlA ) ;
            if ( GetFileAttributesA ( szTmp1 ) != (unsigned long) -1 ) IsLnkOrUrl = IS_LINK_URL ;
         }
         if ( IsLnkOrUrl == IS_LINK_UNKNOWN ) { nResult = 2 ; goto CAUGHT_ERROR ; }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -