Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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 
63 void
65 {
66  /*
67  * Document-class: WIN32OLERuntimeError
68  *
69  * Raised when OLE processing failed.
70  *
71  * EX:
72  *
73  * obj = WIN32OLE.new("NonExistProgID")
74  *
75  * raises the exception:
76  *
77  * WIN32OLERuntimeError: unknown OLE server: `NonExistProgID'
78  * HRESULT error code:0x800401f3
79  * Invalid class string
80  *
81  */
82  eWIN32OLERuntimeError = rb_define_class("WIN32OLERuntimeError", rb_eRuntimeError);
83  eWIN32OLEQueryInterfaceError = rb_define_class("WIN32OLEQueryInterfaceError", eWIN32OLERuntimeError);
84 }
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
rb_str_new2
#define rb_str_new2
Definition: intern.h:903
win32ole.h
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:972
sprintf
int sprintf(char *__restrict, const char *__restrict,...) __attribute__((__format__(__printf__
eWIN32OLEQueryInterfaceError
VALUE eWIN32OLEQueryInterfaceError
Definition: win32ole_error.h:5
VALUE
unsigned long VALUE
Definition: ruby.h:102
fmt
const VALUE int int int int int int VALUE char * fmt
Definition: rb_mjit_min_header-2.7.1.h:6423
Init_win32ole_error
void Init_win32ole_error(void)
Definition: win32ole_error.c:64
DWORD
IUnknown DWORD
Definition: win32ole.c:33
rb_str_cat2
#define rb_str_cat2
Definition: intern.h:912
NULL
#define NULL
Definition: _sdbm.c:101
strlen
size_t strlen(const char *)
eWIN32OLERuntimeError
VALUE eWIN32OLERuntimeError
Definition: win32ole_error.h:4
rb_vsprintf
VALUE rb_vsprintf(const char *, va_list)
Definition: sprintf.c:1191
va_list
__gnuc_va_list va_list
Definition: rb_mjit_min_header-2.7.1.h:831
term
const char term
Definition: id.c:37
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:920
va_init_list
#define va_init_list(a, b)
Definition: win32ole.h:34
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:668
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2965
HRESULT
typedef HRESULT(STDAPICALLTYPE FNCOCREATEINSTANCEEX)(REFCLSID
cWIN32OLE_lcid
LCID cWIN32OLE_lcid
Definition: win32ole.h:116
ole_raise
void ole_raise(HRESULT hr, VALUE ecs, const char *fmt,...)
Definition: win32ole_error.c:46
Qnil
#define Qnil
Definition: ruby.h:469
va_end
#define va_end(v)
Definition: rb_mjit_min_header-2.7.1.h:3947