WEB开发网
开发学院软件开发VC VC 如何创建自身快捷方式 阅读

VC 如何创建自身快捷方式

 2012-09-13 10:19:00 来源:WEB开发网   
核心提示:#include <shobjidl.h>#include <shlguid.h>#include <shlobj.h>void main(){ HRESULT hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { IShellLink *p
#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)

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

Tags:VC 如何 创建

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接