桌面及开始菜单快捷方式图标的更新

来源:互联网 发布:床单 知乎 编辑:程序博客网 时间:2024/05/16 07:45

        基本思路:找到桌面和开始菜单快捷方式文件路径,然后用指定的图标来更新快捷方式的图标即可。注意当前登录的用户是否是管理员,这与快捷方式的路径有直接的关系,找到路径后才能更新图标。

        相关代码如下:

void UpdateFileOperator::UpdateXXXXXXShortcutIcon(){char printInfo[MAX_PRINT_INFO_LEN] = {0};// 获取当前路径TCHAR achModuleName[MAX_PATH] = _T("");CString strCurPath = _T("");GetModuleFileName( AfxGetInstanceHandle(), achModuleName, sizeof (achModuleName) );strCurPath = achModuleName;strCurPath = strCurPath.Left( strCurPath.ReverseFind('\\') + 1 );// 检查图标文件是否存在char szIconPath[MAX_PATH] = {0};sprintf( szIconPath, "%sTrueLink.ico", strCurPath );if ( _taccess( szIconPath, 0 ) != 0 ) // 图标文件不存在{return;}CoInitialize(NULL); BOOL bIsAdmin = FALSE; // 标记当前登录系统的用户是否是管理员if ( IsAdmin() ) // 判断当前登录系统的用户是否是管理员{bIsAdmin = TRUE;}// 更新桌面快捷方式图标    char szDeskShortCutPath[MAX_PATH] = {0};    char szDeskTargetPath[MAX_PATH] = {0};// 当前登录用户是否是管理员与目标路径有直接的关系int nType = 0;if ( bIsAdmin ){nType = CSIDL_COMMON_DESKTOPDIRECTORY;}else{nType = CSIDL_DESKTOP;}BOOL bRet = GetTargetPath( szDeskTargetPath, nType ); // 获取桌面路径if ( !bRet ){return;}sprintf( szDeskShortCutPath, "%s\\桌面快捷方式1.lnk", szDeskTargetPath ); // 对应桌面快捷方式名称if ( _taccess( szDeskShortCutPath, 0 ) == 0 ){UpdateShortcutIcon( szDeskShortCutPath, szIconPath );}else{sprintf( printInfo, "桌面快捷方式路径:%s不存在\r\n", szDeskShortCutPath );_fileLog.WriteData( printInfo, strlen(printInfo) );}    // 更新开始菜单主程序快捷方式图标    char szStartShortCutPath[MAX_PATH] = {0};    char szStartTargetPath[MAX_PATH] = {0};// 当前登录用户是否是管理员与目标路径有直接的关系if ( bIsAdmin ){nType = CSIDL_COMMON_PROGRAMS;}else{nType = CSIDL_PROGRAMS;}bRet = GetTargetPath( szStartTargetPath, nType ); // 获取开始菜单路径if ( !bRet ){return;}sprintf( szStartShortCutPath, "%s\\XXXXXX\\开始菜单快捷方式2.lnk", szStartTargetPath ); // 对应的快捷方式名称if ( _taccess( szStartShortCutPath, 0 ) == 0 ){UpdateShortcutIcon( szStartShortCutPath, szIconPath );}else{sprintf( printInfo, "开始菜单快捷方式路径:%s不存在\r\n", szStartShortCutPath );_fileLog.WriteData( printInfo, strlen(printInfo) );}// 更新开始菜单卸载快捷方式图标    char szUninstallShortCutPath[MAX_PATH] = {0};sprintf( szUninstallShortCutPath, "%s\\XXXXXXX\\tools\\卸载 XXXXXXX.lnk", szStartTargetPath ); // 对应的快捷方式名称if ( _taccess( szUninstallShortCutPath, 0 ) == 0 ){UpdateShortcutIcon( szUninstallShortCutPath, szIconPath );}else{sprintf( printInfo, "开始菜单快捷方式路径:%s不存在\r\n", szUninstallShortCutPath );_fileLog.WriteData( printInfo, strlen(printInfo) );}CoUninitialize(); }// 获取 桌面、开始->程序组 的路径  BOOL UpdateFileOperator::GetTargetPath( char *pszTargetPath, int nFolder ){  if ( pszTargetPath == NULL ){return FALSE;}LPITEMIDLIST  ppidl;  if ( SHGetSpecialFolderLocation( NULL, nFolder, &ppidl ) == S_OK )  {  BOOL flag = SHGetPathFromIDList( ppidl, pszTargetPath );  CoTaskMemFree(ppidl);  return flag;  }  return FALSE;  } BOOL UpdateFileOperator::UpdateShortcutIcon( char* pszShortcutPath, char* pszIconPath ){if ( pszShortcutPath == NULL || pszIconPath == NULL ){return FALSE;} HRESULT hr;  IShellLink     *pLink;  IPersistFile   *ppf; // 创建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;  }  WCHAR  wsz[MAX_PATH];  // 使用宽字符wmemset( wsz, 0, MAX_PATH );MultiByteToWideChar( CP_ACP, 0, pszShortcutPath, -1, wsz, MAX_PATH );hr = ppf->Load( wsz, STGM_READ ); // 通过快捷方式文件路径打开快捷方式if (  FAILED( hr ) )  {ppf->Release();pLink->Release();  return FALSE;}hr = pLink->Resolve( NULL, SLR_UPDATE );if (  FAILED( hr ) )  {ppf->Release();pLink->Release();  return FALSE;}hr = pLink->SetIconLocation( pszIconPath, 0 );if (  FAILED( hr ) )  {ppf->Release();pLink->Release();  return FALSE;}ppf->Save(wsz, TRUE);ppf->Release();pLink->Release();  return TRUE;}// 当前登录用户是否具有管理员权限 BOOL UpdateFileOperator::IsAdmin()            {      #define ACCESS_READ         0x01    #define ACCESS_WRITE        0x02HANDLE hToken;  DWORD  dwStatus;  DWORD  dwAccessMask;  DWORD  dwAccessDesired;  DWORD  dwACLSize;  DWORD  dwStructureSize = sizeof(PRIVILEGE_SET);  PACL   pACL = NULL;  PSID   psidAdmin = NULL;  BOOL   bReturn = FALSE;  PRIVILEGE_SET       ps;  GENERIC_MAPPING   GenericMapping;  PSECURITY_DESCRIPTOR           psdAdmin                       =   NULL;  SID_IDENTIFIER_AUTHORITY   SystemSidAuthority   =   SECURITY_NT_AUTHORITY;  //   AccessCheck()   requires   an   impersonation   token.  ImpersonateSelf(SecurityImpersonation);  if ( !OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken ) )   {  if ( GetLastError() != ERROR_NO_TOKEN )  {goto LeaveFun;} //   If   the   thread   does   not   have   an   access   token,   we'll    //   examine   the   access   token   associated   with   the   process.  if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )  {goto LeaveFun;}}  if ( !AllocateAndInitializeSid( &SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin ) )  {goto LeaveFun;} psdAdmin = LocalAlloc( LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH );  if ( psdAdmin == NULL)  {goto LeaveFun;}  if ( !InitializeSecurityDescriptor( psdAdmin, SECURITY_DESCRIPTOR_REVISION ) )  {goto LeaveFun;} //   Compute   size   needed   for   the   ACL.  dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidAdmin) - sizeof(DWORD);  //   Allocate   memory   for   ACL.  pACL = (PACL)LocalAlloc(LPTR,   dwACLSize);  if ( pACL == NULL )  {goto LeaveFun;}//   Initialize   the   new   ACL.  if ( !InitializeAcl(pACL,   dwACLSize,   ACL_REVISION2) )  {goto LeaveFun;} dwAccessMask = ACCESS_READ | ACCESS_WRITE;  //   Add   the   access-allowed   ACE   to   the   DACL.  if ( !AddAccessAllowedAce( pACL, ACL_REVISION2, dwAccessMask, psidAdmin ) )  {goto LeaveFun;} //   Set   our   DACL   to   the   SD.  if ( !SetSecurityDescriptorDacl( psdAdmin,   TRUE,   pACL,   FALSE ) )  {goto LeaveFun;} //   AccessCheck   is   sensitive   about   what   is   in   the   SD;   set  //   the   group   and   owner.  SetSecurityDescriptorGroup( psdAdmin, psidAdmin, FALSE );  SetSecurityDescriptorOwner( psdAdmin, psidAdmin, FALSE );  if ( !IsValidSecurityDescriptor( psdAdmin ) )  {goto LeaveFun;} dwAccessDesired = ACCESS_READ;  //    //   Initialize   GenericMapping   structure   even   though   we  //   won't   be   using   generic   rights.  //    GenericMapping.GenericRead = ACCESS_READ;  GenericMapping.GenericWrite = ACCESS_WRITE;  GenericMapping.GenericExecute = 0;  GenericMapping.GenericAll = ACCESS_READ   |   ACCESS_WRITE;  if ( !AccessCheck( psdAdmin, hToken, dwAccessDesired, &GenericMapping, &ps, &dwStructureSize, &dwStatus, &bReturn ) )   {  goto LeaveFun;}  RevertToSelf();    LeaveFun://   Cleanup    if ( pACL != NULL ) {LocalFree(pACL); }if ( psdAdmin != NULL )  {LocalFree(psdAdmin);}if ( psidAdmin != NULL )   {FreeSid(psidAdmin);}return   bReturn;   } 


原创粉丝点击