トップ
新規
一覧
検索
ヘルプ
ショートカットを作りたい
をテンプレートにして作成
開始行:
**ショートカットを作る [#k1ccfa7e]
プログラムからショートカットを作りたくなることがある。~
サブルーチン化して使っているので、ここに記録しておく。~
~
subShortcut.h
//-------------------------------------------------------...
// ショートカットを作成する
//------------------------------------------------------...
#ifndef subShortcutH
#define subShortcutH
//------------------------------------------------------...
bool subCreateShortCut(AnsiString strMAKE, AnsiString st...
AnsiString strARG, AnsiString strDIR, An...
//------------------------------------------------------...
#endif
subShortcut.cpp
//-------------------------------------------------------...
// ショートカットを作成する
//------------------------------------------------------...
#include <vcl.h>
#pragma hdrstop
#include <shlobj.h>
#include <alloc.h>
#include "subShortcut.h"
//------------------------------------------------------...
#pragma package(smart_init)
//------------------------------------------------------...
bool subCreateShortCut(AnsiString strMAKE, AnsiString st...
AnsiString strARG, AnsiString strDIR, An...
{
// ショートカットを作成する
// strMAKE : 作るショートカットの名前(*.lnk...
// strPATH : ショートカットのリンク先
// strARG : 引数
// strDIR : 作業ディレクトリ
// strDESC : 説明
// 戻り値 : 成功するとtrue 失敗するとfalse
char cPATH[MAX_PATH], *cARG, cDIR[MAX_PATH], *cDSC;
bool bRET = false;
IShellLink *slTMP;
IPersistFile *pfTMP;
wchar_t wPATH[MAX_PATH];
HRESULT hRES = NULL;
try {
// COMライブラリの初期化
CoInitialize(NULL);
// IShellLinkオブジェクトの生成
hRES = CoCreateInstance(CLSID_ShellLink, NULL, C...
if (SUCCEEDED(hRES)) {
hRES = slTMP->QueryInterface(IID_IPersistFil...
if (SUCCEEDED(hRES)) {
// 作成するショートカット名をUnicode...
MultiByteToWideChar(CP_ACP, 0, strMAKE.c...
// 引数をchar配列に変換
strcpy(cPATH, strPATH.c_str());
cARG = (char*)malloc(strARG.Length() + 1);
strcpy(cARG, strARG.c_str());
strcpy(cDIR, strDIR.c_str());
cDSC = (char*)malloc(strDESC.Length() + ...
strcpy(cDSC, strDESC.c_str());
// パラメータの設定
slTMP->SetPath(cPATH);
slTMP->SetArguments(cARG);
slTMP->SetWorkingDirectory(cDIR);
slTMP->SetDescription(cDSC);
// ショートカットの作成
hRES = pfTMP->Save(wPATH, TRUE);
if (SUCCEEDED(hRES)) {
bRET = true;
}
pfTMP->Release();
// メモリの開放
free(cARG);
free(cDSC);
}
slTMP->Release();
}
CoUninitialize();
}
catch(...) {
bRET = false;
}
return bRET;
}
//------------------------------------------------------...
※このサブルーチンを使うプロジェクトでは、「プロジェクト-...
終了行:
**ショートカットを作る [#k1ccfa7e]
プログラムからショートカットを作りたくなることがある。~
サブルーチン化して使っているので、ここに記録しておく。~
~
subShortcut.h
//-------------------------------------------------------...
// ショートカットを作成する
//------------------------------------------------------...
#ifndef subShortcutH
#define subShortcutH
//------------------------------------------------------...
bool subCreateShortCut(AnsiString strMAKE, AnsiString st...
AnsiString strARG, AnsiString strDIR, An...
//------------------------------------------------------...
#endif
subShortcut.cpp
//-------------------------------------------------------...
// ショートカットを作成する
//------------------------------------------------------...
#include <vcl.h>
#pragma hdrstop
#include <shlobj.h>
#include <alloc.h>
#include "subShortcut.h"
//------------------------------------------------------...
#pragma package(smart_init)
//------------------------------------------------------...
bool subCreateShortCut(AnsiString strMAKE, AnsiString st...
AnsiString strARG, AnsiString strDIR, An...
{
// ショートカットを作成する
// strMAKE : 作るショートカットの名前(*.lnk...
// strPATH : ショートカットのリンク先
// strARG : 引数
// strDIR : 作業ディレクトリ
// strDESC : 説明
// 戻り値 : 成功するとtrue 失敗するとfalse
char cPATH[MAX_PATH], *cARG, cDIR[MAX_PATH], *cDSC;
bool bRET = false;
IShellLink *slTMP;
IPersistFile *pfTMP;
wchar_t wPATH[MAX_PATH];
HRESULT hRES = NULL;
try {
// COMライブラリの初期化
CoInitialize(NULL);
// IShellLinkオブジェクトの生成
hRES = CoCreateInstance(CLSID_ShellLink, NULL, C...
if (SUCCEEDED(hRES)) {
hRES = slTMP->QueryInterface(IID_IPersistFil...
if (SUCCEEDED(hRES)) {
// 作成するショートカット名をUnicode...
MultiByteToWideChar(CP_ACP, 0, strMAKE.c...
// 引数をchar配列に変換
strcpy(cPATH, strPATH.c_str());
cARG = (char*)malloc(strARG.Length() + 1);
strcpy(cARG, strARG.c_str());
strcpy(cDIR, strDIR.c_str());
cDSC = (char*)malloc(strDESC.Length() + ...
strcpy(cDSC, strDESC.c_str());
// パラメータの設定
slTMP->SetPath(cPATH);
slTMP->SetArguments(cARG);
slTMP->SetWorkingDirectory(cDIR);
slTMP->SetDescription(cDSC);
// ショートカットの作成
hRES = pfTMP->Save(wPATH, TRUE);
if (SUCCEEDED(hRES)) {
bRET = true;
}
pfTMP->Release();
// メモリの開放
free(cARG);
free(cDSC);
}
slTMP->Release();
}
CoUninitialize();
}
catch(...) {
bRET = false;
}
return bRET;
}
//------------------------------------------------------...
※このサブルーチンを使うプロジェクトでは、「プロジェクト-...
ページ名: