]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxMSW_CONV_LPCTSTR() and related macros and use them in wxBase.
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 1 Jun 2012 22:34:13 +0000 (22:34 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 1 Jun 2012 22:34:13 +0000 (22:34 +0000)
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
src/msw/dde.cpp
src/msw/registry.cpp
src/msw/utilsexc.cpp

index 622c3b1544f036fbf6d2121c0490b159b791a949..959898fe4ccf5cfd590b06954d18397c4a231581 100644 (file)
@@ -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<const wxChar *>((s).t_str())
+#define wxMSW_CONV_LPTSTR(s) const_cast<wxChar *>(wxMSW_CONV_LPCTSTR(s))
+#define wxMSW_CONV_LPARAM(s) reinterpret_cast<LPARAM>(wxMSW_CONV_LPCTSTR(s))
+
+
 #if wxUSE_GUI
 
 #include "wx/gdicmn.h"
index 10f0dc792a2a8f9c7be63783ac01261e1ee52764..ce2f37211b4e7af644a3e125ecb7b39723e65bd3 100644 (file)
@@ -1068,7 +1068,7 @@ static HSZ DDEAtomFromString(const wxString& s)
 {
     wxASSERT_MSG( DDEIdInst, wxT("DDE not initialized") );
 
-    HSZ hsz = DdeCreateStringHandle(DDEIdInst, const_cast<wxChar*>(static_cast<const wxChar*>(s.t_str())), DDE_CP);
+    HSZ hsz = DdeCreateStringHandle(DDEIdInst, wxMSW_CONV_LPTSTR(s), DDE_CP);
     if ( !hsz )
     {
         DDELogError(_("Failed to create DDE string"));
index 80903a6e4d473375c01d9edc10d3c1a17ba13f41..dd98c124ccea9b5d0d6fcec5bb9368e56670dbb9 100644 (file)
@@ -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<const TCHAR *>(strValue.t_str()),
+                                    (RegString)wxMSW_CONV_LPCTSTR(strValue),
                                     (strValue.Len() + 1)*sizeof(wxChar));
       if ( m_dwLastError == ERROR_SUCCESS )
         return true;
index ae2558770ec549e0c24b9c2b7191091e9c4038b9..416fe17562c293a26d5774b7b568db8fcfdf4f9e 100644 (file)
@@ -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<const TCHAR *>(
-                 moduleName.t_str()),// application name
-                 const_cast<TCHAR *>(static_cast<const TCHAR *>(
-                 arguments.t_str())), // arguments
+                 moduleName.t_str(), // application name
+                 wxMSW_CONV_LPTSTR(arguments), // arguments
 #else
                  NULL,               // application name (use only cmd line)
-                 const_cast<TCHAR *>(static_cast<const TCHAR *>(
-                 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<TCHAR *>(static_cast<const TCHAR *>(env->cwd.t_str()))
+                    ? wxMSW_CONV_LPTSTR(env->cwd)
                     : NULL,          //     (or use the same)
                  &si,                // startup info (unused here)
                  &pi                 // process info