1

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

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

我还使用了 3rd 方程序,主要是“elevate.exe”来运行我的程序,但仍然无法正常工作,它继续返回错误代码 5。

我的问题是,如何使用下面的代码在没有 UAC 的情况下在所有用户的启动文件夹中创建快捷方式?

 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;

    }
4

0 回答 0