Module: check_mk
Branch: master
Commit: b9533d5b8ec34098d938cf36865660027e863475
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b9533d5b8ec340…
Author: Sebastian Herbord <sh(a)mathias-kettner.de>
Date: Mon Nov 2 15:40:05 2015 +0100
windows agent: reliable utf-8 encoding of service names
---
agents/windows/check_mk_agent.cc | 14 +++++++-------
agents/windows/stringutil.cc | 25 +++++++++++++++++++++++++
agents/windows/stringutil.h | 2 ++
agents/windows/wmiHelper.cc | 22 ----------------------
4 files changed, 34 insertions(+), 29 deletions(-)
diff --git a/agents/windows/check_mk_agent.cc b/agents/windows/check_mk_agent.cc
index 8eee31b..6e57734 100644
--- a/agents/windows/check_mk_agent.cc
+++ b/agents/windows/check_mk_agent.cc
@@ -488,13 +488,13 @@ void section_df(SOCKET &out)
// Determine the start type of a service. Unbelievable how much
// code is needed for that...
-const char *service_start_type(SC_HANDLE scm, LPCTSTR service_name)
+const char *service_start_type(SC_HANDLE scm, LPCWSTR service_name)
{
// Query the start type of the service
const char *start_type = "invalid1";
SC_HANDLE schService;
LPQUERY_SERVICE_CONFIG lpsc;
- schService = OpenService(scm, service_name, SERVICE_QUERY_CONFIG);
+ schService = OpenServiceW(scm, service_name, SERVICE_QUERY_CONFIG);
if (schService) {
start_type = "invalid2";
DWORD dwBytesNeeded, cbBufSize;
@@ -533,16 +533,16 @@ void section_services(SOCKET &out)
DWORD bytes_needed = 0;
DWORD num_services = 0;
// first determine number of bytes needed
- EnumServicesStatusEx(scm, SC_ENUM_PROCESS_INFO, SERVICE_WIN32,
SERVICE_STATE_ALL,
+ EnumServicesStatusExW(scm, SC_ENUM_PROCESS_INFO, SERVICE_WIN32,
SERVICE_STATE_ALL,
NULL, 0, &bytes_needed, &num_services, 0, 0);
if (GetLastError() == ERROR_MORE_DATA && bytes_needed > 0) {
BYTE *buffer = (BYTE *)malloc(bytes_needed);
if (buffer) {
- if (EnumServicesStatusEx(scm, SC_ENUM_PROCESS_INFO, SERVICE_WIN32,
SERVICE_STATE_ALL,
+ if (EnumServicesStatusExW(scm, SC_ENUM_PROCESS_INFO, SERVICE_WIN32,
SERVICE_STATE_ALL,
buffer, bytes_needed,
&bytes_needed, &num_services, 0, 0))
{
- ENUM_SERVICE_STATUS_PROCESS *service = (ENUM_SERVICE_STATUS_PROCESS
*)buffer;
+ ENUM_SERVICE_STATUS_PROCESSW *service =
(ENUM_SERVICE_STATUS_PROCESSW*)buffer;
for (unsigned i=0; i<num_services; i++) {
DWORD state = service->ServiceStatusProcess.dwCurrentState;
const char *state_name = "unknown";
@@ -567,9 +567,9 @@ void section_services(SOCKET &out)
*w = '_';
}
- output(out, "%s %s/%s %s\n",
+ output(out, "%ls %s/%s %s\n",
service->lpServiceName, state_name, start_type,
- service->lpDisplayName);
+ to_utf8(service->lpDisplayName).c_str());
service ++;
}
}
diff --git a/agents/windows/stringutil.cc b/agents/windows/stringutil.cc
index f6b0eef..04efb36 100644
--- a/agents/windows/stringutil.cc
+++ b/agents/windows/stringutil.cc
@@ -9,6 +9,9 @@
#endif
+using std::string;
+
+
char *lstrip(char *s)
{
while (isspace(*s))
@@ -151,6 +154,28 @@ int parse_boolean(char *value)
}
+string to_utf8(const wchar_t *input)
+{
+ string result;
+ // preflight: how many bytes to we need?
+ int required_size = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
+ if (required_size == 0) {
+ // conversion failure. What to do?
+ return string();
+ }
+ result.resize(required_size);
+
+ // real conversion
+ WideCharToMultiByte(CP_UTF8, 0, input, -1, &result[0], required_size, NULL,
NULL);
+
+ // strip away the zero termination. This is necessary, otherwise the stored string
length
+ // in the string is wrong
+ result.resize(required_size - 1);
+
+ return result;
+}
+
+
bool ci_compare_pred(unsigned char lhs, unsigned char rhs)
{
return std::tolower(lhs) == std::tolower(rhs);
diff --git a/agents/windows/stringutil.h b/agents/windows/stringutil.h
index b000eb0..8c4376a 100644
--- a/agents/windows/stringutil.h
+++ b/agents/windows/stringutil.h
@@ -55,6 +55,8 @@ void lowercase(char *s);
int parse_boolean(char *value);
+std::string to_utf8(const wchar_t *input);
+
// case insensitive compare
bool ci_equal(const std::string &lhs, const std::string &rhs);
diff --git a/agents/windows/wmiHelper.cc b/agents/windows/wmiHelper.cc
index 0b7fde5..f63067f 100644
--- a/agents/windows/wmiHelper.cc
+++ b/agents/windows/wmiHelper.cc
@@ -257,28 +257,6 @@ bool Result::next()
}
-string to_utf8(const wchar_t *input)
-{
- string result;
- // preflight: how many bytes to we need?
- int required_size = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
- if (required_size == 0) {
- // conversion failure. What to do?
- return string();
- }
- result.resize(required_size);
-
- // real conversion
- WideCharToMultiByte(CP_UTF8, 0, input, -1, &result[0], required_size, NULL,
NULL);
-
- // strip away the zero termination. This is necessary, otherwise the stored string
length
- // in the string is wrong
- result.resize(required_size - 1);
-
- return result;
-}
-
-
template <> int Variant::get()
{
switch (_value.vt) {