Skip to content

Commit e772a96

Browse files
sreckovic1987matejk
andcommitted
fix(ServerApplication): force service cmd line params on windows (#5240)
* fix(ServerApplication): force service cmd line params on windows * enh(Util): Link shell32.lib on Windows, note about behavioral changes. --------- Co-authored-by: Matej Kenda <matejken@gmail.com>
1 parent ee292b1 commit e772a96

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Util/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ set_target_properties(Util
3333
)
3434

3535
target_link_libraries(Util PUBLIC Poco::Foundation)
36+
if(WIN32)
37+
target_link_libraries(Util PUBLIC shell32.lib)
38+
endif()
3639
if(ENABLE_XML)
3740
target_link_libraries(Util PUBLIC Poco::XML)
3841
else()

Util/src/ServerApplication.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "Poco/Util/WinService.h"
3838
#include "Poco/Util/WinRegistryKey.h"
3939
#include "Poco/UnWindows.h"
40+
#include <shellapi.h>
4041
#include <cstring>
4142
#endif
4243

@@ -180,12 +181,35 @@ void ServerApplication::ServiceMain(DWORD argc, LPWSTR* argv)
180181

181182
try
182183
{
184+
// Use the process command line (from binPath) instead of
185+
// SCM-provided argv, which only contains the service name.
186+
// This allows command-line options like --config-file to
187+
// reach the application when running as a service.
188+
// Since 1.15.1, args[0] is the executable path (from
189+
// GetCommandLineW), matching the argv[0] convention on
190+
// Unix/daemon startup.
183191
std::vector<std::string> args;
184-
for (DWORD i = 0; i < argc; ++i)
192+
int nArgs = 0;
193+
LPWSTR* pArgv = CommandLineToArgvW(GetCommandLineW(), &nArgs);
194+
if (pArgv)
185195
{
186-
std::string arg;
187-
Poco::UnicodeConverter::toUTF8(argv[i], arg);
188-
args.push_back(arg);
196+
for (int i = 0; i < nArgs; ++i)
197+
{
198+
std::string arg;
199+
Poco::UnicodeConverter::toUTF8(pArgv[i], arg);
200+
args.push_back(arg);
201+
}
202+
LocalFree(pArgv);
203+
}
204+
if (args.empty())
205+
{
206+
// fallback to SCM-provided args
207+
for (DWORD i = 0; i < argc; ++i)
208+
{
209+
std::string arg;
210+
Poco::UnicodeConverter::toUTF8(argv[i], arg);
211+
args.push_back(arg);
212+
}
189213
}
190214
app.init(args);
191215
_serviceStatus.dwCurrentState = SERVICE_RUNNING;

0 commit comments

Comments
 (0)