C# 如何获取有关异常的更多信息
我在Windows7上的VisualStudio2008中创建了一个64位的解决方案 它起作用了 当我把它移到另一台机器上,也赢得了7,64位,它崩溃了,几乎没有任何信息 最初的问题是: 然后我实施了这个解决方案: 但是,现在我的问题是,当我在另一台机器上运行可执行文件时,程序会立即崩溃,并显示以下信息:C# 如何获取有关异常的更多信息,c#,.net,exception,exception-handling,C#,.net,Exception,Exception Handling,我在Windows7上的VisualStudio2008中创建了一个64位的解决方案 它起作用了 当我把它移到另一台机器上,也赢得了7,64位,它崩溃了,几乎没有任何信息 最初的问题是: 然后我实施了这个解决方案: 但是,现在我的问题是,当我在另一台机器上运行可执行文件时,程序会立即崩溃,并显示以下信息: Description: Stopped working Problem signature: Problem Event Name: APPCRASH Applicat
Description:
Stopped working
Problem signature:
Problem Event Name: APPCRASH
Application Name: EmailSalesVolumeSolution.exe
Application Version: 1.0.0.0
Application Timestamp: 508064dd
Fault Module Name: KERNELBASE.dll
Fault Module Version: 6.1.7601.17932
Fault Module Timestamp: 503285c2
Exception Code: e0434f4d
Exception Offset: 000000000000caed
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt
我将代码包装在一个try/catch
中,但仍然没有得到正确的错误消息:
static void Main()
{
try
{
EnvDTE80.DTE2 dte;
object obj = null;
System.Type t = null;
// Get the ProgID for DTE 8.0.
t = System.Type.GetTypeFromProgID("VisualStudio.DTE.9.0",
true);
// Create a new instance of the IDE.
obj = System.Activator.CreateInstance(t, true);
// Cast the instance to DTE2 and assign to variable dte.
dte = (EnvDTE80.DTE2)obj;
// Register the IOleMessageFilter to handle any threading
// errors.
MessageFilter.Register();
// Display the Visual Studio IDE.
dte.MainWindow.Activate();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
// For example, get a reference to the solution2 object
// and do what you like with it.
// All done, so shut down the IDE...
dte.Quit();
// and turn off the IOleMessageFilter.
MessageFilter.Revoke();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
如何确定异常发生的确切位置以及异常是什么?
我确实有一些非托管代码:
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using System.Runtime.InteropServices;
namespace EmailSalesVolumeSolution
{
public class MessageFilter : IOleMessageFilter
{
//
// Class containing the IOleMessageFilter
// thread error-handling functions.
// Start the filter.
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(newFilter, out oldFilter);
}
// Done with the filter, close it.
public static void Revoke()
{
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(null, out oldFilter);
}
//
// IOleMessageFilter functions.
// Handle incoming thread requests.
int IOleMessageFilter.HandleInComingCall(int dwCallType,
System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr
lpInterfaceInfo)
{
//Return the flag SERVERCALL_ISHANDLED.
return 0;
}
// Thread call was rejected, so try again.
int IOleMessageFilter.RetryRejectedCall(System.IntPtr
hTaskCallee, int dwTickCount, int dwRejectType)
{
if (dwRejectType == 2)
// flag = SERVERCALL_RETRYLATER.
{
// Retry the thread call immediately if return >=0 &
// <100.
return 99;
}
// Too busy; cancel call.
return -1;
}
int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee,
int dwTickCount, int dwPendingType)
{
//Return the flag PENDINGMSG_WAITDEFPROCESS.
return 2;
}
// Implement the IOleMessageFilter interface.
[DllImport("Ole32.dll")]
private static extern int
CoRegisterMessageFilter(IOleMessageFilter newFilter, out
IOleMessageFilter oldFilter);
}
[ComImport(), Guid("00000016-0000-0000-C000-000000000046"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(
int dwCallType,
IntPtr hTaskCaller,
int dwTickCount,
IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(
IntPtr hTaskCallee,
int dwTickCount,
int dwRejectType);
[PreserveSig]
int MessagePending(
IntPtr hTaskCallee,
int dwTickCount,
int dwPendingType);
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用EnvDTE;
使用EnvDTE80;
使用EnvDTE90;
使用System.Runtime.InteropServices;
命名空间EmailSalesVolumeSolution
{
公共类MessageFilter:IOleMessageFilter
{
//
//包含IOleMessageFilter的类
//线程错误处理函数。
//启动过滤器。
公共静态无效寄存器()
{
IOleMessageFilter newFilter=新消息过滤器();
IOleMessageFilter-oldFilter=null;
CoRegisterMessageFilter(新过滤器、旧过滤器);
}
//完成过滤器,关闭它。
公共静态void Revoke()
{
IOleMessageFilter-oldFilter=null;
CoRegisterMessageFilter(null,out oldFilter);
}
//
//IOleMessageFilter函数。
//处理传入的线程请求。
int IOleMessageFilter.HandleInComingCall(int dwCallType,
System.IntPtr hTaskCaller,int dwTickCount,System.IntPtr
LPEINFO)
{
//返回标志SERVERCALL\u ISHANDLED。
返回0;
}
//线程调用被拒绝,请重试。
int IOleMessageFilter.RetryRejectedCall(System.IntPtr
hTaskCallee、int-dwTickCount、int-dwreturnType)
{
if(dwRejectType==2)
//flag=SERVERCALL\u RETRYLATER。
{
//如果返回>=0&
//尝试从异常中打印更多内容:
catch(Exception e)
{
MessageBox.Show(e.Message + ":\n" + e.StackTrace);
}
您也可以使用此选项捕捉所有内部例外:
string except = "Uncaught Exception: ";
while(e != null)
{
except += e.Message + ";\n";
e = e.InnerException;
}
如果程序无一例外地崩溃,那么您需要尝试并附加一个调试器
您需要在计算机上安装Visual Studio,或者能够使用远程桌面进行远程调试
要附加到正在运行的进程,请执行以下操作:
在“调试”菜单上,选择“附加到进程”
在“附加到进程”对话框中,找到所需的程序
从“可用进程”列表中附加到
如果要调试的程序正在另一台计算机上运行
计算机,使用限定符列表框选择远程计算机。
有关详细信息,请参见如何:选择远程计算机
如果进程在其他用户帐户下运行,请选择
“显示来自所有用户的进程”复选框
如果通过远程桌面连接进行连接,请选择
“在所有会话中显示进程”复选框
在“附加到”框中,确保要调试的代码类型
已列出。默认的自动设置尝试确定
要调试的代码类型。如果未启用自动设置
适当的:
单击选择
在“选择代码类型”对话框中,单击“调试这些代码类型”,然后单击“调试”
选择要调试的类型
单击“确定”
单击“附加”
必须使用调试模式编译可执行文件
更新
我看到您使用了Type.GetTypeFromProgID
,它需要.NET 4.5,请确保在抛出错误的PC上安装了.NET 4.5
似乎t=System.Type.GetTypeFromProgID(“VisualStudio.DTE.9.0”,true);
正在抛出,这意味着“VisualStudio.DTE.9.0”未在其他PC中注册
您应该将Microsoft Visual Studio 2008 DTE添加为程序的依赖项。您也可以尝试订阅AppDomain.CurrentDomain.UnhandledException
事件:
AppDomain.CurrentDomain.UnhandledException +=
(sender, e) => MessageBox.Show(e.ExceptionObject.ToString());
在Main
方法的顶部执行此操作。唯一有效的解决方案是安装Visual studio。安装Visual studio后,我得到了一个完全例外。您的DLL可能未在客户端计算机上注册。请检查您调用COM对象的DLL,并在管理员中执行Regsvr32 YouComDllName.DLL
比率命令提示符(右键单击命令提示符图标并以管理员身份运行。根据您使用的是32位还是64位应用程序,您可能需要在C:\Windows\System32
或C:\Windows\SysWOW64
中使用Regsvr32的副本(向后看,64位版本在system32中,32位版本在SysWOW64中)
如果只是在您的计算机上重新设置DLL不起作用,请在您的计算机上的程序上运行。当您获得程序所接触的所有DLL时,请检查客户端计算机是否丢失任何DLL。您正在调用的DLL可能在那里,但它所依赖的DLL可能不在那里。尝试附加一个调试器以查看哪一行崩溃在我的机器上,程序在调试和发布模式下运行良好,但当我将其移动到另一台机器上时,可执行文件会吐出上面的垃圾,例如ToString()提供完整的异常消息并StackTraceUpdate,如果您成功附加调试器,请通知我。非常感谢我现在正在下载VS 2008 Express谢谢,我刚刚尝试了此操作,但仍然得到相同的结果如果您在另一台计算机上安装了visual studio,则可以添加调试器.Break
命令在debugger.Btw中遍历代码