Skip to content

Commit d0de816

Browse files
committed
Migrating from CreateProcess to ShellExecuteEx
1 parent 8659617 commit d0de816

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# their names here. Please keep the list sorted by first names.
44

55
Antoine Beauchamp <[email protected]>
6+
Mindaugas Ribaconka <[email protected]>

include/shellanything/ActionExecute.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ namespace shellanything
4040
virtual ~ActionExecute();
4141

4242
/// <summary>
43-
/// Execute an application.
43+
/// Execute an application.
44+
/// </summary>
45+
/// <param name="iContext">The current context of execution.</param>
46+
/// <returns>Returns true if the execution is successful. Returns false otherwise.</returns>
47+
virtual bool Execute(const Context& iContext) const;
48+
49+
/// <summary>
50+
/// Execute an application with RapidAssist method.
4451
/// </summary>
4552
/// <param name="iContext">The current context of execution.</param>
4653
/// <returns>Returns true if the execution is successful. Returns false otherwise.</returns>
47-
virtual bool Execute(const Context & iContext) const;
54+
virtual bool StartProcess(const Context & iContext) const;
4855

4956
/// <summary>
5057
/// Getter for the 'path' parameter.
@@ -76,10 +83,21 @@ namespace shellanything
7683
/// </summary>
7784
void SetArguments(const std::string & iArguments);
7885

86+
/// <summary>
87+
/// Getter for the 'verb' parameter.
88+
/// </summary>
89+
const std::string& GetVerb() const;
90+
91+
/// <summary>
92+
/// Setter for the 'verb' parameter.
93+
/// </summary>
94+
void SetVerb(const std::string& iArguments);
95+
7996
private:
8097
std::string mPath;
8198
std::string mBaseDir;
8299
std::string mArguments;
100+
std::string mVerb;
83101
};
84102

85103
} //namespace shellanything

src/ActionExecute.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "rapidassist/filesystem_utf8.h"
2929
#include "PropertyManager.h"
3030

31+
#include <windows.h>
3132
#pragma warning( push )
3233
#pragma warning( disable: 4355 ) // glog\install_dir\include\glog/logging.h(1167): warning C4355: 'this' : used in base member initializer list
3334
#include <glog/logging.h>
@@ -45,7 +46,56 @@ namespace shellanything
4546
{
4647
}
4748

48-
bool ActionExecute::Execute(const Context & iContext) const
49+
bool ActionExecute::Execute(const Context& iContext) const
50+
{
51+
PropertyManager& pmgr = PropertyManager::GetInstance();
52+
std::string path = pmgr.Expand(mPath);
53+
std::string basedir = pmgr.Expand(mBaseDir);
54+
std::string arguments = pmgr.Expand(mArguments);
55+
std::string verb = pmgr.Expand(mVerb);
56+
57+
std::wstring pathW = ra::unicode::Utf8ToUnicode(path);
58+
std::wstring argumentsW = ra::unicode::Utf8ToUnicode(arguments);
59+
std::wstring basedirW = ra::unicode::Utf8ToUnicode(basedir);
60+
std::wstring verbW = ra::unicode::Utf8ToUnicode(verb);
61+
62+
SHELLEXECUTEINFOW info = { 0 };
63+
64+
info.cbSize = sizeof(SHELLEXECUTEINFOW);
65+
66+
info.fMask |= SEE_MASK_NOCLOSEPROCESS;
67+
info.fMask |= SEE_MASK_NOASYNC;
68+
info.fMask |= SEE_MASK_FLAG_DDEWAIT;
69+
70+
info.hwnd = HWND_DESKTOP;
71+
info.nShow = SW_SHOWDEFAULT;
72+
info.lpFile = pathW.c_str();
73+
74+
LOG(INFO) << "Exec: '" << path;
75+
76+
if (!verb.empty())
77+
{
78+
info.lpVerb = verbW.c_str(); // Verb
79+
LOG(INFO) << "Verb: '" << verb;
80+
}
81+
82+
if (!arguments.empty())
83+
{
84+
info.lpParameters = argumentsW.c_str(); // Arguments
85+
LOG(INFO) << "Arguments: '" << arguments;
86+
}
87+
88+
if (!basedir.empty())
89+
{
90+
info.lpDirectory = basedirW.c_str(); // Default directory
91+
LOG(INFO) << "Basedir: '" << basedir;
92+
}
93+
94+
BOOL success = ShellExecuteExW(&info);
95+
return (success == TRUE);
96+
}
97+
98+
bool ActionExecute::StartProcess(const Context & iContext) const
4999
{
50100
PropertyManager & pmgr = PropertyManager::GetInstance();
51101
std::string path = pmgr.Expand(mPath);
@@ -146,4 +196,14 @@ namespace shellanything
146196
mArguments = iArguments;
147197
}
148198

199+
const std::string& ActionExecute::GetVerb() const
200+
{
201+
return mArguments;
202+
}
203+
204+
void ActionExecute::SetVerb(const std::string& iVerb)
205+
{
206+
mVerb = iVerb;
207+
}
208+
149209
} //namespace shellanything

src/ActionOpen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ namespace shellanything
6969
info.nShow = SW_SHOWDEFAULT;
7070
info.lpVerb = L"open";
7171
info.lpFile = pathW.c_str();
72-
info.lpParameters = NULL; //arguments
73-
info.lpDirectory = NULL; // default directory
72+
info.lpParameters = NULL; // arguments
73+
info.lpDirectory = NULL; // Default directory
7474

7575
BOOL success = ShellExecuteExW(&info);
7676
return (success == TRUE);

src/ObjectFactory.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ namespace shellanything
293293
action->SetBaseDir(tmp_str);
294294
}
295295

296+
//parse verb
297+
tmp_str = "";
298+
tmp_int = -1;
299+
if (ParseAttribute(element, "verb", true, true, tmp_str, error))
300+
{
301+
action->SetVerb(tmp_str);
302+
}
303+
296304
//done parsing
297305
return action;
298306
}

0 commit comments

Comments
 (0)