e$B$3$s$K$A$O!"$J$+$`$ie(B(e$B$&e(B)e$B$G$9!#e(B
In message “[ruby-dev:33446] Re: Win32OLE: set encoding to OLE string”
on Jan.28,2008 15:44:11, [email protected] wrote:
| | e$B$"$H!"e(BWIN32OLE.codepage= e$B$G!"e(BCP_ACPe$B0J30$N%3!<%I%Z!<%8$re(B
| | e$B;XDj$5$l$F$$$?>l9g!"L5>r7o$K!“e(Brb_locale_encoding()e$B$9$k$N$Oe(B
| | e$B$^$:$/$J$$$s$G$7$g$&$+!)e(B
|
| … e$B$,!”%3!<%I%Z!<%8$,;XDj2DG=$H$O5$$E$$$F$J$+$C$?$G$9!#e(B
| e$B$H$$$&$3$H$O!“e(Brb_locale_charmap()e$BAjEv$N=hM}$r$+$^$9I,MW$,$”$ke(B
| e$B$G$7$g$&$M!#e(B
| e$B$^$$$C$?$J!#e(B
e$B$7$F$_$^$7$?!#e(B
ACPe$B!“e(BOEMCPe$B!“e(BMACCPe$B$OESCf$GJQ$o$k$o$1$O$”$k$^$$$H$$$&$3$H$r4|BTe(B
e$B$7$F$^$9!#e(B
THREAD_ACPe$B$O!“L>A0$+$i$9$k$HJQ$o$j$=$&$J5$$,$7$^$9$,!”$H$j$“e(B
e$B$($:JQ$o$i$J$$$H?.$8$k$3$H$K$7$^$7$?!#e(B
rubye$B$,CN$i$J$$e(Bcodepagee$B$,Mh$?$ie(Bdummy
encodinge$B$,EPO?$5$l$k$h$&e(B
e$B$K$J$C$F$$$^$9$,!”$^$”<B:]$K;HMQ$5$l$ke(Bencodinge$B$O$[$H$s$ICN$Ce(B
e$B$F$k$O$:$J$N$G$3$s$J$b$s$G!#e(B
CP_UTF7e$B$He(BCP_UTF8e$B$O!"<B$O:#$Oe(Brubye$B$OCN$i$J$$$s$G$9$,!"8e$GDIe(B
e$B2C$7$F$*$-$^$9!#e(B
Index: ext/win32ole/win32ole.c
— ext/win32ole/win32ole.c (revision 15288)
+++ ext/win32ole/win32ole.c (working copy)
@@ -17,6 +17,7 @@
#include “ruby/ruby.h”
#include “ruby/st.h”
+#include “ruby/encoding.h”
#include <ctype.h>
#include <windows.h>
#include <ocidl.h>
@@ -195,7 +196,8 @@ static FNCOCREATEINSTANCEEX *gCoCreateIn
static VALUE com_hash;
static IDispatchVtbl com_vtbl;
static UINT cWIN32OLE_cp = CP_ACP;
-static LCID cWIN32OLE_lcid = LOCALE_SYSTEM_DEFAULT;
+static LCID cWIN32OLE_lcid = LOCALE_SYSTEM_DEFAULT;
+static rb_encoding *cWIN32OLE_enc;
static UINT g_cp_to_check = CP_ACP;
static char g_lcid_to_check[8 + 1];
static VARTYPE g_nil_to = VT_ERROR;
@@ -766,6 +768,67 @@ date2time_str(double date)
return rb_str_new2(szTime);
}
+struct myCPINFOEX {
- UINT MaxCharSize;
- BYTE DefaultChar[2];
- BYTE LeadByte[12];
- WCHAR UnicodeDefaultChar;
- UINT CodePage;
- char CodePageName[MAX_PATH];
+};
-
+static rb_encoding *
+ole_cp2encoding(UINT cp)
+{
- static BOOL (*pGetCPInfoEx)(UINT, DWORD, struct myCPINFOEX *) =
NULL;
- struct myCPINFOEX* buf;
- VALUE enc_name;
- char *enc_cstr;
- int idx;
-
- if (!code_page_installed(cp)) {
- switch(cp) {
- case CP_ACP:
-
cp = GetACP();
-
break;
- case CP_OEMCP:
-
cp = GetOEMCP();
-
break;
- case CP_MACCP:
- case CP_THREAD_ACP:
-
if (!pGetCPInfoEx) {
- pGetCPInfoEx = (BOOL (*)(UINT, DWORD, LPVOID))
-
GetProcAddress(GetModuleHandle("kernel32"), "GetCPInfoEx");
- if (!pGetCPInfoEx) {
-
pGetCPInfoEx = (void*)-1;
- }
-
}
-
buf = ALLOCA_N(struct myCPINFOEX, 1);
-
ZeroMemory(buf, sizeof(struct myCPINFOEX));
-
if (pGetCPInfoEx == (void*)-1 || !pGetCPInfoEx(cp, 0, buf)) {
- rb_raise(eWIN32OLERuntimeError, “cannot map codepage to
encoding.”);
- break; /* never reach here */
-
}
-
cp = buf->CodePage;
-
break;
- case CP_SYMBOL:
- case CP_UTF7:
- case CP_UTF8:
-
// nothing ToDo
-
break;
- default:
-
rb_raise(eWIN32OLERuntimeError, "codepage should be
WIN32OLE::CP_ACP, WIN32OLE::CP_OEMCP, WIN32OLE::CP_MACCP,
WIN32OLE::CP_THREAD_ACP, WIN32OLE::CP_SYMBOL, WIN32OLE::CP_UTF7,
WIN32OLE::CP_UTF8, or installed codepage.");
-
break;
-
}
- }
-
- enc_name = rb_sprintf(“CP%d”, cp);
- idx = rb_enc_find_index(enc_cstr = StringValueCStr(enc_name));
- if (idx < 0)
- idx = rb_define_dummy_encoding(enc_cstr);
- return rb_enc_from_index(idx);
+}
-
static char *
ole_wc2mb(LPWSTR pw)
{
@@ -967,7 +1030,7 @@ static VALUE
ole_wc2vstr(LPWSTR pw, BOOL isfree)
{
char *p = ole_wc2mb(pw);
- VALUE vstr = rb_str_new2(p);
-
VALUE vstr = rb_enc_str_new(p, strlen(p), cWIN32OLE_enc);
if(isfree)
SysFreeString(pw);
free(p);
@@ -1809,13 +1872,10 @@ ole_variant2val(VARIANT *pvar)
case VT_BSTR:
{
@@ -1892,9 +1952,7 @@ ole_variant2val(VARIANT *pvar)
hr = VariantChangeTypeEx(&variant, pvar,
cWIN32OLE_lcid, 0, VT_BSTR);
if (SUCCEEDED(hr) && V_VT(&variant) == VT_BSTR) {
@@ -2630,6 +2688,7 @@ fole_s_set_code_page(VALUE self, VALUE v
break;
}
}
- cWIN32OLE_enc = ole_cp2encoding(cWIN32OLE_cp);
/*
- Should this method return old codepage?
*/
@@ -3613,12 +3672,12 @@ fole_missing(int argc, VALUE *argv, VALU
}
n = strlen(mname);
if(mname[n-1] == ‘=’) {
-
argv[0] = rb_enc_str_new(mname, n-1, cWIN32OLE_enc);
return ole_propertyput(self, argv[0], argv[1]);
}
else {
-
argv[0] = rb_enc_str_new(mname, n, cWIN32OLE_enc);
return ole_invoke(argc, argv, self,
DISPATCH_METHOD|DISPATCH_PROPERTYGET, FALSE);
}
}
@@ -8167,4 +8226,5 @@ Init_win32ole()
eWIN32OLERuntimeError = rb_define_class("WIN32OLERuntimeError",
rb_eRuntimeError);
- cWIN32OLE_enc = ole_cp2encoding(cWIN32OLE_cp);
}
e$B$=$l$G$O!#e(B