c语言创建快捷方式

在windows下用c语言创建快捷方式

1、将C语言编辑器装在哪个文件夹下,找到那儿,应该有个比较鲜艳的图标,选中它后点右键,创建快捷方式,应该就可以了,如果找不到,那就到控制面板里面去将它卸载后重新安装的时候将它的安装路径记下来,就可以找到那儿创建快捷方式了
2、例程:

#define STRICT
#include <windows.h>
/* C run time library headers */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* COM headers (requires shell32.lib, ole32.lib, uuid.lib) */
#include <objbase.h>
#include <shlobj.h>
static HRESULT CreateShortCut(LPSTR pszTargetfile, LPSTR pszTargetargs,
                              LPSTR pszLinkfile, LPSTR pszDescription, 
                              int iShowmode, LPSTR pszCurdir, 
                              LPSTR pszIconfile, int iIconindex)
{
  HRESULT       hRes;                  /* Returned COM result code */
  IShellLink*   pShellLink;            /* IShellLink object pointer */
  IPersistFile* pPersistFile;          /* IPersistFile object pointer */
  WORD          wszLinkfile[MAX_PATH]; /* pszLinkfile as Unicode string */
  int           iWideCharsWritten;     /* Number of wide characters written */
  hRes = E_INVALIDARG;
  if (
       (pszTargetfile != NULL) && (strlen(pszTargetfile) > 0) &&
       (pszTargetargs != NULL) &&
       (pszLinkfile != NULL) && (strlen(pszLinkfile) > 0) &&
       (pszDescription != NULL) && 
       (iShowmode >= 0) &&
       (pszCurdir != NULL) && 
       (pszIconfile != NULL) &&
       (iIconindex >= 0)
     )
  {
    hRes = CoCreateInstance(&CLSID_ShellLink,     /* pre-defined CLSID of the IShellLink object */
                            NULL,                 /* pointer to parent interface if part of aggregate */
                            CLSCTX_INPROC_SERVER, /* caller and called code are in same process */
                            &IID_IShellLink,      /* pre-defined interface of the IShellLink object */
                            &pShellLink);         /* Returns a pointer to the IShellLink object */
    if (SUCCEEDED(hRes))
    {
      /* Set the fields in the IShellLink object */
      hRes = pShellLink->lpVtbl->SetPath(pShellLink, pszTargetfile);
      hRes = pShellLink->lpVtbl->SetArguments(pShellLink, pszTargetargs);
      if (strlen(pszDescription) > 0)
      {
        hRes = pShellLink->lpVtbl->SetDescription(pShellLink, pszDescription);
      }
      if (iShowmode > 0)
      {
        hRes = pShellLink->lpVtbl->SetShowCmd(pShellLink, iShowmode);
      }
      if (strlen(pszCurdir) > 0)
      {
        hRes = pShellLink->lpVtbl->SetWorkingDirectory(pShellLink, pszCurdir);
      }
      if (strlen(pszIconfile) > 0 && iIconindex >= 0)
      {
        hRes = pShellLink->lpVtbl->SetIconLocation(pShellLink, pszIconfile, iIconindex);
      }
      /* Use the IPersistFile object to save the shell link */
      hRes = pShellLink->lpVtbl->QueryInterface(pShellLink,        /* existing IShellLink object */
                                                &IID_IPersistFile, /* pre-defined interface of the IPersistFile object */
                                                &pPersistFile);    /* returns a pointer to the IPersistFile object */
      if (SUCCEEDED(hRes))
      {
        iWideCharsWritten = MultiByteToWideChar(CP_ACP, 0, pszLinkfile, -1, wszLinkfile, MAX_PATH);
        hRes = pPersistFile->lpVtbl->Save(pPersistFile, wszLinkfile, TRUE);
        pPersistFile->lpVtbl->Release(pPersistFile);
      }
      pShellLink->lpVtbl->Release(pShellLink);
    }
  }
  return (hRes);
}
int main(int n, char **argv)
{
  HRESULT   hRes;                      /* result of calling COM functions */
    hRes = CoInitialize(NULL);
    if (SUCCEEDED(hRes))
    {
      hRes = CreateShortCut(argv[0],
                            "",  /* Target arguments */
                            "E:\\sam.lnk",    /* Short-cut filename */
                            "Samurai 2", /* Short-cut description */
                            SW_SHOW,     /* Showmode constant */
                            "",      /* Working directory for linked file */
                            "",    /* Icon file shown for the link */
                            0);   /* Index of icon in the file */
     }
    /* call CoUninitialize() and exit the program. */
    CoUninitialize();
  return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-14
#define STRICT
#include <windows.h>
/* C run time library headers */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* COM headers (requires shell32.lib, ole32.lib, uuid.lib) */
#include <objbase.h>
#include <shlobj.h>
static HRESULT CreateShortCut(LPSTR pszTargetfile, LPSTR pszTargetargs,
LPSTR pszLinkfile, LPSTR pszDescription,
int iShowmode, LPSTR pszCurdir,
LPSTR pszIconfile, int iIconindex)
{
HRESULT hRes; /* Returned COM result code */
IShellLink* pShellLink; /* IShellLink object pointer */
IPersistFile* pPersistFile; /* IPersistFile object pointer */
WORD wszLinkfile[MAX_PATH]; /* pszLinkfile as Unicode string */
int iWideCharsWritten; /* Number of wide characters written */
hRes = E_INVALIDARG;
if (
(pszTargetfile != NULL) && (strlen(pszTargetfile) > 0) &&
(pszTargetargs != NULL) &&
(pszLinkfile != NULL) && (strlen(pszLinkfile) > 0) &&
(pszDescription != NULL) &&
(iShowmode >= 0) &&
(pszCurdir != NULL) &&
(pszIconfile != NULL) &&
(iIconindex >= 0)
)
{
hRes = CoCreateInstance(&CLSID_ShellLink, /* pre-defined CLSID of the IShellLink object */
NULL, /* pointer to parent interface if part of aggregate */
CLSCTX_INPROC_SERVER, /* caller and called code are in same process */
&IID_IShellLink, /* pre-defined interface of the IShellLink object */
&pShellLink); /* Returns a pointer to the IShellLink object */
if (SUCCEEDED(hRes))
{
/* Set the fields in the IShellLink object */
hRes = pShellLink->lpVtbl->SetPath(pShellLink, pszTargetfile);
hRes = pShellLink->lpVtbl->SetArguments(pShellLink, pszTargetargs);
if (strlen(pszDescription) > 0)
{
hRes = pShellLink->lpVtbl->SetDescription(pShellLink, pszDescription);
}
if (iShowmode > 0)
{
hRes = pShellLink->lpVtbl->SetShowCmd(pShellLink, iShowmode);
}
if (strlen(pszCurdir) > 0)
{
hRes = pShellLink->lpVtbl->SetWorkingDirectory(pShellLink, pszCurdir);
}
if (strlen(pszIconfile) > 0 && iIconindex >= 0)
{
hRes = pShellLink->lpVtbl->SetIconLocation(pShellLink, pszIconfile, iIconindex);
}
/* Use the IPersistFile object to save the shell link */
hRes = pShellLink->lpVtbl->QueryInterface(pShellLink, /* existing IShellLink object */
&IID_IPersistFile, /* pre-defined interface of the IPersistFile object */
&pPersistFile); /* returns a pointer to the IPersistFile object */
if (SUCCEEDED(hRes))
{
iWideCharsWritten = MultiByteToWideChar(CP_ACP, 0, pszLinkfile, -1, wszLinkfile, MAX_PATH);
hRes = pPersistFile->lpVtbl->Save(pPersistFile, wszLinkfile, TRUE);
pPersistFile->lpVtbl->Release(pPersistFile);
}
pShellLink->lpVtbl->Release(pShellLink);
}
}
return (hRes);
}
int main(int n, char **argv)
{
HRESULT hRes; /* result of calling COM functions */
hRes = CoInitialize(NULL);
if (SUCCEEDED(hRes))
{
hRes = CreateShortCut(argv[0],
"", /* Target arguments */
"E:\\sam.lnk", /* Short-cut filename */
"Samurai 2", /* Short-cut description */
SW_SHOW, /* Showmode constant */
"", /* Working directory for linked file */
"", /* Icon file shown for the link */
0); /* Index of icon in the file */
}
/* call CoUninitialize() and exit the program. */
CoUninitialize();
return 0;
}
中国物联网校企联盟技术部本回答被提问者采纳
第2个回答  推荐于2016-04-20
C语言创建快捷方式,c++builder测试通过。
/* C run time library headers */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* COM headers (requires shell32.lib, ole32.lib, uuid.lib) */
#include <objbase.h>
#include <shlobj.h>
相似回答