0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > ショートカットを作成する

Last updated at Posted at 2015-10-26
動作確認
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

ショートカットを作成したい。

参考 @ プログラミングは道連れ

上記を参考に以下の実装。

//---------------------------------------------------------------------------
bool __fastcall TForm1::CreateShortcutLink(String srcFile, String dstDir, String shortcutBaseName)
{

	IShellLink *pShellLink = NULL;
	IPersistFile *pPersistFile = NULL;
	String shortcutPath;

	if (FileExists(srcFile) == false) {
		OutputDebugString(L"27");
		return false;
	}

	CoInitialize(NULL);

	bool res;

	HRESULT hRes = CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink );
	if (SUCCEEDED(hRes)) {
		hRes = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);

		pShellLink->SetPath(srcFile.c_str());
		pShellLink->SetWorkingDirectory(dstDir.c_str());

		if (shortcutBaseName.Length() > 0) {
			shortcutPath = IncludeTrailingPathDelimiter(dstDir) + shortcutBaseName + L".lnk";
		} else {
			shortcutPath = IncludeTrailingPathDelimiter(dstDir) + ExtractFileName(srcFile) + L" - Shortcut.lnk";
		}

		if (pPersistFile->Save(shortcutPath.c_str(), /* fRemember=*/false) != S_OK) {
			OutputDebugString(L"52");

			res = false;
		} else {
			res = true;
		}
	}

	pPersistFile->Release();
	pShellLink->Release();
	CoUninitialize();

	return res;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	String srcFile = L"D:\\WORK\\151026_shortcut\\dat.txt";
	String dstDir = L"D:\\WORK\\151026_shortcut";
	String shortcutName = L"";

	CreateShortcutLink(srcFile, dstDir, shortcutName);
}
//---------------------------------------------------------------------------

dat.txt - Shortcutというショートカットが作成できる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?