0

프로젝트에서 참조의 오른쪽 클릭 (컨텍스트) 메뉴에 요소를 추가하는 Visual Studio 확장을 개발 중입니다. 부모가 IDM_VS_CTXT_REFERENCEGroup을 정의하여이 작업을 수행 할 수 있습니다.Visual Studio 확장에서 어떤 참조를 클릭했는지 확인하십시오.

나는 참조를 클릭 한에 따라 메뉴 요소를 보여-숨기려면, 그래서 나는 OleMenuCommand로 내 메뉴 항목을 정의

if (commandService != null) 
{ 
    var menuCommandID = new CommandID(CommandSet, CommandId); 
    var menuItem = new OleMenuCommand(this.MenuItemCallback, menuCommandID); 

    menuItem.BeforeQueryStatus += (sender, args) => 
    { 
     var button = (OleMenuCommand)sender; 
     button.Visible = this.CommandVisible(); 
    }; 

    commandService.AddCommand(menuItem); 
} 

나는 문제가 CommandVisible 방법을 구현 있습니다. 참조 이름이 A으로 시작하는 경우 메뉴를 표시하려는 예제를 위해 가정 해 보겠습니다. 내가 어떻게 그럴 수 있니?

나는 interop 지옥에 갇혀있어 맹목적으로 임의의 ID, guids 및 존재하지 않는/이해할 수없는 문서에 걸린 것처럼 느낍니다.

내 참조가 IVsProject이고 참조 용 ID가있는 프로젝트를 파고 들었지만 GetMkDocument을 호출하면 아무 것도 반환하지 않습니다. 프로젝트의 파일에는 사용할 수 있지만 참조에는 사용할 수 없습니다.

어떻게하면됩니까? 이 작업을 수행하는 방법에 대한 설명서는 어디에서 찾을 수 있습니까?

+0

GetMkDocument 실제 문서에 대해서만 유효 전체 코드는 참조는 단지 시각적 인 원조하고 실제로 파일이 없습니다. 내가 너를 도울 수 있는지 알아보기 위해 몇 가지 테스트를하고있다. –

+0

일이 생겼지 만 내가 가지고있는 한 그것을 가져 갔다. 열쇠는 itemid를 사용하는 IVsHierarchy 메서드를 사용하게 될 것이다. 나는 네가 제대로 된 것 같아. –

답변

3

마지막으로 가져 왔습니다. 선택한 항목의 IVsHierarchy 및 itemid를 가져 오면이 행에서 원하는 이름을 out 매개 변수로 가져옵니다.

hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_Name, out name); 

object name; 
uint itemid = VSConstants.VSITEMID_NIL; 
IVsMultiItemSelect multiItemSelect = null; 
IntPtr hierarchyPtr = IntPtr.Zero; 
IntPtr selectionContainerPtr = IntPtr.Zero; 
try 
{ 
    var monitorSelection = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection; 
    monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainerPtr); 
    hierarchy = Marshal.GetObjectForIUnknown(hierarchyPtr) as IVsHierarchy;  
    hierarchy.GetProperty(itemid, (int)__VSHPROPID.VSHPROPID_Name, out name); 
}finally 
{ 
    if (selectionContainerPtr != IntPtr.Zero) 
     Marshal.Release(selectionContainerPtr); 

     if (hierarchyPtr != IntPtr.Zero) 
      Marshal.Release(hierarchyPtr); 
} 
+0

한 가지 중요한 점은 hierachyPtr 변수에서 Marshal.Release를 호출해야한다는 것입니다. 그렇지 않으면 COM 참조가 누출되어 메모리 누수가 발생합니다. –

+0

네가 맞다면, 그 코드는 다른 코드에있는 스 니펫입니다. 그래서 그것을 잊어 버렸습니다. 업데이트 중. –

+0

finally 블록을 사용하는 것을 잊지 마세요. 중간에 뭔가가있는 경우를 대비하여. :-( –

관련 문제