簡體   English   中英

如何使用Win32 API和C語言創建快捷方式

[英]How to create shortcut with Win32 API and c language

我想編寫一個程序,使用c中的win32 API為特定文件創建快捷方式。 我的IDE是visual studio2010。我找到了頁面,但其示例未能編譯並返回許多錯誤。 我也找到了這段代碼,但這總是與Target創建鏈接:“ D:\\ Desktop \\㩣打開湩潤獷湜琅灥摡攮數”,我不知道為什么。 有人可以告訴我為什么Microsoft的示例代碼不起作用,或者第二個示例代碼以中文形狀的語言返回某些內容,並且任何參數的位置都錯誤且恆定嗎? 這是我的MSDN示例代碼:

#include "stdafx.h"
#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"

void _tmain(int argc, TCHAR  *argv[])
{
    CreateLink(argv[1],__argv[2],argv[3]);
}

HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc) 
{ 
    HRESULT hres; 
    IShellLink* psl; 

    // 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; 

        // Set the path to the shortcut target and add the description. 
        psl->SetPath(lpszPathObj); 
        psl->SetDescription(lpszDesc); 

        // Query IShellLink for the IPersistFile interface, used for saving the 
        // shortcut in persistent storage. 
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 

        if (SUCCEEDED(hres)) 
        { 
            WCHAR wsz[MAX_PATH]; 

            // Ensure that the string is Unicode. 
            MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); 

            // Add code here to check return value from MultiByteWideChar 
            // for success.

            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(wsz, TRUE); 
            ppf->Release(); 
        } 
        psl->Release(); 
    } 
    return hres; 
}

錯誤是:1錯誤C1083:無法打開包含文件:“ stdafx.h”和2 IntelliSense:無法打開源文件“ stdafx.h”

 CreateLink(argv[1],__argv[2],argv[3]); 

這個電話看起來很奇怪。 您將argv[]用於兩個LPCWSTRconst wchar_t * )參數,但將__argv[]用於LPCSTRconst char * )參數。 您應該將第二個參數更改為LPCWSTR以匹配其他參數,然后使用argv[]而不是__argv[]

基於TCHARIShellLinkLP(C)WSTR字符串參數一起使用,並且在編譯Unicode時, LP(C)TSTRLP(C)WSTR 考慮到您正在將基於TCHARargv[]值傳遞給LPCWSTR參數,顯然,這是在做的,僅當TCHARwchar_t時才編譯。

IPersistFile::Save()僅將Unicode字符串作為輸入,而不管TCHAR映射到什么。 __argv[] char*值從__argv[]從ANSI轉換為Unicode,因此也可以從argv[]開始獲得Unicode字符串,然后完全省略對MultiByteToWideChar()的調用。

沒有充分的理由混合這樣的ANSI和Unicode字符串。 這是MSDN示例出錯的地方。

並且由於函數參數正在使用Unicode字符串,因此應直接使用IShellLinkW接口,而不是基於TCHARIShellLink接口。

嘗試這個:

#include "stdafx.h"
#include "windows.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"

HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszPathLink, LPCWSTR lpszDesc) 
{ 
    HRESULT hres; 
    IShellLinkW* psl; 

    // 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_IShellLinkW, (LPVOID*)&psl); 
    if (SUCCEEDED(hres)) 
    { 
        IPersistFile* ppf; 

        // Set the path to the shortcut target and add the description. 
        psl->SetPath(lpszPathObj); 
        psl->SetDescription(lpszDesc); 

        // Query IShellLink for the IPersistFile interface, used for saving the 
        // shortcut in persistent storage. 
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); 

        if (SUCCEEDED(hres)) 
        { 
            // Save the link by calling IPersistFile::Save. 
            hres = ppf->Save(lpszPathLink, TRUE); 
            ppf->Release(); 
        } 

        psl->Release(); 
    } 

    return hres; 
}

void _tmain(int argc, TCHAR *argv[])
{
    if (argc > 3)
        CreateLink(argv[1], argv[2], argv[3]);
}

使用新的項目向導時,Visual Studio會生成stdafx.hstdafx.cpp 如果標記了創建空項目復選標記,它將不會生成它們。 這些文件用於構建預編譯的頭文件Projname.pch和預編譯的類型文件Stdafx.obj。

對於小型項目,您最終可以刪除#include“ stdafx.h” ,但是最好創建帶有未標記的創建空項目的新項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM