1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/win32ole/win32ole_error.c
usa 1bce19a7e5 merge revision(s) a0bc3f2a1c2c98f225612101cb4e1ea1a6813546,75a0447c15a7ab017bd4240c2a9cc69b134b80b9,f1699314147bad2cf5550cc582693424fdbc2510: [Backport #16846]
Suppress C4267 "possible loss of data" warnings

	---
	 ext/win32ole/win32ole.c | 2 +-
	 1 file changed, 1 insertion(+), 1 deletion(-)

	Suppress C4267 "possible loss of data" warnings

	Just cast down explicitly.
	---
	 ext/win32ole/win32ole.c | 2 +-
	 1 file changed, 1 insertion(+), 1 deletion(-)

	win32ole: separate global variable declarations and definitions

	https://gcc.gnu.org/gcc-10/changes.html#c

	> * GCC now defaults to `-fno-common`.  As a result, global
	>   variable accesses are more efficient on various targets.  In
	>   C, global variables with multiple tentative definitions now
	>   result in linker errors.  With `-fcommon` such definitions are
	>   silently merged during linking.
	---
	 ext/win32ole/win32ole.c           | 1 +
	 ext/win32ole/win32ole.h           | 4 ++--
	 ext/win32ole/win32ole_error.c     | 3 +++
	 ext/win32ole/win32ole_error.h     | 4 ++--
	 ext/win32ole/win32ole_method.c    | 2 ++
	 ext/win32ole/win32ole_method.h    | 2 +-
	 ext/win32ole/win32ole_record.c    | 2 ++
	 ext/win32ole/win32ole_record.h    | 2 +-
	 ext/win32ole/win32ole_type.c      | 2 ++
	 ext/win32ole/win32ole_type.h      | 2 +-
	 ext/win32ole/win32ole_typelib.c   | 2 ++
	 ext/win32ole/win32ole_typelib.h   | 2 +-
	 ext/win32ole/win32ole_variable.c  | 2 ++
	 ext/win32ole/win32ole_variable.h  | 2 +-
	 ext/win32ole/win32ole_variant.c   | 2 ++
	 ext/win32ole/win32ole_variant.h   | 2 +-
	 ext/win32ole/win32ole_variant_m.c | 2 ++
	 ext/win32ole/win32ole_variant_m.h | 2 +-
	 18 files changed, 29 insertions(+), 11 deletions(-)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67919 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2021-04-04 22:58:46 +00:00

87 lines
2.3 KiB
C

#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));
}
VALUE eWIN32OLERuntimeError;
VALUE eWIN32OLEQueryInterfaceError;
void
Init_win32ole_error(void)
{
/*
* 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);
eWIN32OLEQueryInterfaceError = rb_define_class("WIN32OLEQueryInterfaceError", eWIN32OLERuntimeError);
}