2

この win32 ベースの C プログラムを使用して、ショートカットのターゲットを
["C:\Program Files\YP\YPseries\Bin\myexe.exe"] から
["C:\Program Files\YP\YPseries\Bin\ ] に変更しています。 myexe.exe" -Start UDCDevicePage]
角かっこを除きます。

ただし、
WCHAR newTargetPath[] = L"\"C:\Program Files\YP\YP series\Bin\myexe.exe\" -Start UDCDevicePage"; を使用すると、
メインでは、SetPath は E_INVALIDARG エラー コードを返します。

IShellLink::SetPath 関数を使用して myexe に引数を渡すにはどうすればよいですか?

プログラムを以下に示します。

HRESULT changeLinkTarget(LPCSTR pathLink, LPWSTR newTargetPath) 
{ 
    HRESULT hres; 
    IShellLink* psl; 
    WCHAR szGotPath[MAX_PATH]; 
    WIN32_FIND_DATA wfd; 

    // Get a pointer to the IShellLink interface.
    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, pathLink, -1, wsz, MAX_PATH); 

            // Load the shortcut. 
            hres = ppf->Load(wsz, STGM_READ); 

            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))
                {
                    hres = psl->SetPath(newTargetPath);
                    hres = ppf->Save(wsz, TRUE); //save changes
                }
                else
                {
                    // Handle the error
                }

            } 
            // Release the pointer to the IPersistFile interface. 
            ppf->Release(); 
        } 
        // Release the pointer to the IShellLink interface. 
        psl->Release(); 
    } 
    return hres; 
}



int _tmain(int argc, _TCHAR* argv[])
{
    char linkPath[128] = "C:\\Users\\Public\\Desktop\\YP  series.lnk";
    WCHAR newTargetPath[] = L"\"C:\\Program Files\\YP\\YP  series\\Bin\\myexe.exe\" -Start UDCDevicePage";

    CoInitialize(NULL); // initialize the COM subsystem
    HRESULT ret = changeLinkTarget(linkPath, newTargetPath);


    return 0;
}
4

1 に答える 1

3

パスは通常、単なる exe です。コマンドライン引数を使用してパスを実行可能ファイルに設定しようとしているようです。代わりに、余分な引用符なしで exe だけに SetPath を使用してみてください。また、コマンド ライン パラメータにはIShellLink::SetArguments()を使用してください。SetPath はおそらく、渡した完全な文字列の名前を持つ exe が存在することを確認することによってパラメーターを検証しようとしていますが、これが失敗してエラーが発生している可能性があります。

于 2012-02-13T12:22:01.763 に答える