Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FFI::LastError.winapi_error #6065

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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