Ruby: ext/win32ole/win32ole_error.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

win32ole_error.c
Go to the documentation of this file.
1#include "win32ole.h"
2
3static VALUE ole_hresult2msg(HRESULT hr);
4
5static VALUE
6ole_hresult2msg(HRESULT hr)
7{
8 VALUE msg = Qnil;
9 char *p_msg = NULL;
10 char *term = NULL;
11 DWORD dwCount;
12
13 char strhr[100];
14 sprintf(strhr, " HRESULT error code:0x%08x\n ", (unsigned)hr);
15 msg = rb_str_new2(strhr);
16 dwCount = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
17 FORMAT_MESSAGE_FROM_SYSTEM |
18 FORMAT_MESSAGE_IGNORE_INSERTS,
19 NULL, hr,
20 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
21 (LPTSTR)&p_msg, 0, NULL);
22 if (dwCount == 0) {
23 dwCount = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
24 FORMAT_MESSAGE_FROM_SYSTEM |
25 FORMAT_MESSAGE_IGNORE_INSERTS,
26 NULL, hr, cWIN32OLE_lcid,
27 (LPTSTR)&p_msg, 0, NULL);
28 }
29 if (dwCount > 0) {
30 term = p_msg + strlen(p_msg);
31 while (p_msg < term) {
32 term--;
33 if (*term == '\r' || *term == '\n')
34 *term = '\0';
35 else break;
36 }
37 if (p_msg[0] != '\0') {
38 rb_str_cat2(msg, p_msg);
39 }
40 }
41 LocalFree(p_msg);
42 return msg;
43}
44
45void
46ole_raise(HRESULT hr, VALUE ecs, const char *fmt, ...)
47{
48 va_list args;
49 VALUE msg;
50 VALUE err_msg;
51 va_init_list(args, fmt);
52 msg = rb_vsprintf(fmt, args);
53 va_end(args);
54
55 err_msg = ole_hresult2msg(hr);
56 if(err_msg != Qnil) {
57 rb_str_cat2(msg, "\n");
58 rb_str_append(msg, err_msg);
59 }
60 rb_exc_raise(rb_exc_new_str(ecs, msg));
61}
62
63 VALUE eWIN32OLERuntimeError;
64 VALUE eWIN32OLEQueryInterfaceError;
65
66void
67Init_win32ole_error(void)
68{
69 /*
70 * Document-class: WIN32OLERuntimeError
71 *
72 * Raised when OLE processing failed.
73 *
74 * EX:
75 *
76 * obj = WIN32OLE.new("NonExistProgID")
77 *
78 * raises the exception:
79 *
80 * WIN32OLERuntimeError: unknown OLE server: `NonExistProgID'
81 * HRESULT error code:0x800401f3
82 * Invalid class string
83 *
84 */
85 eWIN32OLERuntimeError = rb_define_class("WIN32OLERuntimeError", rb_eRuntimeError);
86 eWIN32OLEQueryInterfaceError = rb_define_class("WIN32OLEQueryInterfaceError", eWIN32OLERuntimeError);
87}
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:1055
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:1107
term
const char term
Definition: id.c:37
rb_str_new2
#define rb_str_new2
Definition: string.h:276
rb_str_cat2
#define rb_str_cat2
Definition: string.h:285
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:3118
NULL
#define NULL
Definition: regenc.h:69
Qnil
#define Qnil
Definition: special_consts.h:51
rb_vsprintf
VALUE rb_vsprintf(const char *, va_list)
Definition: sprintf.c:1197
strlen
size_t strlen(const char *)
VALUE
unsigned long VALUE
Definition: value.h:38
HRESULT
typedef HRESULT(STDAPICALLTYPE FNCOCREATEINSTANCEEX)(REFCLSID
DWORD
IUnknown DWORD
Definition: win32ole.c:33
cWIN32OLE_lcid
LCID cWIN32OLE_lcid
Definition: win32ole.c:3966
win32ole.h
va_init_list
#define va_init_list(a, b)
Definition: win32ole.h:31
eWIN32OLERuntimeError
VALUE eWIN32OLERuntimeError
Definition: win32ole_error.c:63
eWIN32OLEQueryInterfaceError
VALUE eWIN32OLEQueryInterfaceError
Definition: win32ole_error.c:64
Init_win32ole_error
void Init_win32ole_error(void)
Definition: win32ole_error.c:67
ole_raise
void ole_raise(HRESULT hr, VALUE ecs, const char *fmt,...)
Definition: win32ole_error.c:46