Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
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,
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 }
61}
62
65
66void
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 */
87}
VALUE rb_define_class(const char *, VALUE)
Defines a top-level class.
Definition: class.c:662
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:668
VALUE rb_eRuntimeError
Definition: error.c:922
VALUE rb_exc_new_str(VALUE, VALUE)
Definition: error.c:974
const char term
Definition: id.c:37
#define rb_str_new2
#define NULL
int sprintf(char *__restrict__, const char *__restrict__,...) __attribute__((__format__(__printf__
size_t strlen(const char *)
#define rb_str_cat2
#define va_init_list(a, b)
unsigned long VALUE
const VALUE int int int int int int VALUE char * fmt
#define va_end(v)
__gnuc_va_list va_list
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2965
#define Qnil
VALUE VALUE rb_vsprintf(const char *, va_list)
Definition: sprintf.c:1191
typedef HRESULT(STDAPICALLTYPE FNCOCREATEINSTANCEEX)(REFCLSID
IUnknown DWORD
Definition: win32ole.c:33
LCID cWIN32OLE_lcid
Definition: win32ole.c:3965
VALUE eWIN32OLERuntimeError
VALUE eWIN32OLEQueryInterfaceError
void Init_win32ole_error(void)
void ole_raise(HRESULT hr, VALUE ecs, const char *fmt,...)