code_junkie

Open a file in Visual Studio at a specific line number

Nov 12th, 2011
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. emacs +140 filename.c
  2.  
  3. devenv /Command "Edit.Goto 140" filename.c
  4.  
  5. devenv /Command "Edit.Goto 140" /Edit filename.c
  6. devenv /Command /Edit filename.c "Edit.Goto 140"
  7.  
  8. #include "stdafx.h"
  9.  
  10. //-----------------------------------------------------------------------
  11. // This code is blatently stolen from http://benbuck.com/archives/13
  12. //
  13. // This is from the blog of somebody called "BenBuck" for which there
  14. // seems to be no information.
  15. //-----------------------------------------------------------------------
  16.  
  17. // import EnvDTE
  18. #pragma warning(disable : 4278)
  19. #pragma warning(disable : 4146)
  20. #import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids
  21. #pragma warning(default : 4146)
  22. #pragma warning(default : 4278)
  23.  
  24. bool visual_studio_open_file(char const *filename, unsigned int line)
  25. {
  26. HRESULT result;
  27. CLSID clsid;
  28. result = ::CLSIDFromProgID(L"VisualStudio.DTE", &clsid);
  29. if (FAILED(result))
  30. return false;
  31.  
  32. CComPtr<IUnknown> punk;
  33. result = ::GetActiveObject(clsid, NULL, &punk);
  34. if (FAILED(result))
  35. return false;
  36.  
  37. CComPtr<EnvDTE::_DTE> DTE;
  38. DTE = punk;
  39.  
  40. CComPtr<EnvDTE::ItemOperations> item_ops;
  41. result = DTE->get_ItemOperations(&item_ops);
  42. if (FAILED(result))
  43. return false;
  44.  
  45. CComBSTR bstrFileName(filename);
  46. CComBSTR bstrKind(EnvDTE::vsViewKindTextView);
  47. CComPtr<EnvDTE::Window> window;
  48. result = item_ops->OpenFile(bstrFileName, bstrKind, &window);
  49. if (FAILED(result))
  50. return false;
  51.  
  52. CComPtr<EnvDTE::Document> doc;
  53. result = DTE->get_ActiveDocument(&doc);
  54. if (FAILED(result))
  55. return false;
  56.  
  57. CComPtr<IDispatch> selection_dispatch;
  58. result = doc->get_Selection(&selection_dispatch);
  59. if (FAILED(result))
  60. return false;
  61.  
  62. CComPtr<EnvDTE::TextSelection> selection;
  63. result = selection_dispatch->QueryInterface(&selection);
  64. if (FAILED(result))
  65. return false;
  66.  
  67. result = selection->GotoLine(line, TRUE);
  68. if (FAILED(result))
  69. return false;
  70.  
  71. return true;
  72. }
  73.  
  74. devenv /command "Macros.MyMacros.Module1.OpenFavoriteFiles"
  75.  
  76. devenv /edit FILE_PATH /command "edit.goto FILE_LINE"
Add Comment
Please, Sign In to add comment