无法创建所有用户启动的快捷方式

时间:2012-10-27 20:09:51

标签: c++ winapi

我正在使用一些基本代码在我的程序的安装程序中使用。为了让我的程序在启动时运行,我在All Users Startup文件夹中创建了一个.lnk。

我已经创建了一个快捷方式的代码,但我一直收到错误代码5(priv错误)。我尝试右键单击并以管理员身份运行此代码,但它不起作用,返回错误代码5.我还使用代码在文件系统的其他部分创建快捷方式,没有任何问题。

我还使用了第三方程序,主要是“elevate.exe”来运行我的程序,但仍然无效,它继续返回错误代码5.

我的问题是,如何在不使用UAC的情况下使用下面的代码在All User的Startup文件夹中创建快捷方式?

 int main () 
 {

 CoInitialize(NULL);

 IShellLink* pShellLink = NULL;

 HRESULT hres;

 hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink,   (void**)&pShellLink);

  if (SUCCEEDED(hres))

  {
    pShellLink->SetPath("C:\\Windows\\System32\\testinstall\\test.exe");  // Path to the object we are referring to

    pShellLink->SetDescription("This is a test");

    pShellLink->SetIconLocation("C:\\Windows\\System32\\testinstall\\test.exe", 0);

    IPersistFile *pPersistFile;

    hres = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);

    if (SUCCEEDED(hres))

    {
        hres = pPersistFile->Save(L"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup", TRUE);

        pPersistFile->Release();
    }

    else
    {
        cout << "Error 2" << endl;

        return 2;
    }

    pShellLink->Release();

    }

    else
    {

    cout << "Error 1" << endl;

    return 1;

    }

    cout << GetLastError ();

    _getch ();

    return 0;

    }

0 个答案:

没有答案