VC 如何创建自身快捷方式

#include <shobjidl.h>
#include <shlguid.h>
#include <shlobj.h>

void main()
{
 HRESULT hr = CoInitialize(NULL);
 
 if (SUCCEEDED(hr))
 {
  IShellLink *pisl;

  hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (void**)&pisl);
  
  if (SUCCEEDED(hr))
  {
   IPersistFile* pIPF;

   char path[MAX_PATH];

   GetModuleFileName(GetModuleHandle(NULL), path, MAX_PATH);
   pisl->SetPath(path);
   hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
   
   if (SUCCEEDED(hr))
   {
    char lnk[MAX_PATH];

    SHGetSpecialFolderPath(0, lnk, CSIDL_DESKTOPDIRECTORY, 0);
    strcat(lnk, TEXT("\\"));

    WCHAR wpath[MAX_PATH] = { 0 };

    strcat(lnk, strtok(strrchr(path, '\\'), "\\"));
    strcat(lnk, ".lnk");
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lnk, -1, wpath, MAX_PATH); 
    pIPF->Save(wpath, FALSE);
    pIPF->Release();
   }
   pisl->Release();
  }
  CoUninitialize();
 }
}

CoInitialize(NULL)
必须调用,并且必须成功,否则无法创建!

你可能感兴趣的:(创建)