在桌面创建快捷方式 XP下成功 Windows7下失败 为什么

Gagaga 2013-07-16 09:52:51
用的是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("创建快捷方式成功!");
}

...全文
138 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccrun.com 2013-07-17
  • 打赏
  • 举报
回复
好面熟的代码啊。这段代码在Win8下没有问题。
关工 2013-07-17
  • 打赏
  • 举报
回复
帮你在WIN7下调试过这段代码了,确认是CoInitialize(NULL) == S_OK 的问题,S_OK=0, 而CoInitialize(NULL)返回是1,所以就有问题了。改成 CoInitialize(NULL) >= S_OK 即可。 改了后,什么都不用设置,运行,点击BUTTON1,可生产快捷方式。
我不懂电脑 2013-07-17
  • 打赏
  • 举报
回复
在桌面上,请确认程序运行在管理员权限下 。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧