您绝对可以使用wer.dll
作为 Win32 API 的一部分附带的 Windows 错误报告 API。
从 Java 调用基于 DLL 的函数的最佳方式是使用积极开发的Java Native Access 项目。
为了进行所需的 Win32 API 调用,我们至少需要向 JNA 介绍这些函数:
HRESULT WINAPI WerReportCreate(
__in PCWSTR pwzEventType,
__in WER_REPORT_TYPE repType,
__in_opt PWER_REPORT_INFORMATION pReportInformation,
__out HREPORT *phReportHandle
);
HRESULT WINAPI WerReportSubmit(
__in HREPORT hReportHandle,
__in WER_CONSENT consent,
__in DWORD dwFlags,
__out_opt PWER_SUBMIT_RESULT pSubmitResult
);
还有这个结构:
typedef struct _WER_REPORT_INFORMATION {
DWORD dwSize;
HANDLE hProcess;
WCHAR wzConsentKey[64];
WCHAR wzFriendlyEventName[128];
WCHAR wzApplicationName[128];
WCHAR wzApplicationPath[MAX_PATH];
WCHAR wzDescription[512];
HWND hwndParent;
} WER_REPORT_INFORMATION, *PWER_REPORT_INFORMATION;
为此,我们将创建 WER.java:
package com.sun.jna.platform.win32;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
public interface Wer extends StdCallLibrary {
Wer INSTANCE = (Wer) Native.loadLibrary("wer", Wer.class,
W32APIOptions.DEFAULT_OPTIONS);
public static class HREPORT extends HANDLE {
public HREPORT() { }
public HREPORT(Pointer p) { super(p); }
public HREPORT(int value) { super(new Pointer(value)); }
}
public static class HREPORTByReference extends ByReference {
public HREPORTByReference() {
this(null);
}
public HREPORTByReference(HREPORT h) {
super(Pointer.SIZE);
setValue(h);
}
public void setValue(HREPORT h) {
getPointer().setPointer(0, h != null ? h.getPointer() : null);
}
public HREPORT getValue() {
Pointer p = getPointer().getPointer(0);
if (p == null)
return null;
if (WinBase.INVALID_HANDLE_VALUE.getPointer().equals(p))
return (HKEY) WinBase.INVALID_HANDLE_VALUE;
HREPORT h = new HREPORT();
h.setPointer(p);
return h;
}
}
public class WER_REPORT_INFORMATION extends Structure {
public DWORD dwSize;
public HANDLE hProcess;
public char[] wzConsentKey = new char[64];
public char[] wzFriendlyEventName = new char[128];
public char[] wzApplicationName = new char[MAX_PATH];
public char[] wzDescription = new char[512];
public HWND hwndParent;
dwSize = new DWORD(size());
}
public abstract class WER_REPORT_TYPE {
public static final int WerReportNonCritical = 0;
public static final int WerReportCritical = 1;
public static final int WerReportApplicationCrash = 2;
public static final int WerReportApplicationHang = 3;
public static final int WerReportKernel = 4;
public static final int WerReportInvalid = 5;
}
HRESULT WerReportCreate(String pwzEventType, int repType, WER_REPORT_INFORMATION pReportInformation, HREPORTByReference phReportHandle);
HRESULT WerReportSubmit(HREPORT hReportHandle, int consent, DWORD dwFlags, WER_SUBMIT_RESULT.ByReference pSubmitResult);
}
我只是在几分钟内从 MSDN 文档中将它拼凑起来——如果它不完整或不正确,JNA 网站上有大量示例和非常好的文档。
为了运行 JNA,您需要jna.jar
和platform.jar
,您也可以从 JNA 网站获取。