tools
読み取り中…
検索中…
一致する文字列を見つけられません
fs.h
[詳解]
1#pragma once
2#include <string>
3#include <shobjidl.h>
4#include <shlguid.h>
5#include <objbase.h>
6
7#include <yy981/proc.h>
8
9/**
10 * @file fs.h
11 * @brief ファイルシステム補助ユーティリティ(Windows)
12 *
13 * Windows Shell API を用いた .lnk ショートカットの解決と、
14 * ショートカット対応のアプリ起動機能を提供します。
15 */
16
17/**
18 * @namespace yfs
19 * @brief yy981 ファイルシステムユーティリティ用名前空間
20 */
21namespace yfs {
22
23/**
24 * @brief Windows ショートカット (.lnk) ファイルのリンク先パスを解決する
25 * @param lnkPath 解決する .lnk ファイルのパス(std::string)
26 * @return リンク先の実ファイルパス文字列
27 * @throws std::runtime_error .lnk の解決に失敗した場合
28 * @note COM (CoInitialize/CoUninitialize) を内部で初期化・解放します
29 */
30std::string resolveShortcut(const std::string& lnkPath) {
31 CoInitialize(NULL);
32
33 IShellLinkA* pShellLink = nullptr;
34 IPersistFile* pPersistFile = nullptr;
35 char targetPath[MAX_PATH] = {};
36
37 std::string ret;
38
39 if (SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (void**)&pShellLink))) {
40 if (SUCCEEDED(pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile))) {
41 std::wstring wpath(lnkPath.begin(), lnkPath.end());
42 if (SUCCEEDED(pPersistFile->Load(wpath.c_str(), STGM_READ))) {
43 if (SUCCEEDED(pShellLink->GetPath(targetPath, MAX_PATH, NULL, SLGP_RAWPATH))) {
44 ret = std::string(targetPath);
45 }
46 }
47 pPersistFile->Release();
48 }
49 pShellLink->Release();
50 }
51
52 CoUninitialize();
53
54 if (ret.empty()) {
55 throw std::runtime_error("yy981/fs::resolveShortcut(): .lnkの解決に失敗");
56 }
57 return ret;
58}
59
60/**
61 * @brief アプリケーションを起動する(.lnk ショートカット対応)
62 * @param app 起動するアプリケーションのパス。.lnk 拡張子の場合は自動的にリンク先を解決します
63 * @param arg コマンドライン引数(デフォルト: 空文字列)
64 * @param wait プロセスの終了を待つか(デフォルト: false)
65 * @param cd 起動時のカレントディレクトリ(デフォルト: 空文字列 = 親プロセスと同じ)
66 * @return wait=true の場合は起動したプロセスの終了コード、wait=false の場合は 0
67 * @throws std::runtime_error 起動失敗または .lnk 解決失敗時
68 */
69int start(const std::string app, std::string arg = "", const bool wait = false, const std::string& cd = "") {
70 if (app.ends_with(".lnk")) return proc::start(resolveShortcut(app),arg,wait,cd);
71 return proc::start(app,arg,wait,cd);
72}
73
74}
int start(const std::string app, std::string arg="", const bool wait=false, const std::string &cd="")
外部アプリケーションを起動する(ANSI ビルド版)
Definition proc.h:76
yy981 ファイルシステムユーティリティ用名前空間
int start(const std::string app, std::string arg="", const bool wait=false, const std::string &cd="")
アプリケーションを起動する(.lnk ショートカット対応)
Definition fs.h:69
std::string resolveShortcut(const std::string &lnkPath)
Windows ショートカット (.lnk) ファイルのリンク先パスを解決する
Definition fs.h:30
プロセス起動ユーティリティ(Windows)