Generating GUIDs in Visual Studio IDE

An easy way to generate GUIDs in the Visual Studio IDE is to create you own macros and do it through the Windows clipboard. The clipboard is accessed through System.Windows.Forms.Clipboard, easy enough, but the rub is that Visual Studio macros run in the context of an MTA apartment. You need to access the Clipboard .NET class in an STA thread. To do this, we create a new thread in a Visual Studio macro and make it an MTA thread.

I You can use the class below in a Visual Studio 2010 macro project. The same class should work in other Visual Studio versions, you may just have to tweak the Import statements a bit.

[code language=”VB”]
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Windows.Forms


” Class for setting and getting values from Windows clipboard in Visual Studio macros.

” Remarks: The call to System.Windows.Forms.Clipboard need to be make in an STA, Visual
” Studio runs in an MTA.
Public Class VSClip

Private _data As Object = Nothing
Private _fmt As String = Nothing

Public Sub SetData(ByVal Data As Object)
_data = Data
Dim t As New System.Threading.Thread(AddressOf Me._runSet)

‘ create STA thread to set clipboard data
t.SetApartmentState(Threading.ApartmentState.STA)
t.Start()
t.Join() ‘ wait for thread to finish
End Sub

Private Sub _runSet()
System.Windows.Forms.Clipboard.SetDataObject(_data, True)
End Sub

Public Function GetData(ByVal fmt As String) As Object
_fmt = fmt
Dim t As New System.Threading.Thread(AddressOf Me._runGet)

‘ create STA thread to get clipboard data
t.SetApartmentState(Threading.ApartmentState.STA)
t.Start()
t.Join() ‘ wait for thread to finish
Return _data
End Function

Private Sub _runGet()
_data = Nothing
Dim oData As IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
If Not oData Is Nothing Then
If oData.GetDataPresent(_fmt, True) Then
_data = oData.GetData(_fmt, True)
End If
End If
End Sub

End Class
[/code]

Now, for the task of adding a new GUID to the clipboard, we create a macros such as the one below:

[code language=”VB”]
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module ClipboardExchange

Public Sub NewGuid()
Dim cp As New VSClip
cp.SetData(Guid.NewGuid().ToString())
End Sub

End Module
[/code]

I included all of the Import ... stuff at the top of each snippet so you can easily copy and paste into a Visual Studio macro project.

This macro was written for Visual Studio 2010, but I have been doing this back since the days of Visual Studio 2003. You may have to tweak a little for different versions, maybe the Import statements, but the code stays pretty much the same.

This entry was posted in Visual Studio Macros and tagged . Bookmark the permalink.

3 Responses to Generating GUIDs in Visual Studio IDE

  1. Aris Green says:

    Ugh, I just found out there aren’t going to be macros in Visual Studio 11. You can write an addin to replace this macro. Maybe I’ll learn how to do macros in Notepad++!

    • William Bosacker says:

      There is no need for this at all. Since the release of the first version of Visual Studio, there has been TOOLS / Create GUID.

      • Aris Green says:

        The macro, from back when you could have macros in Visual Studio IDE, was for generating guids quickly, formatted the way you want them, with the end of line present from the Create GUID tool. Also, with the Create GUID tool, you had to reformat the guid for use in Wix project files, guid attributes in .NET files, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *