From 068394a7d6eb203b583f2b76195a92c847b1c917 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 21 Sep 2015 14:40:57 +0000 Subject: win32ole.c: make ole_mb2wc more generic * ext/win32ole/win32ole.c (ole_vstr2wc): use ole_mb2wc. * ext/win32ole/win32ole.c (ole_mb2wc): parametrize code page. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51911 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/win32ole/win32ole.c | 49 +++++++++++++++---------------------------------- 1 file changed, 15 insertions(+), 34 deletions(-) (limited to 'ext/win32ole/win32ole.c') diff --git a/ext/win32ole/win32ole.c b/ext/win32ole/win32ole.c index 26e6865f4f..0917075206 100644 --- a/ext/win32ole/win32ole.c +++ b/ext/win32ole/win32ole.c @@ -99,7 +99,7 @@ static void ole_freeexceptinfo(EXCEPINFO *pExInfo); static VALUE ole_excepinfo2msg(EXCEPINFO *pExInfo); static void ole_free(void *ptr); static size_t ole_size(const void *ptr); -static LPWSTR ole_mb2wc(char *pm, int len); +static LPWSTR ole_mb2wc(char *pm, int len, UINT cp); static VALUE ole_ary_m_entry(VALUE val, LONG *pid); static VALUE is_all_index_under(LONG *pid, long *pub, long dim); static void * get_ptr_of_variant(VARIANT *pvar); @@ -853,7 +853,6 @@ ole_vstr2wc(VALUE vstr) { rb_encoding *enc; int cp; - UINT size = 0; LPWSTR pw; st_data_t data; struct st_table *tbl = DATA_PTR(enc2cp_hash); @@ -877,58 +876,40 @@ ole_vstr2wc(VALUE vstr) rb_raise(eWIN32OLERuntimeError, "not installed Windows codepage(%d) according to `%s'", cp, rb_enc_name(enc)); } } - if (conv_51932(cp)) { -#ifndef pIMultiLanguage - DWORD dw = 0; - UINT len = RSTRING_LENINT(vstr); - HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage, - &dw, cp, RSTRING_PTR(vstr), &len, NULL, &size); - if (FAILED(hr)) { - ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp); - } - pw = SysAllocStringLen(NULL, size); - len = RSTRING_LEN(vstr); - hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage, - &dw, cp, RSTRING_PTR(vstr), &len, pw, &size); - if (FAILED(hr)) { - ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp); - } - return pw; -#endif - } - size = MultiByteToWideChar(cp, 0, RSTRING_PTR(vstr), RSTRING_LEN(vstr), NULL, 0); - pw = SysAllocStringLen(NULL, size); - MultiByteToWideChar(cp, 0, RSTRING_PTR(vstr), RSTRING_LEN(vstr), pw, size); + pw = ole_mb2wc(RSTRING_PTR(vstr), RSTRING_LENINT(vstr), cp); + RB_GC_GUARD(vstr); return pw; } static LPWSTR -ole_mb2wc(char *pm, int len) +ole_mb2wc(char *pm, int len, UINT cp) { UINT size = 0; LPWSTR pw; - if (conv_51932(cWIN32OLE_cp)) { + if (conv_51932(cp)) { #ifndef pIMultiLanguage DWORD dw = 0; UINT n = len; HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage, - &dw, cWIN32OLE_cp, pm, &n, NULL, &size); + &dw, cp, pm, &n, NULL, &size); if (FAILED(hr)) { - ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cWIN32OLE_cp); + ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp); } pw = SysAllocStringLen(NULL, size); + n = len; hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage, - &dw, cWIN32OLE_cp, pm, &n, pw, &size); + &dw, cp, pm, &n, pw, &size); if (FAILED(hr)) { - ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cWIN32OLE_cp); + ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp); } return pw; #endif } - size = MultiByteToWideChar(cWIN32OLE_cp, 0, pm, len, NULL, 0); - pw = SysAllocStringLen(NULL, size - 1); - MultiByteToWideChar(cWIN32OLE_cp, 0, pm, len, pw, size); + size = MultiByteToWideChar(cp, 0, pm, len, NULL, 0); + pw = SysAllocStringLen(NULL, size); + pw[size-1] = 0; + MultiByteToWideChar(cp, 0, pm, len, pw, size); return pw; } @@ -1839,7 +1820,7 @@ clsid_from_remote(VALUE host, VALUE com, CLSID *pclsid) len = sizeof(clsid); err = RegQueryValueEx(hpid, "", NULL, &dwtype, (BYTE *)clsid, &len); if (err == ERROR_SUCCESS && dwtype == REG_SZ) { - pbuf = ole_mb2wc(clsid, -1); + pbuf = ole_mb2wc(clsid, -1, cWIN32OLE_cp); hr = CLSIDFromString(pbuf, pclsid); SysFreeString(pbuf); } -- cgit v1.2.3