尝试创建快捷方式时出错[C ++]

时间:2013-10-29 12:56:32

标签: c++ compilation shortcut

我有这些包括:

#include <stdafx.h>
#include <winnls.h>
#include <shobjidl.h>
#include <shlobj.h> 
#include <shlguid.h>
#include <objbase.h>
#include <objidl.h>
#include <windows.h>

我找到了这段代码来制作快捷方式:

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

但是我收到了这些编译错误: “没有用于调用'IShellLinkA :: SetPath(const WCHAR *&amp;)'候选者的匹配函数是:virtual HRESULT IShellLinkA :: SetPath(const CHAR *)”

我为setDescription得到了同样的错误,显然它们被编程为Virtual,这意味着它们中没有真正的代码:

virtual HRESULT WINAPI SetPath(LPCSTR pszFile) = 0;

我必须丢失头文件或其他东西,但我不知道。我有点失落,我认为不应该得到这些错误。有任何想法吗? 非常感谢你: - )

2 个答案:

答案 0 :(得分:0)

将项目设置从Use Multi-Byte Character Set更改为Use Unicode Character Set。此选项位于“项目默认值”下的“常规”配置设置中。

答案 1 :(得分:0)

使用

psl->SetPath(CW2T(lpszPathObj)); 

如果您有MBCS项目。在这种情况下,SetPath需要一个char *。 即使您将项目切换为Unicode,此表示法也将始终有效。