@@ -56,82 +56,82 @@ namespace _com_util
5656
5757inline BSTR WINAPI ConvertStringToBSTR (const char *pSrc)
5858{
59- DWORD cwch;
60- BSTR wsOut = nullptr ;
61-
62- if (!pSrc)
63- return nullptr ;
64-
65- // Compute the needed size with the null terminator
66- cwch = MultiByteToWideChar (CP_ACP, 0 , pSrc, -1 , nullptr , 0 );
67- if (cwch == 0 )
68- return nullptr ;
69-
70- // Allocate the BSTR (without the null terminator)
71- wsOut = SysAllocStringLen (nullptr , cwch - 1 );
72- if (!wsOut)
73- {
74- _com_issue_error (HRESULT_FROM_WIN32 (ERROR_OUTOFMEMORY));
75- return nullptr ;
76- }
77-
78- // Convert the string
79- if (MultiByteToWideChar (CP_ACP, 0 , pSrc, -1 , wsOut, cwch) == 0 )
80- {
81- // We failed, clean everything up
82- cwch = GetLastError ();
83-
84- SysFreeString (wsOut);
85- wsOut = nullptr ;
86-
87- _com_issue_error (!IS_ERROR (cwch) ? HRESULT_FROM_WIN32 (cwch) : cwch);
88- }
89-
90- return wsOut;
59+ DWORD cwch;
60+ BSTR wsOut = nullptr ;
61+
62+ if (!pSrc)
63+ return nullptr ;
64+
65+ // Compute the needed size with the null terminator
66+ cwch = MultiByteToWideChar (CP_ACP, 0 , pSrc, -1 , nullptr , 0 );
67+ if (cwch == 0 )
68+ return nullptr ;
69+
70+ // Allocate the BSTR (without the null terminator)
71+ wsOut = SysAllocStringLen (nullptr , cwch - 1 );
72+ if (!wsOut)
73+ {
74+ _com_issue_error (HRESULT_FROM_WIN32 (ERROR_OUTOFMEMORY));
75+ return nullptr ;
76+ }
77+
78+ // Convert the string
79+ if (MultiByteToWideChar (CP_ACP, 0 , pSrc, -1 , wsOut, cwch) == 0 )
80+ {
81+ // We failed, clean everything up
82+ cwch = GetLastError ();
83+
84+ SysFreeString (wsOut);
85+ wsOut = nullptr ;
86+
87+ _com_issue_error (!IS_ERROR (cwch) ? HRESULT_FROM_WIN32 (cwch) : cwch);
88+ }
89+
90+ return wsOut;
9191}
9292
9393inline char * WINAPI ConvertBSTRToString (BSTR pSrc)
9494{
95- DWORD cb, cwch;
96- char *szOut = nullptr ;
97-
98- if (!pSrc)
99- return nullptr ;
100-
101- // Retrieve the size of the BSTR with the null terminator
102- cwch = SysStringLen (pSrc) + 1 ;
103-
104- // Compute the needed size with the null terminator
105- cb = WideCharToMultiByte (CP_ACP, 0 , pSrc, cwch, nullptr , 0 , nullptr , nullptr );
106- if (cb == 0 )
107- {
108- cwch = GetLastError ();
109- _com_issue_error (!IS_ERROR (cwch) ? HRESULT_FROM_WIN32 (cwch) : cwch);
110- return nullptr ;
111- }
112-
113- // Allocate the string
114- szOut = (char *)::operator new (cb * sizeof (char ));
115- if (!szOut)
116- {
117- _com_issue_error (HRESULT_FROM_WIN32 (ERROR_OUTOFMEMORY));
118- return nullptr ;
119- }
120-
121- // Convert the string and null-terminate
122- szOut[cb - 1 ] = ' \0 ' ;
123- if (WideCharToMultiByte (CP_ACP, 0 , pSrc, cwch, szOut, cb, nullptr , nullptr ) == 0 )
124- {
125- // We failed, clean everything up
126- cwch = GetLastError ();
127-
128- ::operator delete (szOut);
129- szOut = nullptr ;
130-
131- _com_issue_error (!IS_ERROR (cwch) ? HRESULT_FROM_WIN32 (cwch) : cwch);
132- }
133-
134- return szOut;
95+ DWORD cb, cwch;
96+ char *szOut = nullptr ;
97+
98+ if (!pSrc)
99+ return nullptr ;
100+
101+ // Retrieve the size of the BSTR with the null terminator
102+ cwch = SysStringLen (pSrc) + 1 ;
103+
104+ // Compute the needed size with the null terminator
105+ cb = WideCharToMultiByte (CP_ACP, 0 , pSrc, cwch, nullptr , 0 , nullptr , nullptr );
106+ if (cb == 0 )
107+ {
108+ cwch = GetLastError ();
109+ _com_issue_error (!IS_ERROR (cwch) ? HRESULT_FROM_WIN32 (cwch) : cwch);
110+ return nullptr ;
111+ }
112+
113+ // Allocate the string
114+ szOut = (char *)::operator new (cb * sizeof (char ));
115+ if (!szOut)
116+ {
117+ _com_issue_error (HRESULT_FROM_WIN32 (ERROR_OUTOFMEMORY));
118+ return nullptr ;
119+ }
120+
121+ // Convert the string and null-terminate
122+ szOut[cb - 1 ] = ' \0 ' ;
123+ if (WideCharToMultiByte (CP_ACP, 0 , pSrc, cwch, szOut, cb, nullptr , nullptr ) == 0 )
124+ {
125+ // We failed, clean everything up
126+ cwch = GetLastError ();
127+
128+ ::operator delete (szOut);
129+ szOut = nullptr ;
130+
131+ _com_issue_error (!IS_ERROR (cwch) ? HRESULT_FROM_WIN32 (cwch) : cwch);
132+ }
133+
134+ return szOut;
135135}
136136
137137}
0 commit comments