Ruby  2.5.0dev(2017-10-22revision60238)
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 }
VALUE eWIN32OLERuntimeError
Definition: win32ole_error.h:4
#define va_init_list(a, b)
Definition: win32ole.h:34
size_t strlen(const char *)
LCID cWIN32OLE_lcid
Definition: win32ole.h:116
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:848
const char term
Definition: id.c:37
void ole_raise(HRESULT hr, VALUE ecs, const char *fmt,...)
IUnknown DWORD
Definition: win32ole.c:32
VALUE rb_str_cat2(VALUE, const char *)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
#define rb_str_new2
Definition: intern.h:835
#define Qnil
Definition: ruby.h:438
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:615
unsigned long VALUE
Definition: ruby.h:85
typedef HRESULT(STDAPICALLTYPE FNCOCREATEINSTANCEEX)(REFCLSID
VALUE rb_eRuntimeError
Definition: error.c:800
void Init_win32ole_error(void)
VALUE rb_vsprintf(const char *, va_list)
Definition: sprintf.c:1446
#define NULL
Definition: _sdbm.c:102
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2900