回答
试试看Windows Shell Links.此页面还包含C++ example的信息。说明摘要:
使用外壳链接
本节包含的例子是 演示如何创建并从基于Win32的应用 内解决 快捷方式。本节假定您对 已熟悉Win32,C++和OLE COM编程。
编辑:(和MSDN链接也经常死)添加的情况下,链接死亡的代码示例
// CreateLink - Uses the Shell's IShellLink and IPersistFile interfaces
// to create and store a shortcut to the specified object.
//
// Returns the result of calling the member functions of the interfaces.
//
// Parameters:
// lpszPathObj - Address of a buffer that contains the path of the object,
// including the file name.
// lpszPathLink - Address of a buffer that contains the path where the
// Shell link is to be stored, including the file name.
// lpszDesc - Address of a buffer that contains a description of the
// Shell link, stored in the Comment field of the link
// properties.
#include "stdafx.h"
#include "windows.h"
#include "winnls.h"
#include "shobjidl.h"
#include "objbase.h"
#include "objidl.h"
#include "shlguid.h"
HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc)
{
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
// has already been called.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the description.
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
// Query IShellLink for the IPersistFile interface, used for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
这MSDN条每页,Shell Links,提供了有关代码主题的综合教程例。
这里是基于 William Rayer code on codeproject.com
头文件(ShortcutProvider.h)一个C++示例代码:
#include <Windows.h>
class ShortcutProvider
{
public:
/*
-------------------------------------------------------------------
Description:
Creates the actual 'lnk' file (assumes COM has been initialized).
Parameters:
pszTargetfile - File name of the link's target.
pszTargetargs - Command line arguments passed to link's target.
pszLinkfile - File name of the actual link file being created.
pszDescription - Description of the linked item.
iShowmode - ShowWindow() constant for the link's target.
pszCurdir - Working directory of the active link.
pszIconfile - File name of the icon file used for the link.
iIconindex - Index of the icon in the icon file.
Returns:
HRESULT value >= 0 for success, < 0 for failure.
--------------------------------------------------------------------
*/
HRESULT Create(LPSTR pszTargetfile, LPSTR pszTargetargs,
LPSTR pszLinkfile, LPSTR pszDescription,
int iShowmode, LPSTR pszCurdir,
LPSTR pszIconfile, int iIconindex);
};
源文件(ShortcutProvide.cpp):
#include "ShortcutProvider.h"
#include <Windows.h>
#include <shlobj.h>
#include <winnls.h>
#include <shobjidl.h>
#include <objbase.h>
#include <objidl.h>
#include <shlguid.h>
HRESULT ShortcutProvider::Create(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 */
WCHAR wszLinkfile[MAX_PATH]; /* pszLinkfile as Unicode
string */
int iWideCharsWritten; /* Number of wide characters
written */
CoInitialize(NULL);
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 */
(LPVOID*)&pShellLink); /* Returns a pointer to the IShellLink
object */
if (SUCCEEDED(hRes))
{
/* Set the fields in the IShellLink object */
hRes = pShellLink->SetPath(pszTargetfile);
hRes = pShellLink->SetArguments(pszTargetargs);
if (strlen(pszDescription) > 0)
{
hRes = pShellLink->SetDescription(pszDescription);
}
if (iShowmode > 0)
{
hRes = pShellLink->SetShowCmd(iShowmode);
}
if (strlen(pszCurdir) > 0)
{
hRes = pShellLink->SetWorkingDirectory(pszCurdir);
}
if (strlen(pszIconfile) > 0 && iIconindex >= 0)
{
hRes = pShellLink->SetIconLocation(pszIconfile, iIconindex);
}
/* Use the IPersistFile object to save the shell link */
hRes = pShellLink->QueryInterface(
IID_IPersistFile, /* pre-defined interface of the
IPersistFile object */
(LPVOID*)&pPersistFile); /* returns a pointer to the
IPersistFile object */
if (SUCCEEDED(hRes))
{
iWideCharsWritten = MultiByteToWideChar(CP_ACP, 0,
pszLinkfile, -1,
wszLinkfile, MAX_PATH);
hRes = pPersistFile->Save(wszLinkfile, TRUE);
pPersistFile->Release();
}
pShellLink->Release();
}
}
CoUninitialize();
return (hRes);
}
这不会创建快捷方式(lnk文件) – 2014-05-24 07:03:17
,但是其他一些组合,例如/ J用于链接目录。 – 2015-05-26 20:16:02
/d是指向目录的链接;只需删除/ d链接到文件 – Marichyasana 2016-04-19 22:29:34
- 1. 以编程方式创建组合桌面快捷键“快捷方式”
- 2. 如何以编程方式创建打印机的快捷方式?
- 3. 创建应用程序快捷方式
- 4. 以编程方式在用户桌面上创建Web快捷方式
- 5. C#创建快捷方式
- 6. 以编程方式安装应用程序快捷方式
- 7. 使用NSIS创建快捷方式
- 8. 使用.Net创建快捷方式
- 9. 创建批处理和快捷方式的快捷方式
- 10. 如何创建关机快捷方式?
- 11. 如何创建FOLDER的快捷方式?
- 12. 以编程方式在python中创建键盘快捷键
- 13. 以编程方式在Revit中创建键盘快捷键
- 14. 以编程方式编辑Windows快捷方式
- 15. 如何使用WIX创建卸载程序快捷方式
- 16. 如何在android中以编程方式添加应用程序快捷方式
- 17. 如何在android中以编程方式更改应用程序图标而不创建快捷方式?
- 18. 以编程方式添加URL快捷方式
- 19. 以编程方式在Windows 8上启动快捷方式
- 20. iOS以编程方式添加主屏幕快捷方式
- 21. 以编程方式iOS键盘快捷方式
- 22. 在Cocoa/XCode中以编程方式添加快捷方式
- 23. 如何以编程方式从用户桌面删除快捷方式?
- 24. 如何重新创建webstart应用程序的快捷方式?
- 25. 如何从其他Android应用程序创建快捷方式
- 26. 如何创建Java桌面应用程序的快捷方式?
- 27. 如何在KDE中以编程方式创建键盘快捷键?
- 28. 如何使安装程序创建快捷方式文档和文件夹的快捷方式(Windows 8)
- 29. 使用Microsoft.Web.Administration以编程方式创建
- 30. 如何以编程方式在Android中创建另一个应用的快捷方式?
这很有趣,您提供的链接和Paul提供的链接是相同的页面,但它们在物理上有不同的链接。我不知道这是为什么。只是奇怪。只是想把它扔到那里。 – dyslexicanaboko 2013-01-07 21:47:12