diff --git a/include/wx/utils.h b/include/wx/utils.h index 38c59e5f6a..61520916af 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -676,10 +676,31 @@ public: void WXDLLIMPEXP_CORE wxGetMousePosition( int* x, int* y ); // MSW only: get user-defined resource from the .res file. -// Returns NULL or newly-allocated memory, so use delete[] to clean up. #ifdef __WXMSW__ - extern WXDLLIMPEXP_CORE const wxChar* wxUserResourceStr; - WXDLLIMPEXP_CORE char* wxLoadUserResource(const wxString& resourceName, const wxString& resourceType = wxUserResourceStr, int* pLen = NULL); + // default resource type for wxLoadUserResource() + extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxUserResourceStr; + + // Return the pointer to the resource data. This pointer is read-only, use + // the overload below if you need to modify the data. + // + // Returns true on success, false on failure. Doesn't log an error message + // if the resource is not found (because this could be expected) but does + // log one if any other error occurs. + WXDLLIMPEXP_BASE bool + wxLoadUserResource(const void **outData, + size_t *outLen, + const wxString& resourceName, + const wxString& resourceType = wxUserResourceStr); + + // This function allocates a new buffer and makes a copy of the resource + // data, remember to delete[] the buffer. And avoid using it entirely if + // the overload above can be used. + // + // Returns NULL on failure. + WXDLLIMPEXP_BASE char* + wxLoadUserResource(const wxString& resourceName, + const wxString& resourceType = wxUserResourceStr, + int* pLen = NULL); #endif // MSW // ---------------------------------------------------------------------------- diff --git a/src/common/datacmn.cpp b/src/common/datacmn.cpp index 10baf57320..bb6a533fbe 100644 --- a/src/common/datacmn.cpp +++ b/src/common/datacmn.cpp @@ -78,6 +78,3 @@ extern WXDLLEXPORT_DATA(const char) wxDirDialogNameStr[] = "wxDirCtrl"; extern WXDLLEXPORT_DATA(const char) wxDirDialogDefaultFolderStr[] = "/"; extern WXDLLEXPORT_DATA(const char) wxFileDialogNameStr[] = "filedlg"; -#if defined(__WXMSW__) || defined(__OS2__) -WXDLLEXPORT_DATA(const wxChar *) wxUserResourceStr = wxT("TEXT"); -#endif diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index fd36d634b1..2958f75788 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -118,6 +118,8 @@ static const wxChar WX_SECTION[] = wxT("wxWindows"); static const wxChar eUSERNAME[] = wxT("UserName"); #endif +WXDLLIMPEXP_DATA_BASE(const wxChar *) wxUserResourceStr = wxT("TEXT"); + // ============================================================================ // implementation // ============================================================================ @@ -1075,6 +1077,67 @@ bool wxIsDebuggerRunning() #endif } +// ---------------------------------------------------------------------------- +// working with MSW resources +// ---------------------------------------------------------------------------- + +bool +wxLoadUserResource(const void **outData, + size_t *outLen, + const wxString& resourceName, + const wxString& resourceType) +{ + wxCHECK_MSG( outData && outLen, false, "output pointers can't be NULL" ); + + HRSRC hResource = ::FindResource(wxGetInstance(), + resourceName.wx_str(), + resourceType.wx_str()); + if ( !hResource ) + return false; + + HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); + if ( !hData ) + { + wxLogSysError(_("Failed to load resource \"%s\"."), resourceName); + return false; + } + + *outData = ::LockResource(hData); + if ( !*outData ) + { + wxLogSysError(_("Failed to lock resource \"%s\"."), resourceName); + return false; + } + + *outLen = ::SizeofResource(wxGetInstance(), hResource); + + // Notice that we do not need to call neither UnlockResource() (which is + // obsolete in Win32) nor GlobalFree() (resources are freed on process + // termination only) + + return true; +} + +char * +wxLoadUserResource(const wxString& resourceName, + const wxString& resourceType, + int* pLen) +{ + const void *data; + size_t len; + if ( !wxLoadUserResource(&data, &len, resourceName, resourceType) ) + return NULL; + + char *s = new char[len + 1]; + memcpy(s, data, len); + s[len] = '\0'; // NUL-terminate in case the resource itself wasn't + + if (pLen) + *pLen = len; + + return s; +} + // ---------------------------------------------------------------------------- // OS version // ---------------------------------------------------------------------------- diff --git a/src/msw/utilsgui.cpp b/src/msw/utilsgui.cpp index 68ca45db20..f85d206b05 100644 --- a/src/msw/utilsgui.cpp +++ b/src/msw/utilsgui.cpp @@ -104,48 +104,6 @@ bool wxCheckForInterrupt(wxWindow *wnd) return true; } -// MSW only: get user-defined resource from the .res file. -// Returns NULL or newly-allocated memory, so use delete[] to clean up. - -#ifndef __WXMICROWIN__ -char *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType, int* pLen /* = NULL */) -{ - HRSRC hResource = ::FindResource(wxGetInstance(), - resourceName.wx_str(), - resourceType.wx_str()); - if ( hResource == 0 ) - return NULL; - - HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); - if ( hData == 0 ) - return NULL; - - void *theText = ::LockResource(hData); - if ( !theText ) - return NULL; - - // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). - // so we need to find the length of the resource. - int len = ::SizeofResource(wxGetInstance(), hResource); - char *s = new char[len + 1]; - memcpy(s, theText, len); - s[len] = '\0'; // NUL-terminate in case the resource itself wasn't - - // Obsolete in WIN32 -#ifndef __WIN32__ - UnlockResource(hData); -#endif - - // No need?? - // GlobalFree(hData); - - if (pLen) - *pLen = len; - - return s; -} -#endif // __WXMICROWIN__ - // ---------------------------------------------------------------------------- // get display info // ----------------------------------------------------------------------------