summaryrefslogtreecommitdiff
path: root/ext/win32ole/win32ole_error.c
diff options
context:
space:
mode:
authorsuke <suke@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-08-06 09:47:39 +0000
committersuke <suke@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-08-06 09:47:39 +0000
commit75d1a6c9fc178a483e52c5638002fb8b1dd8e318 (patch)
treeb81429b72e7257793648ef6745505914907f786b /ext/win32ole/win32ole_error.c
parentf01485b4ec97d6bbc462f6c7210b2499a97ce398 (diff)
* ext/win32ole/win32ole.c: separate src of WIN32OLERuntimeError
from win32ole.c. * ext/win32ole/win32ole.h: ditto * ext/win32ole/depend: ditto. * ext/win32ole/win32ole_error.c: ditto. * ext/win32ole/win32ole_error.h: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47080 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/win32ole/win32ole_error.c')
-rw-r--r--ext/win32ole/win32ole_error.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/ext/win32ole/win32ole_error.c b/ext/win32ole/win32ole_error.c
new file mode 100644
index 0000000..1357df6
--- /dev/null
+++ b/ext/win32ole/win32ole_error.c
@@ -0,0 +1,82 @@
+#include "win32ole.h"
+
+static VALUE ole_hresult2msg(HRESULT hr);
+
+static VALUE
+ole_hresult2msg(HRESULT hr)
+{
+ VALUE msg = Qnil;
+ char *p_msg = NULL;
+ char *term = NULL;
+ DWORD dwCount;
+
+ char strhr[100];
+ sprintf(strhr, " HRESULT error code:0x%08x\n ", (unsigned)hr);
+ msg = rb_str_new2(strhr);
+ dwCount = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, hr,
+ MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
+ (LPTSTR)&p_msg, 0, NULL);
+ if (dwCount == 0) {
+ dwCount = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, hr, cWIN32OLE_lcid,
+ (LPTSTR)&p_msg, 0, NULL);
+ }
+ if (dwCount > 0) {
+ term = p_msg + strlen(p_msg);
+ while (p_msg < term) {
+ term--;
+ if (*term == '\r' || *term == '\n')
+ *term = '\0';
+ else break;
+ }
+ if (p_msg[0] != '\0') {
+ rb_str_cat2(msg, p_msg);
+ }
+ }
+ LocalFree(p_msg);
+ return msg;
+}
+
+void
+ole_raise(HRESULT hr, VALUE ecs, const char *fmt, ...)
+{
+ va_list args;
+ VALUE msg;
+ VALUE err_msg;
+ va_init_list(args, fmt);
+ msg = rb_vsprintf(fmt, args);
+ va_end(args);
+
+ err_msg = ole_hresult2msg(hr);
+ if(err_msg != Qnil) {
+ rb_str_cat2(msg, "\n");
+ rb_str_append(msg, err_msg);
+ }
+ rb_exc_raise(rb_exc_new_str(ecs, msg));
+}
+
+void
+Init_win32ole_error() {
+ /*
+ * Document-class: WIN32OLERuntimeError
+ *
+ * Raised when OLE processing failed.
+ *
+ * EX:
+ *
+ * obj = WIN32OLE.new("NonExistProgID")
+ *
+ * raises the exception:
+ *
+ * WIN32OLERuntimeError: unknown OLE server: `NonExistProgID'
+ * HRESULT error code:0x800401f3
+ * Invalid class string
+ *
+ */
+ eWIN32OLERuntimeError = rb_define_class("WIN32OLERuntimeError", rb_eRuntimeError);
+}