This is a mirror of official site: http://jasper-net.blogspot.com/

Using Windows Error Reporting (WER) API in managed code to generate memory dump

| Monday, June 14, 2010
The WER is a pretty cool technology from Microsoft for collecting memory dumps on process crash/ hang. This can be extended to generate on demand when the application needs to. The usual reason for getting a memory dump could be based on certain conditions, for example, the customer feels the application is slow and would want to send the information to WinQual (WER server). If the application happens to be installed on hundreds / thousands of boxes then its not going to be possible to get from individual customers, the best bet is WER. To do this here is an API. But this is unmanaged API and I didn’t see one for managed code. FYI this would work only on Vista + systems, it will not work on XP.

Here is the basic PInvoke for creating dump and submitting a report. I am also using it along with the watsonbuckets that I had blogged about.

internal enum WER_CONSENT
{

WerConsentAlwaysPrompt = 4,
WerConsentApproved = 2,
WerConsentDenied = 3,
WerConsentMax = 5,
WerConsentNotAsked = 1
}

internal enum WER_DUMP_TYPE
{
WerDumpTypeHeapDump = 3,
WerDumpTypeMax = 4,
WerDumpTypeMicroDump = 1,
WerDumpTypeMiniDump = 2
}

internal enum WER_REPORT_TYPE
{
WerReportNonCritical,
WerReportCritical,
WerReportApplicationCrash,
WerReportApplicationHange,
WerReportKernel,
WerReportInvalid
}

internal static class Unmanaged
{

[DllImport("wer.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int WerReportAddDump(
IntPtr hReportHandle,
IntPtr hProcess, 
IntPtr hThread, 
WER_DUMP_TYPE dumpType, 
IntPtr pExceptionParam, 
IntPtr pDumpCustomOptions, 
int dwFlags);

[DllImport("wer.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int WerReportCreate(
string pwzEventType,
WER_REPORT_TYPE repType, 
IntPtr pReportInformation, 
ref IntPtr phReportHandle);

[DllImport("wer.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int WerReportSetParameter(IntPtr hReportHandle, int dwparamID, string pwzName, string pwzValue);

[DllImport("wer.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int WerReportSubmit(IntPtr hReportHandle, WER_CONSENT consent, int dwFlags, ref IntPtr pSubmitResult);
}

Read more: Naveen's Blog

Posted via email from .NET Info

0 comments: