提问人:Jon Cage 提问时间:6/25/2010 最后编辑:CommunityJon Cage 更新时间:6/25/2010 访问量:708
如何以编程方式启动 Visual Studio 并将其发送到特定文件/行?
How can I programmatically launch visual studio and send it to a specific file / line?
问:
我有一个很好的整洁方法来捕获未经处理的异常,我将其显示给我的用户,并(可选)通过电子邮件发送给我自己。它们通常看起来像这样:
Uncaught exception encountered in MyApp (Version 1.1.0)!
Exception:
Object reference not set to an instance of an object.
Exception type:
System.NullReferenceException
Source:
MyApp
Stack trace:
at SomeLibrary.DoMoreStuff() in c:\projects\myapp\somelibrary.h:line 509
at SomeAlgothim.DoStuff() in c:\projects\myapp\somealgorithm.h:line 519
at MyApp.MainForm.ItemCheckedEventHandler(Object sender, ItemCheckedEventArgs e) in c:\projects\myapp\mainform.cpp:line 106
at System.Windows.Forms.ListView.OnItemChecked(ItemCheckedEventArgs e)
at System.Windows.Forms.ListView.WmReflectNotify(Message& m)
at System.Windows.Forms.ListView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
是否可以启动 Visual Studio 并在有问题的行上打开它,如果是这样,如何打开?c:\projects\myapp\somelibrary.h
如果可能的话,我也想从我生成的 (html) 电子邮件中执行此操作?
答:
4赞
jon hanson
6/25/2010
#1
可以使用示例 VBScript 自动执行 Visual Studio:
filename = Wscript.Arguments(0)
lineNo = Wscript.Arguments(1)
' Creates an instance of the Visual Studio IDE.
Set dte = CreateObject("VisualStudio.DTE")
' Make it visible and keep it open after we finish this script.
dte.MainWindow.Visible = True
dte.UserControl = True
' Open file and move to specified line.
dte.ItemOperations.OpenFile(filename)
dte.ActiveDocument.Selection.GotoLine (lineNo)
将其保存为 say 并运行它,将文件名和行号作为命令行参数传递:debugger.vbs
debugger.vbs c:\dev\my_file.cpp 42
1赞
stijn
6/25/2010
#2
由于您的问题被标记为 C++,因此这里有一些该语言的代码来实现此目的;与乔恩的回答基本相同,但文字更多。
bool OpenFileInVisualStudio( const char* psFile, const unsigned nLine )
{
CLSID clsid;
if( FAILED( ::CLSIDFromProgID( L"VisualStudio.DTE", &clsid ) ) )
return false;
CComPtr<IUnknown> punk;
if( FAILED( ::GetActiveObject( clsid, NULL, &punk ) ) )
return false;
CComPtr<EnvDTE::_DTE> DTE = punk;
CComPtr<EnvDTE::ItemOperations> item_ops;
if( FAILED( DTE->get_ItemOperations( &item_ops ) ) )
return false;
CComBSTR bstrFileName( psFile );
CComBSTR bstrKind( EnvDTE::vsViewKindTextView );
CComPtr<EnvDTE::Window> window;
if( FAILED( item_ops->OpenFile( bstrFileName, bstrKind, &window ) ) )
return false;
CComPtr<EnvDTE::Document> doc;
if( FAILED( DTE->get_ActiveDocument( &doc ) ) )
return false;
CComPtr<IDispatch> selection_dispatch;
if( FAILED( doc->get_Selection( &selection_dispatch ) ) )
return false;
CComPtr<EnvDTE::TextSelection> selection;
if( FAILED( selection_dispatch->QueryInterface( &selection ) ) )
return false;
return !FAILED( selection->GotoLine( Line, TRUE ) ) );
}
评论