Visual Studio/TFS extensibility: why does DocumentService give me an InvalidCastException? - visual-studio-2010

I'm writing an addin for Microsoft Outlook 2007. My goal is to open a work item in a Visual Studio 2010 instance, which I know exists and has the packages loaded for TFS 2012. The following code works fine, until the last line gives me an InvalidCastException:
Imports Microsoft.VisualStudio.TeamFoundation.WorkItemTracking
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.VisualStudio.OLE.Interop
Imports Microsoft.VisualStudio.Shell
Private Sub Test()
Dim vsObj As IServiceProvider = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")
Dim sp As New ServiceProvider(vsObj)
Dim docService As DocumentService = sp.GetService(GetType(DocumentService))
... do stuff with docService ...
End Sub
Here is the full exception text:
Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
I asked the service provider for a DocumentService, and it gave me something, but that something tells me it's not actually a DocumentService. What's the deal? How do I get the DocumentService to know it's a DocumentService, or how do I get something that actually is one?
edit: I've also tried removing "As DocumentService" to get past the invalid cast, but nothing else about the object is discoverable when I inspect it as a Watch. I also tried the following (recommended here), hoping it would shed some light on the problem:
Dim vsObj As IServiceProvider = System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0")
Dim sp As New ServiceProvider(vsObj)
Dim docService = sp.GetService(GetType(DocumentService))
'Note that I still didn't specify a type for docService above.
MsgBox(Microsoft.VisualBasic.Information.TypeName(docService))
To my horror, the message box said "DocumentService", merely reaffirming my indignation. I really have no idea what to do with that.
This related article is the closest I could find to somebody having the same problem, but it was not answered: How to open WorkItem (VS-Team Explorer) from outside visual studio?.
Thank you!

Related

VB6 Compile Outlook add-in - Compile Error: Automation Type Not supported in Visual basic

I have got an infuriating problem trying to compile a VB6 Outlook Add-in component following an upgrade of Office on the same PC to Office 2013 Pro Plus.
The error
Compile Error: Automation type not supported in Visual Basic
is being thrown on the line:
Implements IDExtensibility2
I will paste the full code from that file below.
On another PC, I managed to get everything set up, and upgrade Office to 2012 and it compiled no problem, but on that same PC now - I get the same error. I have messed around a lot with the Office install in the interim so I couldn't accurately tell you what I did in between it working and not working, but the bottom line is it seems it shouldn't be beyond possibility to do what I am trying to do (as I have done it!), I just need some guidance on what I may need to do to get back to a working setup. Any ideas??
Implements IDTExtensibility2
Private gBaseClass As New clsOlkAddIn
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
'To Be Declared for IDTExtensibility2
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
'To Be Declared for IDTExtensibility2
End Sub
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
gBaseClass.InitHandler Application, AddInInst.ProgId
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode _
As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
Dim objCB As Office.CommandBar
On Error Resume Next
'If RemoveMode = ext_dm_UserClosed Then
Set objCB = golApp.ActiveExplorer.CommandBars("Addin")
objCB.FindControl(Type:=msoControlPopup, Tag:="Menu").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="AddEmailsButton").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="SyncContactsButton").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="SyncTasksButton").Delete
objCB.FindControl(Type:=msoControlButton, Tag:="SyncCalendarButton").Delete
objCB.Delete
'End If
gBaseClass.UnInitHandler
Set gBaseClass = Nothing
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
'To Be Declared for IDTExtensibility2
End Sub
Another cause of this error, as I found today is having an optional parameter on a function and giving it a default, e.g.
Public Function Foo(MyString as String, Optional MyVariant as variant = null)
Changing this to:
Public Function Foo(MyString as String, Optional MyVariant as variant)
Solved it, I didn't get there as simply as it looks above though (hence, why I'm hanging around here), I did change the optional parameter to a string first, which worked, in fact, even with a variant parameter, having a default of anything other than Null worked. It was the default of Null that was causing the error.
As you've confirmed in your answer (in comments), ensuring you've upgraded to the latest service pack is always the best option unless you know you're relying upon features that were dropped or changed in later service packs.
Installing the "Cumulative update rollup for the Visual Basic 6.0 Service Pack 6 Runtime Extended Files" (https://support.microsoft.com/en-us/help/957924/description-of-the-cumulative-update-rollup-for-the-visual-basic-6-0-s) resolved this error in my instance.

Detecting Visual Studio license type / distribution channel

There is plenty of information throughout the net on how to detect Windows license type and distribution channel using PID (that can be found in HKLM\SYSTEM\Setup\Pid registry key) and ProductID (that can be found in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion).
However, there is quite little information on how to detect what kind of licensing is used to install other Microsoft products.
Namely, what i need to determine is which ones were installed using MSDN subscription license, and which were installed using other license types.
I've managed to find some info on Office flavors by analyzing product GUID (found in Uninstall registry branch):
Office 2000
Office XP
Office 2003
Office 2007
Office 2010
However, all those articles only distinguish between these release types:
0 Volume license
1 Retail/OEM
2 Trial
5 Download
Furthermore, i could not locate any feasible information about other products.
Namely, Visual Studio... (which is the one i am interested in the most).
To clarify: I do not need to obtain a license key (which i already found several ways of detecting while seeking for the information i need), my interest is solely in detecting what kind of license type was used to install the application.
Ok so the only way I can see of doing this is to use a tool like magicaljellybeans key finder and then have the exported keys from you msdn licenses and do a compare.
From just the software itself I am pretty sure that they are not identified as MSDN versions per se but it is the license that activates them that determines that so I am pretty sure that what you are asking is not possible as you describe it without doing the compare which means you need the key.
This is especially true as some products in msdn have and do use "retail versions" of software.
http://blogs.msdn.com/b/heaths/archive/2010/05/04/detection-keys-for-net-framework-4-0-and-visual-studio-2010.aspx
Visual Studio 2010
The detection keys for Visual Studio are used both to detect if the product is installed and what service pack level is installed. As with previous versions, these keys and values are under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VS\Servicing.
Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VS\Servicing\10.0\$(var.ProductEdition)\$(var.LCID)
Name Install
Type REG_DWORD (32-bit integer)
Data 0x00000001 (1)
The values for $(var.ProductEdition) include the following table. The for other products, this value corresponds to the ProductEdition property in the Property table of the Windows Installer package for that product. The locale IDs for $(Var.LCID) are listed here.
Visual Studio 2010 Ultimate VSTSCore
Visual Studio 2010 Premium VSTDCore
Visual Studio 2010 Professional PROCore
Visual Studio 2010 Shell (Integrated) IntShell
For Dev10 we have also added detection values to the edition keys so that you do not have to detect every single language for ever edition.
Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VS\Servicing\10.0\$(var.ProductEdition)
Name Install
Type REG_DWORD (32-bit integer)
Data 0x00000001 (1)
These registry keys and values are also used to detect the service pack level. Instead of checking for the Name registry value, check the SP registry value. Ignore SPIndex; Microsoft uses this internally. In addition to the registry keys above, we also set a version-dependent registry value listed below.
Key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VS\Servicing\10.0
Name SP
Type REG_DWORD (32-bit integer)
Data 0x00000001 (1)
Keep in mind, however, that as with all shared resources the more shared a resource is the less accurate it may be. This is because there is no version policy for registry values and other resources that do not have versions like text files. If two languages were installed at different servicing pack levels – which is unsupported but possible – the value would be set by the last product to be installed or reinstalled (repaired).
For more information, see the Visual Studio 2010 detection system requirements. This also includes information to use the CompLocator in your Windows Installer package as an alternative to registry detection.
http://www.mztools.com/articles/2008/MZ2008003.aspx
To detect which Visual Studio version a (multi-IDE) add-in is running under, you can use the EnvDTE.DTE.RegistryRoot property, which can return the following values:
For Visual Studio 2005: Software\Microsoft\VisualStudio\8.0
For Visual Studio 2008: Software\Microsoft\VisualStudio\9.0
To detect the edition and subedition of the Visual Studio IDE you can use the IVsShell interface, calling its GetProperty method with the Microsoft.VisualStudio.Shell.Interop.__VSSPROPID2.VSSPROPID_SKUEdition and Microsoft.VisualStudio.Shell.Interop.__VSSPROPID2.VSSPROPID_SubSKUEdition values.
Note: To get a service from an add-in, see HOWTO: Get a Visual Studio service from an add-in; and to know how to reference an assembly from the GAC (such as Microsoft.VisualStudio.Shell.Interop.dll), see HOWTO: Reference a Visual Studio assembly in the GAC from an add-in.
The returned results are not well documented but some values are provided in the following sample add-in:
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports System.Runtime.InteropServices
Imports Microsoft.VisualStudio.Shell.Interop
Imports System.Windows.Forms
<ComVisible(True), ComImport(), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Friend Interface IServiceProvider
<PreserveSig()> _
Function QueryService(<InAttribute()> ByRef guidService As Guid, _
<InAttribute()> ByRef riid As Guid, <OutAttribute()> ByRef ppvObject As IntPtr) As Integer
End Interface
Public Class Connect
Implements IDTExtensibility2
Private Enum SKUEdition
SKUEdition_None = 0
SKUEdition_Express = 500
SKUEdition_Standard = &H3E8
SKUEdition_Professional = &H7D0
SKUEdition_AcademicProfessional = &H834
SKUEdition_AcademicStudent = &H834
SKUEdition_AcademicStudentMSDNAA = &H898
SKUEdition_AcademicEnterprise = &H8FC
SKUEdition_Book = &H960
SKUEdition_DownloadTrial = &H9C4
SKUEdition_Enterprise = &HBB8
End Enum
Private Enum SubSKUEdition
SubSKUEdition_None = &H0
SubSKUEdition_VC = &H1
SubSKUEdition_VB = &H2
SubSKUEdition_CSharp = &H4
SubSKUEdition_Architect = &H8
SubSKUEdition_IDE = &H10
SubSKUEdition_JSharp = &H20
SubSKUEdition_Web = &H40
SubSKUEdition_TeamEditionDevelopers = &H80
End Enum
Public Function GetService(ByVal serviceProvider As Object, ByVal type As System.Type) As Object
Return GetService(serviceProvider, type.GUID)
End Function
Public Function GetService(ByVal serviceProvider As Object, ByVal guid As System.Guid) As Object
Dim objService As Object = Nothing
Dim objIServiceProvider As IServiceProvider
Dim objIntPtr As IntPtr
Dim hr As Integer
Dim objSIDGuid As Guid
Dim objIIDGuid As Guid
objSIDGuid = guid
objIIDGuid = objSIDGuid
objIServiceProvider = CType(serviceProvider, IServiceProvider)
hr = objIServiceProvider.QueryService(objSIDGuid, objIIDGuid, objIntPtr)
If hr <> 0 Then
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr)
ElseIf Not objIntPtr.Equals(IntPtr.Zero) Then
objService = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(objIntPtr)
System.Runtime.InteropServices.Marshal.Release(objIntPtr)
End If
Return objService
End Function
Public Sub OnConnection(ByVal Application As Object, ByVal ConnectMode As Extensibility.ext_ConnectMode, _
ByVal AddInInst As Object, ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnConnection
Dim objDTE As DTE
Dim objService As Object
Dim objIVsShell As IVsShell
Dim objValue As Object = Nothing
Dim eSKUEdition As SKUEdition
Dim eSubSKUEdition As SubSKUEdition
Select Case ConnectMode
Case ext_ConnectMode.ext_cm_AfterStartup, ext_ConnectMode.ext_cm_Startup
objDTE = DirectCast(Application, DTE)
objService = GetService(objDTE, GetType(IVsShell))
objIVsShell = CType(objService, IVsShell)
If objIVsShell.GetProperty(__VSSPROPID2.VSSPROPID_SKUEdition, objValue) = 0 Then
eSKUEdition = CType(objValue, SKUEdition)
MessageBox.Show(eSKUEdition.ToString)
End If
If objIVsShell.GetProperty(__VSSPROPID2.VSSPROPID_SubSKUEdition, objValue) = 0 Then
eSubSKUEdition = CType(objValue, SubSKUEdition)
MessageBox.Show(eSubSKUEdition.ToString)
End If
End Select
End Sub
Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub
Public Sub OnBeginShutdown(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub
Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, _
ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub
Public Sub OnStartupComplete(ByRef custom As System.Array) _
Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub
End Class
To detect whether a specific Visual Studio package is installed or not, you can use the IsPackageInstalled method of the IVsShell interface, which receives the Guid of the package.
Detecting installed Visual Studio Service Packs
Visual Studio uses the following Windows registry entries to track the installed service packs for the several versions (2005, 2008), editions (Standard, Professional, etc.) and languages (English, Spanish, etc.):
HKEY_LOCAL_MACHINE\Software\Microsoft\DevDiv\VS\Servicing\<version>\<edition>\<localeId>
where <version> and <edition> where explained in a previous section of this article and <localeId> is 1033 for English, 3082 for Spanish, etc.
For example, when Visual Studio 2005 Team Edition for Developers has SP1 installed, the value of the "SP" registry entry of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DevDiv\VS\Servicing\8.0\VSTD\1033 is set to 1.
To know the service at general level (without taking into account the edition and language) you can use the "SP" registry entry under the registry key:
HKEY_LOCAL_MACHINE\Software\Microsoft\DevDiv\VS\Servicing\<version>

Referencing functions within VB6 User Controls

I'm having an issue referencing public procedures of User Controls that I've created within a VB6 project.
A simple example (exe), I have a form with a button and a user control:
Option Explicit
Private Sub Command1_Click()
UserControl1.updateMessage ("TIME NOW: " & DateTime.Time)
End Sub
The User Control code is as follows:
Option Explicit
Public Sub updateMessage(ByVal newMessage As String)
Label1.Caption = newMessage
End Sub
This exe compiles and works fine, and when I'm typing updateMessage in the Form, it appears in the intellisense list with the appropriate requirements. The issue I have is when I'm wanting to "go to the definition" of updateMessage, instead of going to the appropriate section of the code within the User Control, the message always returns with:
"Cannot jump to 'updateMessage' because it is in the library 'Unknown1' which is not currently referenced."
where the numbered suffix of "Unkown1" changes from time to time.
It seems that if there were no reference to this procedure, then it would not appear in the intellisense and the project shouldn't compile. When running this with MZTools (though the error appears regardless of this plug-in being installed), I can go into the updateMessage procedure, and use it to find all procedures calling this function, so the link between the two should exist (although I'm not sure if MZTools just finds using a text-matching pattern).
If anyone out there could shed some light on this matter, it would be very much appreciated, and save this poor VB6 developer a lot of hassle!
I have SP6 installed (build 9782) of VB6 and am running XP SP3 on an HP dx2400.
Yes, this is extremely annoying and I'm convinced it's a bug in VB6. I say this because, if you locate the updateMessage method in the object browser and double-click on it, you are taken to the definition. So, the compiler actually does know where the definition is, it just refuses to take you there with Shift+F2.

XP Look-and-feel for VB6 Outlook Property Page?

I am writing a PropertyPage for Outlook using VB6. This is implemented as a VB6 OCX.
When running in a newer version of Outlook (like 2007) on XP (or newer), my dialog looks weird because it doesn't have XP look and feel. Is there a way to do this?
Preferably without adding a manifest file for Outlook.exe.
I think you're right to avoid using a manifest. Unfortunately the standard well-known hacks to support XP themes from VB6 rely on manifests. This MSDN article on developer solutions for Outlook 2007 warns that providing your own manifest for Outlook 2007 might cause it to hang.
I don't think you can do it in VB6 ... those controls are going to look like what they look like. You can, however, create your property pages with Visual Studio .NET and Visual Basic .NET and get the XP, 2007 and Vista look and feel. It's a bit of a change from what you're doing, but you're really behind the times developing with VB6 anyway. More details on how to do that are here and the office developer center.
Not that I know of using VB6
If you can use .NET instead - one way is WPF. I saw earlier an example on code-project.
Here's the link
Edit: And another tool to assist here
This is what I do in all of my VB6 Apps, only ever tested in a standalone EXE so not sure if it will work as an OCX.
Private Type tagInitCommonControlsEx
lngSize As Long
lngICC As Long
End Type
Private Declare Function InitCommonControlsEx Lib "comctl32.dll" _
(iccex As tagInitCommonControlsEx) As Boolean
Private Const ICC_USEREX_CLASSES = &H200
Public Function InitCommonControlsVB() As Boolean
On Error Resume Next
Dim iccex As tagInitCommonControlsEx
' Ensure CC available:
With iccex
.lngSize = LenB(iccex)
.lngICC = ICC_USEREX_CLASSES
End With
InitCommonControlsEx iccex
InitCommonControlsVB = (Err.Number = 0)
On Error Goto 0
End Function
Public Sub Main()
InitCommonControlsVB
'
' Start your application here:
'
End Sub
Create a file similar to this: http://pastebin.com/f689388b2
Then you add the manifest file to your resource file as type RT_MANIFEST (24)
I can't quite remember if that's all you need to do as I always just use the same pre-made .res file now.
Source: http://www.vbaccelerator.com/home/vb/code/libraries/XP_Visual_Styles/Using_XP_Visual_Styles_in_VB/article.asp

where is my ActiveX DLL project template in VS 2005?

In VB6, ActiveX DLL is listed as a project template but in VS 2005+ there is no such thing. Where is my good old ActiveX DLL template? Many thanks in advance.
A couple of concepts; .NET Assemblies are the functional equivalent to ActiveX DLLs in the .NET langauges. .NET Classes and method can be decorated with attribute that have various meaning in different context. A .NET Assembly can be turned into a ActiveX/COM DLL (or OCX) by using various attributes to assign the correct GUIDs.
A basic overview of setting a .NET assembly use COM is here.
Note that do google searches you should include VB6 .NET and COM (not ActiveX). COM generates more hits as it is the underlying technology behind the ActiveX term.
The MSDN article I linked shows a basic COM setup for a .NET Class. The attribute here is the ComClass Attribute.
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "6DB79AF2-F661-44AC-8458-62B06BFDD9E4"
Public Const InterfaceId As String = "EDED909C-9271-4670-BA32-109AE917B1D7"
Public Const EventsId As String = "17C731B8-CE61-4B5F-B114-10F3E46153AC"
#End Region
' A creatable COM class must have a Public Sub New()
' without parameters. Otherwise, the class will not be
' registered in the COM registry and cannot be created
' through CreateObject.
Public Sub New()
MyBase.New()
End Sub
End Class
There are other Attributes as well that are especially useful if you trying to subsitute a .NET assembly for an existing COM DLL or OCX. Finally .NET has a lot of different wizards that help you with the tedious details.
Try this: http://msmvps.com/blogs/pauldomag/archive/2006/08/16/107758.aspx
It outlines how to create an activex control and use it in a web page. As far as I know there's really no 'ActiveX' project template since .NET does it differently. However you can make your .Net controls visible to the COM world, which the article above illustrates.
It's not quite clear from you question, but if you want to be able to consume in VB6 (or some other com environment) something created in VS2005, you want to look at the Interop Forms Toolkit. This greatly simplifies interop between VB6 and VS2005. Now if you actually want to distribute those applications, installing what you created becomes a lot more fun (Hints: Don't use the GAC, install the .Net dll in the same directory as your application executable, and learn to use RegAsm.)
If you give a little description what you want to use the ActiveX.dll for (project library or User Control) and what environment you want to use it, more advice can be given.
I don’t know if this is what you are trying to do or not. But if you right click on the Toolbox in Visual Studio, in the popup menu select Choose Item…
When you get the “Choose Toolbox Item” dialog box come up, select the “COM Components” tab and check the COM Component(s) you want to add to the toolbox. I have done this to added the “Windows Media Player” to the toolbox and used it in a C# Winform.
From this dialog you can access any COM, OCX or ActiveX control loaded on you system.

Resources