This replaces Wine's 11 copies of a2bstr/alloc_string_from_narrow/bstr_from_str/_SHStrDupAToBSTR/TLD_MultiByteToBSTR.
From: Alex Henrie alexhenrie24@gmail.com
--- include/Makefile.in | 1 + include/atlconv.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 include/atlconv.h
diff --git a/include/Makefile.in b/include/Makefile.in index e9f0aa8d5fb..a0058266e28 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -27,6 +27,7 @@ SOURCES = \ asysta.idl \ atlbase.h \ atlcom.h \ + atlconv.h \ atliface.idl \ atlthunk.h \ atlwin.h \ diff --git a/include/atlconv.h b/include/atlconv.h new file mode 100644 index 00000000000..bc57937425d --- /dev/null +++ b/include/atlconv.h @@ -0,0 +1,35 @@ +/* + * Copyright 2023 Alex Henrie + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef __ATLCONV_H__ +#define __ATLCONV_H__ + +inline BSTR A2BSTR(const char *s) +{ + BSTR ret; + int len; + + len = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0); + ret = SysAllocStringLen(NULL, len - 1); + if (!ret) return NULL; + MultiByteToWideChar(CP_ACP, 0, s, -1, ret, len); + + return ret; +} + +#endif /* __ATLCONV_H__ */
From: Alex Henrie alexhenrie24@gmail.com
--- dlls/oleaut32/typelib.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
diff --git a/dlls/oleaut32/typelib.c b/dlls/oleaut32/typelib.c index d92d0234c6c..95a1ea00eda 100644 --- a/dlls/oleaut32/typelib.c +++ b/dlls/oleaut32/typelib.c @@ -69,6 +69,7 @@ #include "typelib.h" #include "wine/debug.h" #include "variant.h" +#include "atlconv.h" #include "wine/asm.h" #include "wine/heap.h" #include "wine/list.h" @@ -1644,18 +1645,6 @@ static inline void TLB_FreeCustData(struct list *custdata_list) } }
-static BSTR TLB_MultiByteToBSTR(const char *ptr) -{ - DWORD len; - BSTR ret; - - len = MultiByteToWideChar(CP_ACP, 0, ptr, -1, NULL, 0); - ret = SysAllocStringLen(NULL, len - 1); - if (!ret) return ret; - MultiByteToWideChar(CP_ACP, 0, ptr, -1, ret, len); - return ret; -} - static inline TLBFuncDesc *TLB_get_funcdesc_by_memberid(ITypeInfoImpl *typeinfo, MEMBERID memid) { int i; @@ -3610,7 +3599,7 @@ static ITypeLib2* ITypeLib2_Constructor_MSFT(LPVOID pLib, DWORD dwTLBLength) size >>= 2; name = heap_alloc_zero(size+1); MSFT_Read(name, size, &cx, DO_NOT_SEEK); - pImpLib->name = TLB_MultiByteToBSTR(name); + pImpLib->name = A2BSTR(name); heap_free(name);
pImpLib->guid = MSFT_ReadGuid(oGuid, &cx); @@ -3717,7 +3706,7 @@ static TLBString *SLTG_ReadName(const char *pNameTable, int offset, ITypeLibImpl return tlbstr; }
- tmp_str = TLB_MultiByteToBSTR(pNameTable + offset); + tmp_str = A2BSTR(pNameTable + offset); tlbstr = TLB_append_str(&lib->name_list, tmp_str); SysFreeString(tmp_str);
@@ -3939,7 +3928,7 @@ static sltg_ref_lookup_t *SLTG_DoRefs(SLTG_RefInfo *pRef, ITypeLibImpl *pTL, if(fname[len-1] != '#') FIXME("fname = %s\n", fname); fname[len-1] = '\0'; - import->name = TLB_MultiByteToBSTR(fname); + import->name = A2BSTR(fname); list_add_tail(&pTL->implib_list, &import->entry); } ref_type->pImpTLInfo = import;
From: Alex Henrie alexhenrie24@gmail.com
--- dlls/shlwapi/string.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-)
diff --git a/dlls/shlwapi/string.c b/dlls/shlwapi/string.c index 2c894198239..be2b55e9d02 100644 --- a/dlls/shlwapi/string.c +++ b/dlls/shlwapi/string.c @@ -36,6 +36,7 @@ #include "shlobj.h" #include "mlang.h" #include "ddeml.h" +#include "atlconv.h" #include "wine/debug.h"
#include "resource.h" @@ -370,29 +371,6 @@ HRESULT WINAPI StrRetToStrW(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPWSTR *p return hRet; }
-/* Makes a Unicode copy of an ANSI string using SysAllocString() */ -static HRESULT _SHStrDupAToBSTR(LPCSTR src, BSTR *pBstrOut) -{ - *pBstrOut = NULL; - - if (src) - { - INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0); - WCHAR* szTemp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); - - if (szTemp) - { - MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len); - *pBstrOut = SysAllocString(szTemp); - HeapFree(GetProcessHeap(), 0, szTemp); - - if (*pBstrOut) - return S_OK; - } - } - return E_OUTOFMEMORY; -} - /************************************************************************* * StrRetToBSTR [SHLWAPI.@] * @@ -421,11 +399,13 @@ HRESULT WINAPI StrRetToBSTR(STRRET *lpStrRet, LPCITEMIDLIST pidl, BSTR* pBstrOut break;
case STRRET_CSTR: - hRet = _SHStrDupAToBSTR(lpStrRet->u.cStr, pBstrOut); + *pBstrOut = A2BSTR(lpStrRet->u.cStr); + hRet = *pBstrOut ? S_OK : E_OUTOFMEMORY; break;
case STRRET_OFFSET: - hRet = _SHStrDupAToBSTR(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, pBstrOut); + *pBstrOut = A2BSTR(((const char*)&pidl->mkid) + lpStrRet->u.uOffset); + hRet = *pBstrOut ? S_OK : E_OUTOFMEMORY; break;
default:
From: Alex Henrie alexhenrie24@gmail.com
--- programs/wscript/tests/run.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-)
diff --git a/programs/wscript/tests/run.c b/programs/wscript/tests/run.c index 7bccf097f87..b996daa6254 100644 --- a/programs/wscript/tests/run.c +++ b/programs/wscript/tests/run.c @@ -26,6 +26,7 @@ #include <windows.h> #include <psapi.h> #include <oaidl.h> +#include <atlconv.h>
#include "wine/test.h"
@@ -74,18 +75,6 @@ static const GUID CLSID_TestObj = static const char *script_name; static HANDLE wscript_process;
-static BSTR a2bstr(const char *str) -{ - BSTR ret; - int len; - - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - ret = SysAllocStringLen(NULL, len-1); - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - - return ret; -} - static HRESULT WINAPI Dispatch_QueryInterface(IDispatch *iface, REFIID riid, void **ppv) { if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDispatch)) { @@ -239,7 +228,7 @@ static HRESULT WINAPI Dispatch_Invoke(IDispatch *iface, DISPID dispIdMember, REF res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, &pos); if(!res || res > sizeof(fullPath)) return E_FAIL; - if(!(V_BSTR(pVarResult) = a2bstr(pos))) + if(!(V_BSTR(pVarResult) = A2BSTR(pos))) return E_OUTOFMEMORY; break; } @@ -255,7 +244,7 @@ static HRESULT WINAPI Dispatch_Invoke(IDispatch *iface, DISPID dispIdMember, REF res = GetFullPathNameA(script_name, sizeof(fullPath), fullPath, NULL); if(!res || res > sizeof(fullPath)) return E_FAIL; - if(!(V_BSTR(pVarResult) = a2bstr(fullPath))) + if(!(V_BSTR(pVarResult) = A2BSTR(fullPath))) return E_OUTOFMEMORY; break; }
From: Alex Henrie alexhenrie24@gmail.com
--- dlls/msxml3/tests/domdoc.c | 13 +++---------- dlls/msxml3/tests/httpreq.c | 11 ++--------- dlls/msxml3/tests/saxreader.c | 11 ++--------- dlls/msxml3/tests/schema.c | 13 +++---------- 4 files changed, 10 insertions(+), 38 deletions(-)
diff --git a/dlls/msxml3/tests/domdoc.c b/dlls/msxml3/tests/domdoc.c index cf9fb62a65e..7bcafd228a1 100644 --- a/dlls/msxml3/tests/domdoc.c +++ b/dlls/msxml3/tests/domdoc.c @@ -42,6 +42,7 @@ #include "initguid.h" #include "asptlb.h" #include "shlwapi.h" +#include "atlconv.h"
#include "wine/test.h"
@@ -1178,7 +1179,7 @@ static const WCHAR nonexistent_fileW[] = L"c:\Nonexistent.xml"; static const WCHAR szStrangeChars[] = L"&x \x2103";
#define expect_bstr_eq_and_free(bstr, expect) { \ - BSTR bstrExp = alloc_str_from_narrow(expect); \ + BSTR bstrExp = A2BSTR(expect); \ ok(lstrcmpW(bstr, bstrExp) == 0, "String differs\n"); \ SysFreeString(bstr); \ SysFreeString(bstrExp); \ @@ -1211,21 +1212,13 @@ static void* _create_object(const GUID *clsid, const char *name, const IID *iid, #define create_cache(iid) _create_object(&_create(CLSID_XMLSchemaCache), iid, __LINE__) #define create_xsltemplate(iid) _create_object(&_create(CLSID_XSLTemplate), iid, __LINE__)
-static BSTR alloc_str_from_narrow(const char *str) -{ - int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */ - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - return ret; -} - static BSTR alloced_bstrs[256]; static int alloced_bstrs_count;
static BSTR _bstr_(const char *str) { assert(alloced_bstrs_count < ARRAY_SIZE(alloced_bstrs)); - alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str); + alloced_bstrs[alloced_bstrs_count] = A2BSTR(str); return alloced_bstrs[alloced_bstrs_count++]; }
diff --git a/dlls/msxml3/tests/httpreq.c b/dlls/msxml3/tests/httpreq.c index bccfbaf582a..25a88f92bb1 100644 --- a/dlls/msxml3/tests/httpreq.c +++ b/dlls/msxml3/tests/httpreq.c @@ -30,6 +30,7 @@ #include "msxml2.h" #include "msxml2did.h" #include "dispex.h" +#include "atlconv.h"
#include "initguid.h" #include "objsafe.h" @@ -112,14 +113,6 @@ static int strcmp_wa(const WCHAR *strw, const char *stra) return lstrcmpW(strw, buf); }
-static BSTR alloc_str_from_narrow(const char *str) -{ - int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */ - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len-1); - return ret; -} - static BSTR alloced_bstrs[256]; static int alloced_bstrs_count;
@@ -129,7 +122,7 @@ static BSTR _bstr_(const char *str) return NULL;
assert(alloced_bstrs_count < ARRAY_SIZE(alloced_bstrs)); - alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str); + alloced_bstrs[alloced_bstrs_count] = A2BSTR(str); return alloced_bstrs[alloced_bstrs_count++]; }
diff --git a/dlls/msxml3/tests/saxreader.c b/dlls/msxml3/tests/saxreader.c index e123d4eba5a..af07379dc3f 100644 --- a/dlls/msxml3/tests/saxreader.c +++ b/dlls/msxml3/tests/saxreader.c @@ -32,6 +32,7 @@ #include "msxml2did.h" #include "ocidl.h" #include "dispex.h" +#include "atlconv.h"
#include "wine/test.h"
@@ -86,21 +87,13 @@ static BOOL is_clsid_supported(const GUID *clsid, const struct msxmlsupported_da return FALSE; }
-static BSTR alloc_str_from_narrow(const char *str) -{ - int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */ - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - return ret; -} - static BSTR alloced_bstrs[512]; static int alloced_bstrs_count;
static BSTR _bstr_(const char *str) { assert(alloced_bstrs_count < ARRAY_SIZE(alloced_bstrs)); - alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str); + alloced_bstrs[alloced_bstrs_count] = A2BSTR(str); return alloced_bstrs[alloced_bstrs_count++]; }
diff --git a/dlls/msxml3/tests/schema.c b/dlls/msxml3/tests/schema.c index 3d209c0e4a0..37b69576c00 100644 --- a/dlls/msxml3/tests/schema.c +++ b/dlls/msxml3/tests/schema.c @@ -31,6 +31,7 @@ #include "msxml2did.h" #include "dispex.h" #include "cguid.h" +#include "atlconv.h"
#include "wine/test.h"
@@ -431,18 +432,10 @@ static ULONG get_refcount(void *iface) static BSTR alloced_bstrs[256]; static int alloced_bstrs_count;
-static BSTR alloc_str_from_narrow(const char *str) -{ - int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */ - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - return ret; -} - static BSTR _bstr_(const char *str) { assert(alloced_bstrs_count < ARRAY_SIZE(alloced_bstrs)); - alloced_bstrs[alloced_bstrs_count] = alloc_str_from_narrow(str); + alloced_bstrs[alloced_bstrs_count] = A2BSTR(str); return alloced_bstrs[alloced_bstrs_count++]; }
@@ -1217,7 +1210,7 @@ L"<?xml version='1.0'?>" hr = IXMLDOMDocument2_QueryInterface(schema, &IID_IDispatch, (void**)&V_DISPATCH(&v)); ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); ok(V_DISPATCH(&v) != NULL, "failed to get IDispatch interface\n"); - namespace = alloc_str_from_narrow("urn:test"); + namespace = A2BSTR("urn:test"); hr = IXMLDOMSchemaCollection_add(cache, namespace, v); SysFreeString(namespace); VariantClear(&v);
From: Alex Henrie alexhenrie24@gmail.com
--- dlls/scrrun/tests/filesystem.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
diff --git a/dlls/scrrun/tests/filesystem.c b/dlls/scrrun/tests/filesystem.c index e53bd092ce9..17872c78c55 100644 --- a/dlls/scrrun/tests/filesystem.c +++ b/dlls/scrrun/tests/filesystem.c @@ -28,6 +28,7 @@ #include "oleauto.h" #include "dispex.h" #include "shlwapi.h" +#include "atlconv.h"
#include "wine/test.h"
@@ -890,14 +891,6 @@ static void test_CopyFolder(void) SysFreeString(bsrc); }
-static BSTR bstr_from_str(const char *str) -{ - int len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - BSTR ret = SysAllocStringLen(NULL, len - 1); /* NUL character added automatically */ - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - return ret; -} - struct buildpath_test { const char *path; @@ -938,7 +931,7 @@ static void test_BuildPath(void) SysFreeString(ret);
ret = (BSTR)0xdeadbeef; - path = bstr_from_str("path"); + path = A2BSTR("path"); hr = IFileSystem3_BuildPath(fs3, path, NULL, &ret); ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); ok(!lstrcmpW(ret, path), "got %s\n", wine_dbgstr_w(ret)); @@ -946,7 +939,7 @@ static void test_BuildPath(void) SysFreeString(path);
ret = (BSTR)0xdeadbeef; - path = bstr_from_str("path"); + path = A2BSTR("path"); hr = IFileSystem3_BuildPath(fs3, NULL, path, &ret); ok(hr == S_OK, "Unexpected hr %#lx.\n", hr); ok(!lstrcmpW(ret, path), "got %s\n", wine_dbgstr_w(ret)); @@ -958,9 +951,9 @@ static void test_BuildPath(void) BSTR name, result;
ret = NULL; - path = bstr_from_str(ptr->path); - name = bstr_from_str(ptr->name); - result = bstr_from_str(ptr->result); + path = A2BSTR(ptr->path); + name = A2BSTR(ptr->name); + result = A2BSTR(ptr->result); hr = IFileSystem3_BuildPath(fs3, path, name, &ret); ok(hr == S_OK, "%d: Unexpected hr %#lx.\n", i, hr); if (hr == S_OK)
From: Alex Henrie alexhenrie24@gmail.com
--- dlls/shell32/tests/shelldispatch.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-)
diff --git a/dlls/shell32/tests/shelldispatch.c b/dlls/shell32/tests/shelldispatch.c index 67659f72345..d8078679627 100644 --- a/dlls/shell32/tests/shelldispatch.c +++ b/dlls/shell32/tests/shelldispatch.c @@ -26,6 +26,7 @@ #include "shlobj.h" #include "shlwapi.h" #include "winsvc.h" +#include "atlconv.h"
#include "wine/test.h"
@@ -50,22 +51,10 @@ static HRESULT (WINAPI *pSHGetNameFromIDList)(PCIDLIST_ABSOLUTE,SIGDN,PWSTR*); /* Updated Windows 7 has a new IShellDispatch6 in its typelib */ DEFINE_GUID(IID_IWin7ShellDispatch6, 0x34936ba1, 0x67ad, 0x4c41, 0x99,0xb8, 0x8c,0x12,0xdf,0xf1,0xe9,0x74);
-static BSTR a2bstr(const char *str) -{ - BSTR ret; - int len; - - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - ret = SysAllocStringLen(NULL, len); - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - - return ret; -} - static void variant_set_string(VARIANT *v, const char *s) { V_VT(v) = VT_BSTR; - V_BSTR(v) = a2bstr(s); + V_BSTR(v) = A2BSTR(s); }
static void init_function_pointers(void) @@ -647,7 +636,7 @@ static void test_items(void) "file_defs[%d]: expected %s, got %s\n", i, wine_dbgstr_w(path), wine_dbgstr_w(bstr)); SysFreeString(bstr);
- bstr = a2bstr(file_defs[i].name); + bstr = A2BSTR(file_defs[i].name); r = FolderItem_get_Name(item, &name); ok(r == S_OK, "Failed to get item name, hr %#lx.\n", r); /* Returned display name does not have to strictly match file name, e.g. extension could be omitted. */
From: Alex Henrie alexhenrie24@gmail.com
--- dlls/vbscript/tests/createobj.c | 17 ++----- dlls/vbscript/tests/run.c | 89 +++++++++++++++------------------ dlls/vbscript/tests/vbscript.c | 25 +++------ 3 files changed, 49 insertions(+), 82 deletions(-)
diff --git a/dlls/vbscript/tests/createobj.c b/dlls/vbscript/tests/createobj.c index 552a849ea27..e1a453414f7 100644 --- a/dlls/vbscript/tests/createobj.c +++ b/dlls/vbscript/tests/createobj.c @@ -27,6 +27,7 @@ #include <objsafe.h> #include <urlmon.h> #include <mshtmhst.h> +#include <atlconv.h>
#include "wine/test.h"
@@ -117,18 +118,6 @@ const GUID GUID_CUSTOM_CONFIRMOBJECTSAFETY =
#define VB_E_CANNOT_CREATE_OBJ 0x800a01ad
-static BSTR a2bstr(const char *str) -{ - BSTR ret; - int len; - - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - ret = SysAllocStringLen(NULL, len-1); - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - - return ret; -} - static HRESULT WINAPI ObjectWithSite_QueryInterface(IObjectWithSite *iface, REFIID riid, void **ppv) { ok(0, "unexpected call\n"); @@ -699,7 +688,7 @@ static void _parse_script_a(unsigned line, IActiveScriptParse *parser, const cha BSTR str; HRESULT hres;
- str = a2bstr(script); + str = A2BSTR(script); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL); SysFreeString(str); ok_(__FILE__,line)(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); @@ -710,7 +699,7 @@ static HRESULT parse_script_ae(IActiveScriptParse *parser, const char *script) BSTR str; HRESULT hres;
- str = a2bstr(script); + str = A2BSTR(script); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL); SysFreeString(str);
diff --git a/dlls/vbscript/tests/run.c b/dlls/vbscript/tests/run.c index 65459711061..ef04e275d46 100644 --- a/dlls/vbscript/tests/run.c +++ b/dlls/vbscript/tests/run.c @@ -24,6 +24,7 @@ #include <ole2.h> #include <dispex.h> #include <activscp.h> +#include <atlconv.h>
#include "vbsregexp55.h"
@@ -168,18 +169,6 @@ static IDispatchEx testObj; static HRESULT onerror_hres = E_NOTIMPL; static BOOL strict_enter_script;
-static BSTR a2bstr(const char *str) -{ - BSTR ret; - int len; - - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - ret = SysAllocStringLen(NULL, len-1); - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - - return ret; -} - static const char *vt2a(VARIANT *v) { if(V_VT(v) == (VT_BYREF|VT_VARIANT)) { @@ -303,35 +292,35 @@ static void test_disp(IDispatch *disp) hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex); ok(hres == S_OK, "Could not get IDispatchEx iface: %08lx\n", hres);
- str = a2bstr("publicProp"); + str = A2BSTR("publicProp"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &public_prop_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicProp) failed: %08lx\n", hres);
- str = a2bstr("PUBLICPROP"); + str = A2BSTR("PUBLICPROP"); hres = IDispatchEx_GetDispID(dispex, str, 0, &id); SysFreeString(str); ok(hres == S_OK, "GetDispID(PUBLICPROP) failed: %08lx\n", hres); ok(public_prop_id == id, "id = %ld\n", public_prop_id);
- str = a2bstr("publicPROP2"); + str = A2BSTR("publicPROP2"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &public_prop2_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicProp2) failed: %08lx\n", hres);
- str = a2bstr("defValGet"); + str = A2BSTR("defValGet"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &defvalget_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(defValGet) failed: %08lx\n", hres); ok(defvalget_id == DISPID_VALUE, "id = %ld\n", defvalget_id);
- str = a2bstr("privateProp"); + str = A2BSTR("privateProp"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &id); SysFreeString(str); ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(privateProp) failed: %08lx, expected DISP_E_UNKNOWNNAME\n", hres); ok(id == -1, "id = %ld\n", id);
- str = a2bstr("class_initialize"); + str = A2BSTR("class_initialize"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicProp2) failed: %08lx\n", hres); @@ -429,13 +418,13 @@ static void test_disp(IDispatch *disp) hres = IDispatchEx_InvokeEx(dispex, public_prop_id, 0, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL); ok(hres == DISP_E_PARAMNOTOPTIONAL, "InvokeEx failed: %08lx, expected DISP_E_PARAMNOTOPTIONAL\n", hres);
- str = a2bstr("publicFunction"); + str = A2BSTR("publicFunction"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &public_func_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicFunction) failed: %08lx\n", hres); ok(public_func_id != -1, "public_func_id = -1\n");
- str = a2bstr("publicSub"); + str = A2BSTR("publicSub"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &public_sub_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicSub) failed: %08lx\n", hres); @@ -507,13 +496,13 @@ static void test_disp(IDispatch *disp) ok(hres == S_OK, "InvokeEx failed: %08lx\n", hres); ok(V_VT(&v) == VT_EMPTY, "V_VT(v) = %d\n", V_VT(&v));
- str = a2bstr("privateSub"); + str = A2BSTR("privateSub"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &id); SysFreeString(str); ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(privateSub) failed: %08lx, expected DISP_E_UNKNOWNNAME\n", hres); ok(id == -1, "id = %ld\n", id);
- str = a2bstr("dynprop"); + str = A2BSTR("dynprop"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive|fdexNameEnsure, &id); ok(hres == DISP_E_UNKNOWNNAME, "GetDispID(privateProp) failed: %08lx, expected DISP_E_UNKNOWNNAME\n", hres); ok(id == -1, "id = %ld\n", id); @@ -522,26 +511,26 @@ static void test_disp(IDispatch *disp) ok(id == -1, "id = %ld\n", id); SysFreeString(str);
- str = a2bstr("publicProp"); + str = A2BSTR("publicProp"); hres = IDispatchEx_GetDispID(dispex, str, 0x80000000|fdexNameCaseInsensitive, &public_prop_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicProp) failed: %08lx\n", hres);
id = 0xdeadbeef; - str = a2bstr("publicProp"); + str = A2BSTR("publicProp"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicProp) failed: %08lx\n", hres); ok(id == public_prop_id, "id = %ld, expected %ld\n", id, public_prop_id);
id = 0xdeadbeef; - str = a2bstr("publicprop"); + str = A2BSTR("publicprop"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseSensitive, &id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicProp) failed: %08lx\n", hres); ok(id == public_prop_id, "id = %ld, expected %ld\n", id, public_prop_id);
- str = a2bstr("gsGetProp"); + str = A2BSTR("gsGetProp"); hres = IDispatchEx_GetDispID(dispex, str, fdexNameCaseInsensitive, &gs_getter_id); SysFreeString(str); ok(hres == S_OK, "GetDispID(publicFunction) failed: %08lx\n", hres); @@ -1265,7 +1254,7 @@ static HRESULT WINAPI Global_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, ok(pei != NULL, "pei == NULL\n");
V_VT(pvarRes) = VT_BSTR; - V_BSTR(pvarRes) = a2bstr(vt2a(pdp->rgvarg)); + V_BSTR(pvarRes) = A2BSTR(vt2a(pdp->rgvarg)); return S_OK;
case DISPID_GLOBAL_ISENGLANG: @@ -2123,7 +2112,7 @@ static void parse_script_af(DWORD flags, const char *src) BSTR tmp; HRESULT hres;
- tmp = a2bstr(src); + tmp = A2BSTR(src); hres = parse_script(flags, tmp, NULL); SysFreeString(tmp); ok(hres == S_OK, "parse_script failed: %08lx\n", hres); @@ -2134,7 +2123,7 @@ static HRESULT parse_script_ar(const char *src) BSTR tmp; HRESULT hres;
- tmp = a2bstr(src); + tmp = A2BSTR(src); hres = parse_script(SCRIPTITEM_GLOBALMEMBERS, tmp, NULL); SysFreeString(tmp); return hres; @@ -2160,12 +2149,12 @@ static void test_parse_context(void) ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres);
/* unknown identifier context is not a valid argument */ - str = a2bstr("Call reportSuccess()\n"); + str = A2BSTR("Call reportSuccess()\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, L"y", NULL, NULL, 0, 0, 0, NULL, NULL); ok(hres == E_INVALIDARG, "ParseScriptText failed: %08lx\n", hres); SysFreeString(str);
- str = a2bstr("class Cl\n" + str = A2BSTR("class Cl\n" " Public Sub ClMethod\n" " Call reportSuccess()\n" " End Sub\n" @@ -2177,14 +2166,14 @@ static void test_parse_context(void) SysFreeString(str);
/* known global variable is not a valid context */ - str = a2bstr("Call reportSuccess()\n"); + str = A2BSTR("Call reportSuccess()\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, L"x", NULL, NULL, 0, 0, 0, NULL, NULL); ok(hres == E_INVALIDARG, "ParseScriptText failed: %08lx\n", hres); SysFreeString(str);
SET_EXPECT(global_success_d); SET_EXPECT(global_success_i); - str = a2bstr("Call reportSuccess()\n"); + str = A2BSTR("Call reportSuccess()\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, L"test", NULL, NULL, 0, 0, 0, NULL, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); SysFreeString(str); @@ -2207,7 +2196,7 @@ static void _parse_htmlscript_a(unsigned line, const char *src) BSTR tmp; HRESULT hres;
- tmp = a2bstr(src); + tmp = A2BSTR(src); hres = parse_script(SCRIPTITEM_GLOBALMEMBERS, tmp, L"</SCRIPT>"); SysFreeString(tmp); ok_(__FILE__,line)(hres == S_OK, "parse_script failed: %08lx\n", hres); @@ -2220,7 +2209,7 @@ static IDispatchEx *parse_procedure(IActiveScriptParseProcedure2 *parse_proc, co BSTR str; HRESULT hres;
- str = a2bstr(src); + str = A2BSTR(src); hres = IActiveScriptParseProcedure2_ParseProcedureText(parse_proc, str, NULL, L"", NULL, NULL, L""", 0, 0, SCRIPTPROC_HOSTMANAGESSOURCE|SCRIPTPROC_IMPLICIT_THIS|SCRIPTPROC_IMPLICIT_PARENTS|flags, &disp); SysFreeString(str); @@ -2613,7 +2602,7 @@ static void test_gc(void) hres = IActiveScript_SetScriptState(engine, SCRIPTSTATE_STARTED); ok(hres == S_OK, "SetScriptState(SCRIPTSTATE_STARTED) failed: %08lx\n", hres);
- src = a2bstr( + src = A2BSTR( "class C\n" " Public ref\n" " Public Sub Class_Terminate\n" @@ -2872,7 +2861,7 @@ static HRESULT test_global_vars_ref(BOOL use_close)
refobj_ref = 0;
- script_str = a2bstr("Dim x\nset x = RefObj\n"); + script_str = A2BSTR("Dim x\nset x = RefObj\n"); hres = IActiveScriptParse_ParseScriptText(parser, script_str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL); SysFreeString(script_str);
@@ -2921,7 +2910,7 @@ static void test_isexpression(void) ok(hres == S_OK, "GetScriptState failed: %08lx\n", hres); ok(ss == SCRIPTSTATE_INITIALIZED, "Wrong script state %u\n", ss);
- str = a2bstr("13"); + str = A2BSTR("13"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_I2, "Expected VT_I2, got %s\n", vt2a(&var)); @@ -2934,7 +2923,7 @@ static void test_isexpression(void)
/* Empty expressions */ V_VT(&var) = VT_I2; - str = a2bstr(""); + str = A2BSTR(""); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_EMPTY, "Expected VT_EMPTY, got %s\n", vt2a(&var)); @@ -2942,7 +2931,7 @@ static void test_isexpression(void) SysFreeString(str);
/* Two expressions fail */ - str = a2bstr("1\n3"); + str = A2BSTR("1\n3"); SET_EXPECT(OnScriptError); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(FAILED(hres), "ParseScriptText did not fail: %08lx\n", hres); @@ -2951,7 +2940,7 @@ static void test_isexpression(void) SysFreeString(str);
/* Simple numerical expression */ - str = a2bstr("(1 + 7) * 2 - 3"); + str = A2BSTR("(1 + 7) * 2 - 3"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, NULL, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres);
@@ -2965,7 +2954,7 @@ static void test_isexpression(void) /* Without a global host or named item context, "me" returns the script dispatch */ hres = IActiveScript_GetScriptDispatch(engine, NULL, &disp); ok(hres == S_OK, "GetScriptDispatch failed: %08lx\n", hres); - str = a2bstr("me"); + str = A2BSTR("me"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_DISPATCH, "Expected VT_DISPATCH, got %s\n", vt2a(&var)); @@ -2976,14 +2965,14 @@ static void test_isexpression(void)
/* An expression can also refer to a variable, function, class, etc previously set */ V_VT(&var) = VT_I2; - str = a2bstr("If True Then foo = 42 Else foo = 0\n"); + str = A2BSTR("If True Then foo = 42 Else foo = 0\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_EMPTY, "Expected VT_EMPTY, got %s\n", vt2a(&var)); VariantClear(&var); SysFreeString(str);
- str = a2bstr("foo\n\n"); + str = A2BSTR("foo\n\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_I2, "Expected VT_I2, got %s\n", vt2a(&var)); @@ -2991,7 +2980,7 @@ static void test_isexpression(void) VariantClear(&var); SysFreeString(str);
- str = a2bstr("foo : "); + str = A2BSTR("foo : "); SET_EXPECT(OnScriptError); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(FAILED(hres), "ParseScriptText did not fail: %08lx\n", hres); @@ -2999,7 +2988,7 @@ static void test_isexpression(void) VariantClear(&var); SysFreeString(str);
- str = a2bstr(""foo is " & CStr(foo) \n \n\n "); + str = A2BSTR(""foo is " & CStr(foo) \n \n\n "); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_BSTR, "Expected VT_BSTR, got %s\n", vt2a(&var)); @@ -3007,14 +2996,14 @@ static void test_isexpression(void) VariantClear(&var); SysFreeString(str);
- str = a2bstr("Function test(x)\n" + str = A2BSTR("Function test(x)\n" " test = x + 0.5\n" "End Function\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); SysFreeString(str);
- str = a2bstr("test(4) * 3\n"); + str = A2BSTR("test(4) * 3\n"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_R8, "Expected VT_R8, got %s\n", vt2a(&var)); @@ -3022,7 +3011,7 @@ static void test_isexpression(void) VariantClear(&var); SysFreeString(str);
- str = a2bstr("Class C\n" + str = A2BSTR("Class C\n" " Public x\n" "End Class\n" "Set obj = New C\n" @@ -3031,7 +3020,7 @@ static void test_isexpression(void) ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); SysFreeString(str);
- str = a2bstr("obj.x"); + str = A2BSTR("obj.x"); hres = IActiveScriptParse_ParseScriptText(parser, str, NULL, NULL, NULL, 0, 0, SCRIPTTEXT_ISEXPRESSION, &var, NULL); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); ok(V_VT(&var) == VT_BOOL, "Expected VT_BOOL, got %s\n", vt2a(&var)); diff --git a/dlls/vbscript/tests/vbscript.c b/dlls/vbscript/tests/vbscript.c index 65134fef1af..fbb69809d3c 100644 --- a/dlls/vbscript/tests/vbscript.c +++ b/dlls/vbscript/tests/vbscript.c @@ -25,6 +25,7 @@ #include <activscp.h> #include <objsafe.h> #include <dispex.h> +#include <atlconv.h>
#include "vbsregexp55.h"
@@ -109,18 +110,6 @@ DEFINE_EXPECT(testCall); DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4,0xb0, 0x00,0xaa,0x00,0x4a,0x55,0xe8); DEFINE_GUID(CLSID_VBScriptRegExp, 0x3f4daca4, 0x160d, 0x11d2, 0xa8,0xe9, 0x00,0x10,0x4b,0x36,0x5c,0x9f);
-static BSTR a2bstr(const char *str) -{ - BSTR ret; - int len; - - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - ret = SysAllocStringLen(NULL, len-1); - MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len); - - return ret; -} - #define test_state(s,ss) _test_state(__LINE__,s,ss) static void _test_state(unsigned line, IActiveScript *script, SCRIPTSTATE exstate) { @@ -574,7 +563,7 @@ static void parse_script(IActiveScriptParse *parse, const char *src) SET_EXPECT(OnEnterScript); SET_EXPECT(OnLeaveScript);
- str = a2bstr(src); + str = A2BSTR(src); hres = IActiveScriptParse_ParseScriptText(parse, str, NULL, NULL, NULL, 0, 0, 0, NULL, NULL); SysFreeString(str); ok(hres == S_OK, "ParseScriptText failed: %08lx\n", hres); @@ -590,7 +579,7 @@ static void _get_disp_id(unsigned line, IDispatchEx *dispex, const char *name, H BSTR str; HRESULT hres;
- str = a2bstr(name); + str = A2BSTR(name); hres = IDispatchEx_GetDispID(dispex, str, 0, id); ok_(__FILE__,line)(hres == exhres, "GetDispID(%s) returned %08lx, expected %08lx\n", name, hres, exhres);
@@ -2665,17 +2654,17 @@ static void test_RegExp_Replace(void) hr = IRegExp2_put_Global(regexp, test[i].global ? VARIANT_TRUE : VARIANT_FALSE); ok(hr == S_OK, "got %#lx\n", hr);
- str = a2bstr(test[i].pattern); + str = A2BSTR(test[i].pattern); hr = IRegExp2_put_Pattern(regexp, str); ok(hr == S_OK, "got %#lx\n", hr); SysFreeString(str);
- str = a2bstr(test[i].source); + str = A2BSTR(test[i].source); V_VT(&var) = VT_BSTR; - V_BSTR(&var) = a2bstr(test[i].replace); + V_BSTR(&var) = A2BSTR(test[i].replace); hr = IRegExp2_Replace(regexp, str, var, &ret); ok(hr == S_OK, "got %#lx\n", hr); - result = a2bstr(test[i].result); + result = A2BSTR(test[i].result); ok(!wcscmp(ret, result), "got %s, expected %s\n", wine_dbgstr_w(ret), wine_dbgstr_w(result)); SysFreeString(result); SysFreeString(ret);
I have some unfinished patches to get rid of a2bstr but not by making it generic. The proper fix is to use L-strings instead and avoid a2bstr altogether.
On Thu Jun 29 19:18:07 2023 +0000, Michael Stefaniuc wrote:
I have some unfinished patches to get rid of a2bstr but not by making it generic. The proper fix is to use L-strings instead and avoid a2bstr altogether.
If you send those patches, I'd appreciate it. I suspect there will still be a few places that need A2BSTR, but I agree that using L-strings where possible is better.
On Thu Jun 29 19:18:07 2023 +0000, Alex Henrie wrote:
If you send those patches, I'd appreciate it. I suspect there will still be a few places that need A2BSTR, but I agree that using L-strings where possible is better.
We'll need BSTR when argument type is BSTR, even if WCHAR string is accepted. Cases where we test specifically that WCHAR string works are an exception.
On Thu Jun 29 22:27:25 2023 +0000, Nikolay Sivov wrote:
We'll need BSTR when argument type is BSTR, even if WCHAR string is accepted. Cases where we test specifically that WCHAR string works are an exception.
I thought Michael was saying that in many cases we can create the BSTR with SysAllocString on a WCHAR literal instead of A2BSTR on a char literal.
On Thu Jun 29 22:31:00 2023 +0000, Alex Henrie wrote:
I thought Michael was saying that in many cases we can create the BSTR with SysAllocString on a WCHAR literal instead of A2BSTR on a char literal.
Yes, that makes sense. No reason to have this new helper in tests.