Skip to content

Commit 068394a

Browse files
committed
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
1 parent 5deaacb commit 068394a

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

ext/win32ole/win32ole.c

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static void ole_freeexceptinfo(EXCEPINFO *pExInfo);
9999
static VALUE ole_excepinfo2msg(EXCEPINFO *pExInfo);
100100
static void ole_free(void *ptr);
101101
static size_t ole_size(const void *ptr);
102-
static LPWSTR ole_mb2wc(char *pm, int len);
102+
static LPWSTR ole_mb2wc(char *pm, int len, UINT cp);
103103
static VALUE ole_ary_m_entry(VALUE val, LONG *pid);
104104
static VALUE is_all_index_under(LONG *pid, long *pub, long dim);
105105
static void * get_ptr_of_variant(VARIANT *pvar);
@@ -853,7 +853,6 @@ ole_vstr2wc(VALUE vstr)
853853
{
854854
rb_encoding *enc;
855855
int cp;
856-
UINT size = 0;
857856
LPWSTR pw;
858857
st_data_t data;
859858
struct st_table *tbl = DATA_PTR(enc2cp_hash);
@@ -877,58 +876,40 @@ ole_vstr2wc(VALUE vstr)
877876
rb_raise(eWIN32OLERuntimeError, "not installed Windows codepage(%d) according to `%s'", cp, rb_enc_name(enc));
878877
}
879878
}
880-
if (conv_51932(cp)) {
881-
#ifndef pIMultiLanguage
882-
DWORD dw = 0;
883-
UINT len = RSTRING_LENINT(vstr);
884-
HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
885-
&dw, cp, RSTRING_PTR(vstr), &len, NULL, &size);
886-
if (FAILED(hr)) {
887-
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
888-
}
889-
pw = SysAllocStringLen(NULL, size);
890-
len = RSTRING_LEN(vstr);
891-
hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
892-
&dw, cp, RSTRING_PTR(vstr), &len, pw, &size);
893-
if (FAILED(hr)) {
894-
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
895-
}
896-
return pw;
897-
#endif
898-
}
899-
size = MultiByteToWideChar(cp, 0, RSTRING_PTR(vstr), RSTRING_LEN(vstr), NULL, 0);
900-
pw = SysAllocStringLen(NULL, size);
901-
MultiByteToWideChar(cp, 0, RSTRING_PTR(vstr), RSTRING_LEN(vstr), pw, size);
879+
pw = ole_mb2wc(RSTRING_PTR(vstr), RSTRING_LENINT(vstr), cp);
880+
RB_GC_GUARD(vstr);
902881
return pw;
903882
}
904883

905884
static LPWSTR
906-
ole_mb2wc(char *pm, int len)
885+
ole_mb2wc(char *pm, int len, UINT cp)
907886
{
908887
UINT size = 0;
909888
LPWSTR pw;
910889

911-
if (conv_51932(cWIN32OLE_cp)) {
890+
if (conv_51932(cp)) {
912891
#ifndef pIMultiLanguage
913892
DWORD dw = 0;
914893
UINT n = len;
915894
HRESULT hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
916-
&dw, cWIN32OLE_cp, pm, &n, NULL, &size);
895+
&dw, cp, pm, &n, NULL, &size);
917896
if (FAILED(hr)) {
918-
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cWIN32OLE_cp);
897+
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
919898
}
920899
pw = SysAllocStringLen(NULL, size);
900+
n = len;
921901
hr = pIMultiLanguage->lpVtbl->ConvertStringToUnicode(pIMultiLanguage,
922-
&dw, cWIN32OLE_cp, pm, &n, pw, &size);
902+
&dw, cp, pm, &n, pw, &size);
923903
if (FAILED(hr)) {
924-
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cWIN32OLE_cp);
904+
ole_raise(hr, eWIN32OLERuntimeError, "fail to convert CP%d to Unicode", cp);
925905
}
926906
return pw;
927907
#endif
928908
}
929-
size = MultiByteToWideChar(cWIN32OLE_cp, 0, pm, len, NULL, 0);
930-
pw = SysAllocStringLen(NULL, size - 1);
931-
MultiByteToWideChar(cWIN32OLE_cp, 0, pm, len, pw, size);
909+
size = MultiByteToWideChar(cp, 0, pm, len, NULL, 0);
910+
pw = SysAllocStringLen(NULL, size);
911+
pw[size-1] = 0;
912+
MultiByteToWideChar(cp, 0, pm, len, pw, size);
932913
return pw;
933914
}
934915

@@ -1839,7 +1820,7 @@ clsid_from_remote(VALUE host, VALUE com, CLSID *pclsid)
18391820
len = sizeof(clsid);
18401821
err = RegQueryValueEx(hpid, "", NULL, &dwtype, (BYTE *)clsid, &len);
18411822
if (err == ERROR_SUCCESS && dwtype == REG_SZ) {
1842-
pbuf = ole_mb2wc(clsid, -1);
1823+
pbuf = ole_mb2wc(clsid, -1, cWIN32OLE_cp);
18431824
hr = CLSIDFromString(pbuf, pclsid);
18441825
SysFreeString(pbuf);
18451826
}

0 commit comments

Comments
 (0)