获取快捷方式的属性

从微软网站上面弄下来的
lpszLinkFile 文件名需要带上扩展名.lnk
比如 C:\\Program files\\QQ.lnk
// ResolveIt - Uses the Shell's IShellLink and IPersistFile interfaces 
//             to retrieve the path and description from an existing shortcut. 
//
// Returns the result of calling the member functions of the interfaces. 
//
// Parameters:
// hwnd         - A handle to the parent window. The Shell uses this window to 
//                display a dialog box if it needs to prompt the user for more 
//                information while resolving the link.
// lpszLinkFile - Address of a buffer that contains the path of the link,
//                including the file name.
// lpszPath     - Address of a buffer that receives the path of the link
                  target, including the file name.
// lpszDesc     - Address of a buffer that receives the description of the 
//                Shell link, stored in the Comment field of the link
//                properties.

#include "stdafx.h"
#include "windows.h"
#include "shobjidl.h"
#include "shlguid.h"
#include "strsafe.h"
                            
HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPWSTR lpszPath, int iPathBufferSize) 
{ 
    HRESULT hres; 
    IShellLink* psl; 
    WCHAR szGotPath[MAX_PATH]; 
    WCHAR szDescription[MAX_PATH]; 
    WIN32_FIND_DATA wfd; 
 
    *lpszPath = 0; // Assume failure 

    CoInitialize(NULL); //用的时候如果没有调用这句需要加上
    // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
    // has already been called. 
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); 
    if (SUCCEEDED(hres)) 
    { 
        IPersistFile* ppf; 
 
        // Get a pointer to the IPersistFile interface. 
        hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 
        
        if (SUCCEEDED(hres)) 
        { 
            WCHAR wsz[MAX_PATH]; 
 
            // Ensure that the string is Unicode. 
            MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH); 
 
            // Add code here to check return value from MultiByteWideChar 
            // for success.
 
            // Load the shortcut. 
            hres = ppf->Load(wsz, STGM_READ); 
            
            if (SUCCEEDED(hres)) 
            { 
                // Resolve the link. 
                hres = psl->Resolve(hwnd, 0); 

                if (SUCCEEDED(hres)) 
                { 
                    // Get the path to the link target. 
                    hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); 

                    if (SUCCEEDED(hres)) 
                    { 
                        // Get the description of the target. 
                        hres = psl->GetDescription(szDescription, MAX_PATH); 

                        if (SUCCEEDED(hres)) 
                        {
                            hres = StringCbCopy(lpszPath, iPathBufferSize, szGotPath);
                            if (SUCCEEDED(hres))
                            {
                                // Handle success
                            }
                            else
                            {
                                // Handle the error
                            }
                        }
                    }
                } 
            } 

            // Release the pointer to the IPersistFile interface. 
            ppf->Release(); 
        } 

        // Release the pointer to the IShellLink interface. 
        psl->Release(); 
    } 
    CoUninitialize();  //释放
    return hres; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android开发中,可以使用ShortcutManager类来创建和管理快捷方式。要创建一个桌面快捷方式,首先需要判断启动器是否支持创建快捷方式。可以使用ShortcutManager的isRequestPinShortcutSupported()方法来进行判断。接下来,可以通过实例化ShortcutInfo对象来设置快捷方式的相关属性,比如应用图标、名称、Intent等。最后,通过requestPinShortcut()方法来请求创建快捷方式。 对于autojs桌面快捷方式的创建,你可以按照以下步骤进行操作: 1. 首先,判断启动器是否支持创建快捷方式。你可以使用以下代码获取ShortcutManager实例,并调用isRequestPinShortcutSupported()方法来判断: ```java ShortcutManager shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE); boolean requestPinShortcutSupported = shortcutManager.isRequestPinShortcutSupported(); ``` 2. 创建一个Intent对象,用于启动autojs应用。你可以使用以下代码来创建Intent: ```java Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.autojs.autojs"); ``` 这里假设autojs应用的包名为"com.autojs.autojs"。 3. 创建一个ShortcutInfo对象,并设置相关属性,包括应用图标、名称和启动Intent等。以下是一个示例代码: ```java ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, "autojs_shortcut") .setIcon(Icon.createWithResource(context, R.drawable.autojs_icon)) .setShortLabel("AutoJS") .setLongLabel("AutoJS") .setIntent(launchIntent) .build(); ``` 这里假设你已经准备好了一张名为"autojs_icon"的应用图标资源。 4. 最后,通过调用requestPinShortcut()方法来请求创建快捷方式: ```java shortcutManager.requestPinShortcut(shortcutInfo, null); ``` 这样,你就可以通过以上步骤来创建一个autojs桌面快捷方式了。请注意,这些代码需要在Android 8.0及以上的版本中才能正常工作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [autojs之桌面快捷方式](https://blog.csdn.net/snailuncle2/article/details/115012177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值