您肯定可以使用Windows錯誤報告API作爲Win32 API的一部分在wer.dll
中發貨。
從Java調用基於DLL的函數的最好方法是使用積極開發的Java Native Access project。
爲了使所需的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 dcoumentation s - 如果它不完整或不正確,JNA網站上有tons of examples和pretty good documentation。
爲了運行JNA,您需要jna.jar
和platform.jar
,您還可以從JNA網站獲取該文件。
[NTEventLogAppender](http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/nt/NTEventLogAppender.html)或類似的東西?編輯:[可能相關的問題](http://stackoverflow.com/questions/164879/how-to-write-from-java-to-the-windows-event-log) – 2011-12-14 15:40:25
@VineetReynolds,它會觸發「發送錯誤報告給微軟「行動? – 2011-12-14 15:42:40
[沒有。這是配置爲僅在崩潰時發生。](http://support.microsoft.com/kb/310414) – 2011-12-14 15:44:06