在桌面创建快捷方式 XP下成功 Windows7下失败 为什么
用的是BCB6 代码如下 不知道应该如何修改 或其它方法在Windows7下实现
//---------------------------------------------------------------------------
#define NO_WIN32_LEAN_AND_MEAN
#include <shlobj.hpp>
#include <vcl.h>
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
struct TShortcutCfg
{
// 构造函数
TShortcutCfg()
{
nShowCmd = SW_SHOWNORMAL;
wHotKey = 0;
nIconIndex = 0;
}
// 结构成员:
AnsiString strShortcutName; //
AnsiString strLnkDir; //
AnsiString strDestFile; //
AnsiString strArguments; //
AnsiString strIconFile; //
int nIconIndex; //
AnsiString strWorkingDir; //
AnsiString strDescription; //
WORD wHotKey; //
int nShowCmd; //
};
//---------------------------------------------------------------------------
// 创建快捷方式
bool CrnCreateShortcut(TShortcutCfg sc, int nFolder)
{
char szBuf[MAX_PATH];
bool bResult = true;
wchar_t wszBuf[MAX_PATH];
IShellLink *pShellLink;
AnsiString strShortcutFile;
if (nFolder >= 0)
{
LPITEMIDLIST lpItemIdList;
SHGetSpecialFolderLocation(0, nFolder, &lpItemIdList);
SHGetPathFromIDList(lpItemIdList, szBuf);
if (DirectoryExists(AnsiString(szBuf)))
{
strShortcutFile = AnsiString(szBuf) + "\\" + sc.strShortcutName + ".lnk";
strShortcutFile.WideChar(wszBuf, MAX_PATH);
}
else
bResult = false;
}
else if (DirectoryExists(sc.strLnkDir))
{
strShortcutFile = IncludeTrailingBackslash(sc.strLnkDir) +
sc.strShortcutName + ".lnk";
strShortcutFile.WideChar(wszBuf, MAX_PATH);
}
else
bResult = false;
if (bResult)
{
bResult = (CoInitialize(NULL) == S_OK);
if (bResult)
{
bResult = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **)&pShellLink) >= 0;
if (bResult)
{
IPersistFile *ppf;
bResult = pShellLink->QueryInterface(IID_IPersistFile, (void **)&ppf) >= 0;
if (bResult)
{
// 目标文件
if (sc.strDestFile != EmptyStr)
bResult = pShellLink->SetPath(sc.strDestFile.c_str()) >= 0;
// 参数
if (bResult && sc.strArguments != EmptyStr)
bResult = pShellLink->SetArguments(sc.strArguments.c_str()) >= 0;
// 显示图标
if (bResult && sc.strIconFile != EmptyStr && FileExists(sc.strIconFile))
pShellLink->SetIconLocation(sc.strIconFile.c_str(),
sc.nIconIndex);
// 起始位置
if (bResult && sc.strWorkingDir != EmptyStr)
pShellLink->SetWorkingDirectory(sc.strWorkingDir.c_str());
// 备注
if (bResult && sc.strDescription != EmptyStr)
pShellLink->SetDescription(sc.strDescription.c_str());
// 快捷键
if (bResult && sc.wHotKey != 0)
pShellLink->SetHotkey(sc.wHotKey);
// 运行方式
if (bResult && sc.nShowCmd != 0)
pShellLink->SetShowCmd(sc.nShowCmd);
if (bResult)
bResult = (ppf->Save(wszBuf, TRUE) >= 0);
ppf->Release ();
}
pShellLink->Release ();
}
CoUninitialize();
}
}
return bResult;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char szBuf[MAX_PATH];
GetSystemDirectory(szBuf, sizeof(szBuf));
TShortcutCfg sc;
/*sc.strDestFile = ParamStr(0);
sc.strShortcutName = "快捷方式名称";
sc.strArguments = "/n";
sc.strIconFile = AnsiString(szBuf) + "\\shell32.dll";
sc.nIconIndex = 0;
sc.strWorkingDir = ExtractFilePath(ParamStr(0));
sc.wHotKey = MAKEWORD(VK_F8, HOTKEYF_CONTROL | HOTKEYF_SHIFT);
sc.nShowCmd = SW_SHOWMAXIMIZED; */
sc.strDestFile = "D:\\Client.exe";
sc.strShortcutName = "ShareGTS";
sc.strArguments = "";
sc.strIconFile = "D:\\moon.ico";
sc.nIconIndex = 0;
//sc.strWorkingDir = ExtractFilePath(gtsHome+"ShareGTS.exe");
sc.wHotKey = MAKEWORD(VK_F8, HOTKEYF_CONTROL | HOTKEYF_SHIFT);
sc.nShowCmd = SW_SHOWNORMAL;
if (!CrnCreateShortcut(sc, CSIDL_DESKTOP))
ShowMessage("创建快捷方式失败!");
else
ShowMessage("创建快捷方式成功!");
}