[C#] 如何调用Windows系统发送错误API
2012-10-17 15:52
1241 查看
在Windows XP 系统里面 对于程序的错误调试是用到 Dr.Watson.
可以通过下面的步骤进行配置:
点开始,运行Run
输入drwtsn32,然后确定。
但是drwatson.exe 只是存在早期版本的windows系统里面。
具体可参考微软的官方解释(http://support.microsoft.com/kb/308538)
——————————————————————————————
对于较新版本的操作系统(Vista、Windows 7、Windows server 2008),微软用到了一个新的技术-Windows Error Reporting(WER)。
WER 我们可以从控制面板的活动中心找到。可以对WER进行关闭和配置。
下面是查看WER的记录。
1.
View Code
可以通过下面的步骤进行配置:
点开始,运行Run
输入drwtsn32,然后确定。
但是drwatson.exe 只是存在早期版本的windows系统里面。
具体可参考微软的官方解释(http://support.microsoft.com/kb/308538)
——————————————————————————————
对于较新版本的操作系统(Vista、Windows 7、Windows server 2008),微软用到了一个新的技术-Windows Error Reporting(WER)。
WER 我们可以从控制面板的活动中心找到。可以对WER进行关闭和配置。
下面是查看WER的记录。
1.
View Code
using System; using System.Runtime.InteropServices; namespace WatsonTest { class Program { static bool Failed(int result) { return (result < 0); } private static void Main(string[] args) { var t = AppDomain.CurrentDomain; t.UnhandledException += (s, e) => { var bucket = GetWatsonBuckets(); var zero = IntPtr.Zero; Console.Write(bucket); Console.Read(); }; throw new NullReferenceException(); } private static WatsonBuckets GetWatsonBuckets() { var pParams = new WatsonBuckets(); IClrRuntimeHost host = null; host = Activator.CreateInstance(Type.GetTypeFromCLSID(ClrGuids.ClsIdClrRuntimeHost)) as IClrRuntimeHost; if (host != null) { var clrControl = host.GetCLRControl(); if (clrControl == null) { return pParams; } var clrErrorReportingManager = clrControl.GetCLRManager(ref ClrGuids.IClrErrorReportingManager) as IClrErrorReportingManager; if (clrErrorReportingManager == null) { return pParams; } clrErrorReportingManager.GetBucketParametersForCurrentException(out pParams); } return pParams; } } // BucketParameters Structure to get watson buckets back from CLR //http://msdn.microsoft.com/en-us/library/ms404466(v=VS.100).aspx [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] internal struct WatsonBuckets { internal int fInited; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string pszEventTypeName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param0; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param1; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param2; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param3; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param4; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param5; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param6; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param7; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param8; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0xff)] internal string param9; } internal static class ClrGuids { internal static readonly Guid ClsIdClrRuntimeHost = new Guid("90F1A06E-7712-4762-86B5-7A5EBA6BDB02"); internal static Guid IClrErrorReportingManager = new Guid("980D2F1A-BF79-4c08-812A-BB9778928F78"); internal static readonly Guid IClrRuntimeHost = new Guid("90F1A06C-7712-4762-86B5-7A5EBA6BDB02"); } [Guid("90F1A06C-7712-4762-86B5-7A5EBA6BDB02"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IClrRuntimeHost { void Start(); void Stop(); void SetHostControl(IntPtr pHostControl); IClrControl GetCLRControl(); void UnloadAppDomain(int dwAppDomainId, bool fWaitUntilDone); void ExecuteInAppDomain(int dwAppDomainId, IntPtr pCallback, IntPtr cookie); int GetCurrentAppDomainId(); int ExecuteApplication(string pwzAppFullName, int dwManifestPaths, string[] ppwzManifestPaths, int dwActivationData, string[] ppwzActivationData); int ExecuteInDefaultAppDomain(string pwzAssemblyPath, string pwzTypeName, string pwzMethodName, string pwzArgument); } [Guid("9065597E-D1A1-4fb2-B6BA-7E1FCE230F61"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IClrControl { [return: MarshalAs(UnmanagedType.IUnknown)] object GetCLRManager([In] ref Guid riid); void SetAppDomainManagerType(string pwzAppDomainManagerAssembly, string pwzAppDomainManagerType); } // IClrErrorReportingManager to get watson bukets back from CLR //http://msdn.microsoft.com/en-us/library/ms164367(v=VS.100).aspx [Guid("980D2F1A-BF79-4c08-812A-BB9778928F78"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IClrErrorReportingManager { [PreserveSig] int GetBucketParametersForCurrentException(out WatsonBuckets pParams); } 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); } }
相关文章推荐
- 在C#中如何调用windows的API进行关机操作!!! [转]
- 如何从C#桌面应用程序中调用Windows 8的WinRT API (Metro API)
- 如何从C# 桌面应用程序调用Windows 8的WinRT API- WinRT 图表
- linux调用系统api出错,如何查看错误原因?
- VC6.0 C++ 如何调用微软windows系统SDK 语音API
- VC6.0 C++ 如何调用微软windows系统SDK 语音API
- 在C#中如何调用windows的API进行关机操作!!!
- C#如何调用Windows系统自身的文件复制功能,显示文件进度,显示重名提示
- 调用Android系统API发送短信,打电话
- C#调用系统API指定快捷键
- C#+Windows API操纵系统菜单讲解
- C#WINFORM里如何调用WEB Service API
- InstallShield中如何调用系统API
- C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1)--windows消息大全
- c#中如何跨线程调用windows窗体控件
- C# 如何通过拼接XML调用存储过程来优化系统性能
- How Do Windows NT System Calls REALLY Work?--Windows NT的系统调用究竟是如何工作的?
- C# 调用系统API 内核 简单样例
- C#+Windows API操纵系统菜单
- windows下调用系统API实现进程创建和文件读写