Skip to content

Commit

Permalink
Add C-ext "win32/dll_directory" as an alternative to fiddle
Browse files Browse the repository at this point in the history
  • Loading branch information
larskanis committed Dec 28, 2021
1 parent 1478822 commit b89d503
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
From fd61aba54664f3374e950e260876a64369b5c839 Mon Sep 17 00:00:00 2001
From: Lars Kanis <[email protected]>
Date: Tue, 28 Dec 2021 11:11:42 +0100
Subject: [PATCH] Add C-ext "win32/dll_directory" as an alternative to fiddle

---
ext/win32/dll_directory/depend | 2 ++
ext/win32/dll_directory/dll_directory.c | 42 +++++++++++++++++++++++++
ext/win32/dll_directory/extconf.rb | 3 ++
3 files changed, 47 insertions(+)
create mode 100644 ext/win32/dll_directory/depend
create mode 100644 ext/win32/dll_directory/dll_directory.c
create mode 100644 ext/win32/dll_directory/extconf.rb

diff --git a/ext/win32/dll_directory/depend b/ext/win32/dll_directory/depend
new file mode 100644
index 0000000000..0301ce074c
--- /dev/null
+++ b/ext/win32/dll_directory/depend
@@ -0,0 +1,2 @@
+# AUTOGENERATED DEPENDENCIES START
+# AUTOGENERATED DEPENDENCIES END
diff --git a/ext/win32/dll_directory/dll_directory.c b/ext/win32/dll_directory/dll_directory.c
new file mode 100644
index 0000000000..d44b8f2cb3
--- /dev/null
+++ b/ext/win32/dll_directory/dll_directory.c
@@ -0,0 +1,42 @@
+#include <ruby.h>
+#include <ruby/encoding.h>
+#include <windows.h>
+#include <libloaderapi.h>
+
+#define PTR2NUM(x) (ULL2NUM((LONG_LONG)(x)))
+#define NUM2PTR(x) ((void*)(NUM2ULL(x)))
+
+static VALUE
+s_SetDefaultDllDirectories(VALUE self, VALUE flags)
+{
+ int ret = SetDefaultDllDirectories(RB_NUM2LONG(flags));
+ return RB_INT2NUM(ret);
+}
+
+static VALUE
+s_AddDllDirectory(VALUE self, VALUE dir)
+{
+ void *handle;
+ int encidx = rb_enc_find_index("utf-16le");
+ StringValueCStr(dir);
+ dir = rb_str_export_to_enc(dir, rb_enc_from_index(encidx));
+ handle = AddDllDirectory((PCWSTR)RSTRING_PTR(dir));
+ return PTR2NUM(handle);
+}
+
+static VALUE
+s_RemoveDllDirectory(VALUE self, VALUE handle)
+{
+ int ret = RemoveDllDirectory(NUM2PTR(handle));
+ return RB_INT2NUM(ret);
+}
+
+void
+Init_dll_directory(void)
+{
+ VALUE mWin32 = rb_define_module("Win32");
+ VALUE mDD = rb_define_module_under(mWin32, "DllDirectory");
+ rb_define_singleton_method(mDD, "SetDefaultDllDirectories", s_SetDefaultDllDirectories, 1);
+ rb_define_singleton_method(mDD, "AddDllDirectory", s_AddDllDirectory, 1);
+ rb_define_singleton_method(mDD, "RemoveDllDirectory", s_RemoveDllDirectory, 1);
+}
diff --git a/ext/win32/dll_directory/extconf.rb b/ext/win32/dll_directory/extconf.rb
new file mode 100644
index 0000000000..cfdeafacaf
--- /dev/null
+++ b/ext/win32/dll_directory/extconf.rb
@@ -0,0 +1,3 @@
+if have_library('kernel32', 'AddDllDirectory')
+ create_makefile('win32/dll_directory')
+end
--
2.30.0.windows.1

3 changes: 3 additions & 0 deletions mingw-w64-ruby-head/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
"${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=("https://cache.ruby-lang.org/pub/ruby/snapshot/snapshot-master.tar.xz"
0001-Add-C-ext-win32-dll_directory-as-an-alternative-to-f.patch
0002-Don-t-add-an-exe-file-manifest.patch
ruby.ico
rubyw.ico)
Expand All @@ -29,13 +30,15 @@ noextract=("snapshot-master.tar.xz")
# and set first entry to 'SKIP'

sha256sums=('SKIP'
'a50c81ab8c178689485257f8eaa70bd8184b878a798780084668f838912e2832'
'da079dbfdc3d4e1b976ed15d58244ed6e0df201ec829bead578fb4a7177b9cab'
'6c80ba2ca49840e387a08b7fedc6e7acd298ac2ec853155209efb2af20397b22'
'c30fb04ac0e88ba634daaa811a81ea5a75027646617b895acd14df9518a4a55a')

prepare() {
bsdtar -xf snapshot-master.tar.xz || true
cd ${srcdir}/snapshot-master
patch -p1 -i ${srcdir}/0001-Add-C-ext-win32-dll_directory-as-an-alternative-to-f.patch
patch -p1 -i ${srcdir}/0002-Don-t-add-an-exe-file-manifest.patch
autoreconf -fi
}
Expand Down

0 comments on commit b89d503

Please sign in to comment.