Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
win32ole_error.c
Go to the documentation of this file.
1 #include "win32ole.h"
2 
3 static VALUE ole_hresult2msg(HRESULT hr);
4 
5 static VALUE
6 ole_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 
45 void
46 ole_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 
65 
66 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:759
win32ole.h
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:1144
Init_win32ole_error
void Init_win32ole_error(void)
Definition: win32ole_error.c:67
DWORD
IUnknown DWORD
Definition: win32ole.c:33
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:3109
strlen
size_t strlen(const char *)
eWIN32OLEQueryInterfaceError
VALUE eWIN32OLEQueryInterfaceError
Definition: win32ole_error.c:64
cWIN32OLE_lcid
LCID cWIN32OLE_lcid
Definition: win32ole.c:3966
Qnil
#define Qnil
Definition: special_consts.h:51
term
const char term
Definition: id.c:37
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:1091
NULL
#define NULL
Definition: regenc.h:69
rb_str_cat2
VALUE rb_str_cat2(VALUE, const char *)
va_init_list
#define va_init_list(a, b)
Definition: win32ole.h:31
VALUE
unsigned long VALUE
Definition: value.h:38
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:723
HRESULT
typedef HRESULT(STDAPICALLTYPE FNCOCREATEINSTANCEEX)(REFCLSID
rb_str_new2
#define rb_str_new2
Definition: string.h:276
eWIN32OLERuntimeError
VALUE eWIN32OLERuntimeError
Definition: win32ole_error.c:63
ole_raise
void ole_raise(HRESULT hr, VALUE ecs, const char *fmt,...)
Definition: win32ole_error.c:46
rb_vsprintf
VALUE rb_vsprintf(const char *, va_list)
Definition: sprintf.c:1195