From 125afca0c0e4d060cb0edbf0c498048f7cb66c13 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 1 Jun 2012 22:34:13 +0000 Subject: [PATCH] Add wxMSW_CONV_LPCTSTR() and related macros and use them in wxBase. Add macros hiding the ugly casts needed to pass wxStrings to Windows API functions and use them in a couple of places in wxBase to simplify the code. Closes #14338. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71636 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/private.h | 15 +++++++++++++++ src/msw/dde.cpp | 2 +- src/msw/registry.cpp | 2 +- src/msw/utilsexc.cpp | 11 ++++------- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 622c3b1544..959898fe4c 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -218,6 +218,21 @@ struct WinStruct : public T }; +// Macros for converting wxString to the type expected by API functions. +// +// Normally it is enough to just use wxString::t_str() which is implicitly +// convertible to LPCTSTR, but in some cases an explicit conversion is required. +// +// In such cases wxMSW_CONV_LPCTSTR() should be used. But if an API function +// takes a non-const pointer, wxMSW_CONV_LPTSTR() which casts away the +// constness (but doesn't make it possible to really modify the returned +// pointer, of course) should be used. And if a string is passed as LPARAM, use +// wxMSW_CONV_LPARAM() which does the required ugly reinterpret_cast<> too. +#define wxMSW_CONV_LPCTSTR(s) static_cast((s).t_str()) +#define wxMSW_CONV_LPTSTR(s) const_cast(wxMSW_CONV_LPCTSTR(s)) +#define wxMSW_CONV_LPARAM(s) reinterpret_cast(wxMSW_CONV_LPCTSTR(s)) + + #if wxUSE_GUI #include "wx/gdicmn.h" diff --git a/src/msw/dde.cpp b/src/msw/dde.cpp index 10f0dc792a..ce2f37211b 100644 --- a/src/msw/dde.cpp +++ b/src/msw/dde.cpp @@ -1068,7 +1068,7 @@ static HSZ DDEAtomFromString(const wxString& s) { wxASSERT_MSG( DDEIdInst, wxT("DDE not initialized") ); - HSZ hsz = DdeCreateStringHandle(DDEIdInst, const_cast(static_cast(s.t_str())), DDE_CP); + HSZ hsz = DdeCreateStringHandle(DDEIdInst, wxMSW_CONV_LPTSTR(s), DDE_CP); if ( !hsz ) { DDELogError(_("Failed to create DDE string")); diff --git a/src/msw/registry.cpp b/src/msw/registry.cpp index 80903a6e4d..dd98c124cc 100644 --- a/src/msw/registry.cpp +++ b/src/msw/registry.cpp @@ -1057,7 +1057,7 @@ bool wxRegKey::SetValue(const wxString& szValue, const wxString& strValue) m_dwLastError = RegSetValueEx((HKEY) m_hKey, RegValueStr(szValue), (DWORD) RESERVED, REG_SZ, - (RegString)static_cast(strValue.t_str()), + (RegString)wxMSW_CONV_LPCTSTR(strValue), (strValue.Len() + 1)*sizeof(wxChar)); if ( m_dwLastError == ERROR_SUCCESS ) return true; diff --git a/src/msw/utilsexc.cpp b/src/msw/utilsexc.cpp index ae2558770e..416fe17562 100644 --- a/src/msw/utilsexc.cpp +++ b/src/msw/utilsexc.cpp @@ -857,14 +857,11 @@ long wxExecute(const wxString& cmd, int flags, wxProcess *handler, // WinCE requires appname to be non null // Win32 allows for null #ifdef __WXWINCE__ - static_cast( - moduleName.t_str()),// application name - const_cast(static_cast( - arguments.t_str())), // arguments + moduleName.t_str(), // application name + wxMSW_CONV_LPTSTR(arguments), // arguments #else NULL, // application name (use only cmd line) - const_cast(static_cast( - command.t_str())), // full command line + wxMSW_CONV_LPTSTR(command), // full command line #endif NULL, // security attributes: defaults for both NULL, // the process and its main thread @@ -872,7 +869,7 @@ long wxExecute(const wxString& cmd, int flags, wxProcess *handler, dwFlags, // process creation flags envBuffer.data(), // environment (may be NULL which is fine) useCwd // initial working directory - ? const_cast(static_cast(env->cwd.t_str())) + ? wxMSW_CONV_LPTSTR(env->cwd) : NULL, // (or use the same) &si, // startup info (unused here) &pi // process info -- 2.47.2