Skip to content

Commit

Permalink
Add FFI::LastError.winapi_error
Browse files Browse the repository at this point in the history
  • Loading branch information
ahorek authored and headius committed Feb 16, 2020
1 parent bcec463 commit 16ea5d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 19 additions & 0 deletions core/src/main/java/org/jruby/ext/ffi/jffi/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.jruby.ext.ffi.*;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.platform.Platform;
import org.jruby.util.WindowsFFI;

public class Factory extends org.jruby.ext.ffi.Factory {

Expand Down Expand Up @@ -40,6 +42,9 @@ public void init(Ruby runtime, RubyModule ffi) {
}
if (ffi.getClass("LastError") == null) {
ffi.defineModuleUnder("LastError").defineAnnotatedMethods(LastError.class);
if (Platform.IS_WINDOWS) {
ffi.defineModuleUnder("LastError").defineAnnotatedMethods(WinapiLastError.class);
}
}
}

Expand Down Expand Up @@ -122,4 +127,18 @@ public static final IRubyObject error_set(ThreadContext context, IRubyObject re
return value;
}
}

public static final class WinapiLastError {
@JRubyMethod(name = { "winapi_error" }, module = true)
public static final IRubyObject winapi_error(ThreadContext context, IRubyObject recv) {
return context.runtime.newFixnum(WindowsFFI.getKernel32().GetLastError());
}

@JRubyMethod(name = { "winapi_error=" }, module = true)
public static final IRubyObject winapi_error_set(ThreadContext context, IRubyObject recv, IRubyObject value) {
WindowsFFI.getKernel32().SetLastError((int)value.convertToInteger().getLongValue());

return value;
}
}
}
10 changes: 3 additions & 7 deletions core/src/main/java/org/jruby/util/WindowsFFI.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@

package org.jruby.util;

import jnr.ffi.*;
import jnr.ffi.LibraryOption;
import jnr.ffi.Runtime;
import jnr.ffi.annotations.Out;
import jnr.ffi.byref.IntByReference;
import jnr.ffi.types.intptr_t;
import jnr.ffi.types.uintptr_t;

import java.util.HashMap;
import java.util.Map;
import jnr.ffi.CallingConvention;
import jnr.ffi.LibraryLoader;

/**
* A binding of a few key win32 functions we need to behave properly.
Expand All @@ -53,6 +48,7 @@ public static interface Kernel32 {
jnr.ffi.Pointer OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);
int CloseHandle(jnr.ffi.Pointer handle);
int GetLastError();
int SetLastError(int ErrorCode);
int GetExitCodeProcess(jnr.ffi.Pointer hProcess, @Out IntByReference pointerToExitCodeDword);
int TerminateProcess(jnr.ffi.Pointer hProcess, int uExitCode);
}
Expand Down

0 comments on commit 16ea5d2

Please sign in to comment.