Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

java - How to make GetLastError reliably work with JNA?

I'm using various Win32 API functions in my Java application and I use GetLastError to get information on failed API calls.

Most of the time it works, but now I found a case where something seems to reset the last error.

This is what I do both in my Java application and my VB6 application:

  1. I open the handle to the process with PID 4 (System)
  2. I call GetModuleFileNameEx with that handle.
  3. I call GetProcessImageFileName with that handle.

Now both API functions fail as expected (they return zero) and in my VB6 application GetLastError returns 87 ("Invalid parameter") after both API calls.

But in my Java application, only after GetModuleFileNameEx the last error is set to 87. After GetProcessImageFileName it is always zero.

What can I do about this?

EDIT:

Here are the JNA declarations:

public interface PsApi extends StdCallLibrary {
    PsApi INSTANCE = (PsApi) Native.loadLibrary("psapi", PsApi.class, 
            W32APIOptions.UNICODE_OPTIONS);
    int GetModuleFileNameEx(WinNT.HANDLE hProcess, WinDef.HMODULE hModule, 
            char[] lpFilename, int nSize);
    int GetProcessImageFileName(WinNT.HANDLE hProcess, char[] lpImageFileName, 
            int nSize);
}

And the calling code for GetProcessImageFileName

char[] buffer = new char[2048];
int result = PsApi.INSTANCE.GetProcessImageFileName(hProcess, buffer, 
   buffer.length);
if (result == 0) {
    throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return new String(Arrays.copyOf(buffer, result));

And for GetModuleFileNameEx

char[] buffer = new char[2048];
int result = PsApi.INSTANCE.GetModuleFileNameEx(hProcess, hModule, buffer,
    buffer.length);
if (result == 0) {
     throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return new String(Arrays.copyOf(buffer, result));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The documentation describes two options:

If a function sets the system error property (errno or GetLastError()), the error code will be thrown as a LastErrorException if you declare the exception in your JNA mapping. Alternatively, you can use Native.getLastError() to retrieve it, providing that Native.setPreserveLastError(boolean) has been called with a true value. Throwing an exception is preferred since it has better performance.

The problem with calling GetLastError is that the JNA framework and indeed the Java runtime may call Windows function which reset the error. So you should not attempt to call GetLastError directly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...