ReactOS  0.4.12-dev-14-gd0c8636
xmldomparser.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Applications
3  * LICENSE: LGPL - See COPYING in the top level directory
4  * FILE: base/applications/msconfig_new/xmldomparser.cpp
5  * PURPOSE: XML DOM Parser
6  * COPYRIGHT: Copyright 2011-2012 Hermes BELUSCA - MAITO <hermes.belusca@sfr.fr>
7  */
8 
9 #include "precomp.h"
10 #include "xmldomparser.hpp"
11 #include "utils.h"
12 #include "stringutils.h"
13 
14 // UTF8 adapted version of ConvertStringToBSTR (see lib/sdk/comsupp/comsupp.c)
15 static BSTR
16 ConvertUTF8StringToBSTR(const char *pSrc)
17 {
18  DWORD cwch;
19  BSTR wsOut(NULL);
20 
21  if (!pSrc) return NULL;
22 
23  /* Compute the needed size with the NULL terminator */
24  cwch = MultiByteToWideChar(CP_UTF8, 0, pSrc, -1, NULL, 0);
25  if (cwch == 0) return NULL;
26 
27  /* Allocate the BSTR (without the NULL terminator) */
28  wsOut = SysAllocStringLen(NULL, cwch - 1);
29  if (!wsOut)
30  {
32  return NULL;
33  }
34 
35  /* Convert the string */
36  if (MultiByteToWideChar(CP_UTF8, 0, pSrc, -1, wsOut, cwch) == 0)
37  {
38  /* We failed, clean everything up */
39  cwch = GetLastError();
40 
41  SysFreeString(wsOut);
42  wsOut = NULL;
43 
44  _com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
45  }
46 
47  return wsOut;
48 }
49 
50 HRESULT
52 {
53  return CoInitialize(NULL);
54 }
55 
56 VOID
58 {
60  return;
61 }
62 
63 HRESULT
65 {
66  HRESULT hr = CoCreateInstance(CLSID_DOMDocument30, // __uuidof(DOMDocument30), // NOTE: Do not use DOMDocument60 if you want MSConfig working by default on XP.
67  NULL,
68  CLSCTX_INPROC_SERVER,
69  IID_PPV_ARG(IXMLDOMDocument, ppDoc) /* IID_PPV_ARGS(ppDoc) */);
70  if (!SUCCEEDED(hr))
71  return hr;
72 
73  /* These methods should not fail so don't inspect result */
74  (*ppDoc)->put_async(VARIANT_FALSE);
75  (*ppDoc)->put_validateOnParse(VARIANT_FALSE);
76  (*ppDoc)->put_resolveExternals(VARIANT_FALSE);
77 
78  return hr;
79 }
80 
81 BOOL
83  LPCWSTR lpszXMLResName)
84 {
85  VARIANT_BOOL Success = VARIANT_FALSE;
86  HRSRC hRes;
88  LPVOID lpXMLRes;
89  _bstr_t bstrXMLRes;
90 
91  if (!pDoc)
92  return FALSE;
93 
94  // hRes = FindResourceExW(NULL, RT_HTML, lpszXMLResName, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
95  hRes = FindResourceW(NULL, lpszXMLResName, RT_HTML);
96  if (hRes == NULL)
97  return FALSE;
98 
99  handle = LoadResource(NULL, hRes);
100  if (handle == NULL)
101  return FALSE;
102 
103  lpXMLRes = LockResource(handle);
104  if (lpXMLRes == NULL)
105  goto Cleanup;
106 
107  /* Convert the resource to UNICODE if needed */
108  if (!IsTextUnicode(lpXMLRes, SizeofResource(NULL, hRes), NULL))
109  bstrXMLRes.Attach(ConvertUTF8StringToBSTR((LPCSTR)lpXMLRes));
110  else
111  bstrXMLRes = (LPCWSTR)lpXMLRes;
112 
113  if (SUCCEEDED(pDoc->loadXML(bstrXMLRes, &Success)) && (Success != VARIANT_TRUE))
114  {
115  IXMLDOMParseError* pXMLErr = NULL;
116  _bstr_t bstrErr;
117 
118  if (SUCCEEDED(pDoc->get_parseError(&pXMLErr)) &&
119  SUCCEEDED(pXMLErr->get_reason(&bstrErr.GetBSTR())))
120  {
121  LPWSTR lpszStr = NULL;
122 
123  if (IS_INTRESOURCE((ULONG_PTR)lpszXMLResName))
124  lpszStr = FormatString(L"Failed to load DOM from resource '#%u': %wS", lpszXMLResName, (wchar_t*)bstrErr);
125  else
126  lpszStr = FormatString(L"Failed to load DOM from resource '%wS': %wS", lpszXMLResName, (wchar_t*)bstrErr);
127 
128  MessageBoxW(NULL, lpszStr, L"Error", MB_ICONERROR | MB_OK);
129 
130  MemFree(lpszStr);
131  }
132 
133  SAFE_RELEASE(pXMLErr);
134  }
135 
136 Cleanup:
137  FreeResource(handle);
138 
139  return (Success == VARIANT_TRUE);
140 }
141 
142 BOOL
144  LPCWSTR lpszFilename,
145  BOOL bIgnoreErrorsIfNonExistingFile)
146 {
147  VARIANT_BOOL Success = VARIANT_FALSE;
148  _variant_t varFileName(lpszFilename);
149 
150  if (!pDoc)
151  return FALSE;
152 
153  if (SUCCEEDED(pDoc->load(varFileName, &Success)) && (Success != VARIANT_TRUE))
154  {
155  IXMLDOMParseError* pXMLErr = NULL;
156  LONG lErrorCode = 0L;
157 
158  if (SUCCEEDED(pDoc->get_parseError(&pXMLErr)) &&
159  SUCCEEDED(pXMLErr->get_errorCode(&lErrorCode)))
160  {
161  if ( !bIgnoreErrorsIfNonExistingFile ||
162  ((lErrorCode != _HRESULT_TYPEDEF_(0x800C0006) /* INET_E_OBJECT_NOT_FOUND */) &&
163  (lErrorCode != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))) )
164  {
165  _bstr_t bstrErr;
166 
167  if (SUCCEEDED(pXMLErr->get_reason(&bstrErr.GetBSTR())))
168  {
169  LPWSTR lpszStr = FormatString(L"Failed to load DOM from '%wS': %wS", lpszFilename, (wchar_t*)bstrErr);
170  MessageBoxW(NULL, lpszStr, L"Error", MB_ICONERROR | MB_OK);
171  MemFree(lpszStr);
172  }
173  }
174  }
175 
176  SAFE_RELEASE(pXMLErr);
177  }
178 
179  return (Success == VARIANT_TRUE);
180 }
VOID UninitXMLDOMParser(VOID)
namespace GUID const ADDRINFOEXW ADDRINFOEXW struct timeval OVERLAPPED LPLOOKUPSERVICE_COMPLETION_ROUTINE HANDLE * handle
Definition: sock.c:82
void WINAPI _com_issue_error(HRESULT hr)
Definition: comsupp.cpp:31
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
BOOL MemFree(IN PVOID lpMem)
Definition: utils.c:26
#define _HRESULT_TYPEDEF_(x)
Definition: winerror.h:2351
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
Definition: oleaut.c:343
void WINAPI SysFreeString(BSTR str)
Definition: oleaut.c:275
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
OLECHAR * BSTR
Definition: compat.h:1927
DWORD FormatString(DWORD dwFlags, HINSTANCE hInstance, DWORD dwStringId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, va_list *Arguments)
Definition: fontview.c:48
HRESULT load([in] VARIANT xmlSource, [out, retval] VARIANT_BOOL *isSuccessful)
#define IID_PPV_ARG(Itype, ppType)
short VARIANT_BOOL
Definition: compat.h:1924
HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
Definition: res.c:176
#define RT_HTML
Definition: winuser.h:618
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define CP_UTF8
Definition: nls.h:20
long LONG
Definition: pedump.c:60
BOOL WINAPI FreeResource(HGLOBAL handle)
Definition: res.c:559
HRESULT InitXMLDOMParser(VOID)
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
smooth NULL
Definition: ftsmooth.c:416
HRESULT CreateAndInitXMLDOMDocument(IXMLDOMDocument **ppDoc)
#define SAFE_RELEASE(p)
const char * LPCSTR
Definition: xmlstorage.h:183
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI IsTextUnicode(IN CONST VOID *lpv, IN INT iSize, IN OUT LPINT lpiResult OPTIONAL)
Definition: unicode.c:27
#define IS_INTRESOURCE(i)
Definition: winuser.h:580
int WINAPI MessageBoxW(_In_opt_ HWND, _In_opt_ LPCWSTR, _In_opt_ LPCWSTR, _In_ UINT)
LONG HRESULT
Definition: typedefs.h:77
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL LoadXMLDocumentFromFile(IXMLDOMDocument *pDoc, LPCWSTR lpszFilename, BOOL bIgnoreErrorsIfNonExistingFile)
static BSTR ConvertUTF8StringToBSTR(const char *pSrc)
static const WCHAR L[]
Definition: oid.c:1087
#define MB_ICONERROR
Definition: winuser.h:781
HRESULT loadXML([in] BSTR bstrXML, [out, retval] VARIANT_BOOL *isSuccessful)
static const WCHAR Cleanup[]
Definition: register.c:80
HRESULT WINAPI DECLSPEC_HOTPATCH CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID iid, LPVOID *ppv)
Definition: compobj.c:3237
void WINAPI DECLSPEC_HOTPATCH CoUninitialize(void)
Definition: compobj.c:1994
#define IS_ERROR(stat)
Definition: winerror.h:74
#define MB_OK
Definition: winuser.h:784
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
#define MultiByteToWideChar
Definition: compat.h:100
HRESULT WINAPI CoInitialize(LPVOID lpReserved)
Definition: compobj.c:1900
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL LoadXMLDocumentFromResource(IXMLDOMDocument *pDoc, LPCWSTR lpszXMLResName)
#define SUCCEEDED(hr)
Definition: intsafe.h:57
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define LPCWSTR
Definition: msvc.h:45