Look at some hdden code in your VB project


Some code is always added to your VB project.


Try this:


 


Start VS (2008 or 2010 beta: the numbers below are for 2008)


 


File->New->Project->VB->Console App.


Build->Build Solution.


 


(I like to use temporary projects: see Use temporary projects in Visual Studio. I’m also using the General Profile (Tools->Import/Export Settings->Reset All Settings)


 


The build output window shows this:


—— Build started: Project: ConsoleApplication1, Configuration: Debug Any CPU ——


C:\Windows\Microsoft.NET\Framework\v3.5\Vbc.exe /noconfig /imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Generic,System.Data,System.Diagnostics,System.Linq,System.Xml.Linq /optioncompare:Binary /optionexplicit+ /optionstrict:custom /nowarn:42016,41999,42017,42018,42019,42032,42036,42020,42021,42022 /optioninfer+ /rootnamespace:ConsoleApplication1 /doc:obj\Debug\ConsoleApplication1.xml /define:”CONFIG=\”Debug\”,DEBUG=-1,TRACE=-1,_MyType=\”Console\”,PLATFORM=\”AnyCPU\”,_MYTYPE=\”Empty\”” /reference:”C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll”,”C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll”,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Deployment.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll,C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll,”C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll” /main:ConsoleApplication1.Module1 /debug+ /debug:full /filealign:512 /out:obj\Debug\ConsoleApplication1.exe /resource:obj\Debug\ConsoleApplication1.Resources.resources /target:exe Module1.vb “My Project\AssemblyInfo.vb” “My Project\Application.Designer.vb” “My Project\Resources.Designer.vb” “My Project\Settings.Designer.vb”


ConsoleApplication1 -> C:\Users\calvinh\AppData\Local\Temporary Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe


========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========


 


 


You can make your programs smaller by removing some features.


 


Now right click on the Project in Solution Explorer, and “choose Open Folder in Windows Explorer”, navigate to the ConsoleApplication1.EXE file to see how big it is.


 


Open a Visual Studio Command Prompt( Program Files->Visual Studio->Visual Studio Tools) and do this:


 


dir “C:\Users\calvinh\AppData\Local\Temporary Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe”


 


The size I get is 14,336 bytes.


 


Now use the Intermediate Language Disassembler :


Ildasm “C:\Users\calvinh\AppData\Local\Temporary Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe”


 


You can navigate the tree view to see things like MyProject, MySettingsProperty, etc.


 


For example, even if you’re not using web services, this code is in your binary:


 


.method private static !!T  Create__Instance__<.ctor T>(!!T ‘instance’) cil managed


{


  .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 )


  // Code size       32 (0x20)


  .maxstack  2


  .locals init ([0] !!T Create__Instance__,


           [1] bool VB$CG$t_bool$S0)


  IL_0000:  nop


  IL_0001:  ldarg.0


  IL_0002:  box        !!T


  IL_0007:  ldnull


  IL_0008:  ceq


  IL_000a:  stloc.1


  IL_000b:  ldloc.1


  IL_000c:  brfalse.s  IL_0018


  IL_000e:  call       !!0 [mscorlib]System.Activator::CreateInstance<!!0>()


  IL_0013:  stloc.0


  IL_0014:  br.s       IL_001e


  IL_0016:  br.s       IL_001d


  IL_0018:  nop


  IL_0019:  ldarg.0


  IL_001a:  stloc.0


  IL_001b:  br.s       IL_001e


  IL_001d:  nop


  IL_001e:  ldloc.0


  IL_001f:  ret


} // end of method MyWebServices::Create__Instance__


 


 


Where does all this code come from? Is there a simple way to get rid of it?


 


VB Intellisense and background compiler know about this extra stuff too.


Type in this:


        Dim r = My.WebService


Notice that after “My.” you see a a list of members of “My.”


 


Application


Computer


MySettings


Resources


Settings


User


WebServices


 


You can make the colored ones disappear.


Try this:


Project->Settings->Compiler->Advanced Compiler Options->Custom Constants


Put in _MYTYPE=”Empty”


 


Immediately, you’ll see that there are compile errors for the My.Webservice line and that intellisense doesn’t show the colored members.


 


Now rebuild and see that the file size is 12,288.


 


This size difference is multiplied if you have multiple VB projects.


You can see some of the source VB that is used to generate this code:


 


File->Open->File-> “C:\Windows\Microsoft.NET\Framework\v2.0.50727\Microsoft.VisualBasic.dll”


 


This will open the DLL in the VS Resource Editor. Navigate in the treeview to RCDATA->300 and you’ll see a binary dump of some code.


If you right click on the 300, choose Export->T.VB, then open T.VB, you see the contents below:


 


Now you can see the effect of changing “_MYTYPE” in the code.


 


See also:


Use temporary projects in Visual Studio


Host the CLR and Generate IL to call a MessageBox


 


 


<T.vb>


 


Option Strict On


Option Explicit On


Option Compare Binary


 


‘* Copyright (C) Microsoft Corporation. All Rights Reserved.


 


#If TARGET = “module” AndAlso _MYTYPE = “” Then


#Const _MYTYPE=“Empty”


#End If


 


#If _MYTYPE = “WindowsForms” Then


 


#Const _MYFORMS = True


#Const _MYWEBSERVICES = True


#Const _MYUSERTYPE = “Windows”


#Const _MYCOMPUTERTYPE = “Windows”


#Const _MYAPPLICATIONTYPE = “WindowsForms”


 


#ElseIf _MYTYPE = “WindowsFormsWithCustomSubMain” Then


 


#Const _MYFORMS = True


#Const _MYWEBSERVICES = True


#Const _MYUSERTYPE = “Windows”


#Const _MYCOMPUTERTYPE = “Windows”


#Const _MYAPPLICATIONTYPE = “Console”


 


#ElseIf _MYTYPE = “Windows” OrElse _MYTYPE = “” Then


 


#Const _MYWEBSERVICES = True


#Const _MYUSERTYPE = “Windows”


#Const _MYCOMPUTERTYPE = “Windows”


#Const _MYAPPLICATIONTYPE = “Windows”


 


#ElseIf _MYTYPE = “Console” Then


 


#Const _MYWEBSERVICES = True


#Const _MYUSERTYPE = “Windows”


#Const _MYCOMPUTERTYPE = “Windows”


#Const _MYAPPLICATIONTYPE = “Console”


 


#ElseIf _MYTYPE = “Web” Then


 


#Const _MYFORMS = False


#Const _MYWEBSERVICES = False


#Const _MYUSERTYPE = “Web”


#Const _MYCOMPUTERTYPE = “Web”


 


#ElseIf _MYTYPE = “WebControl” Then


 


#Const _MYFORMS = False


#Const _MYWEBSERVICES = True


#Const _MYUSERTYPE = “Web”


#Const _MYCOMPUTERTYPE = “Web”


 


#ElseIf _MYTYPE = “Custom” Then


 


#ElseIf _MYTYPE <> “Empty” Then


 


#Const _MYTYPE = “Empty”


 


#End If


 


#If _MYTYPE <> “Empty” Then


 


Namespace My


 


#If _MYAPPLICATIONTYPE = “WindowsForms” OrElse _MYAPPLICATIONTYPE = “Windows” OrElse _MYAPPLICATIONTYPE = “Console” Then


 


    <Global.System.CodeDom.Compiler.GeneratedCodeAttribute(“MyTemplate”, “8.0.0.0”)> _


    <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> Partial Friend Class MyApplication


 


#If _MYAPPLICATIONTYPE = “WindowsForms” Then


        Inherits Global.Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase


#If TARGET = “winexe” Then


        <Global.System.STAThread(), Global.System.Diagnostics.DebuggerHidden(), Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _


        Friend Shared Sub Main(ByVal Args As String())


            Try


               Global.System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(MyApplication.UseCompatibleTextRendering())


            Finally


            End Try              


            My.Application.Run(Args)


        End Sub


#End If


 


#ElseIf _MYAPPLICATIONTYPE = “Windows” Then


        Inherits Global.Microsoft.VisualBasic.ApplicationServices.ApplicationBase


#ElseIf _MYAPPLICATIONTYPE = “Console” Then


        Inherits Global.Microsoft.VisualBasic.ApplicationServices.ConsoleApplicationBase


#End If ‘_MYAPPLICATIONTYPE = “WindowsForms”


 


    End Class


 


#End If ‘#If _MYAPPLICATIONTYPE = “WindowsForms” Or _MYAPPLICATIONTYPE = “Windows” or _MYAPPLICATIONTYPE = “Console”


 


#If _MYCOMPUTERTYPE <> “” Then


 


    <Global.System.CodeDom.Compiler.GeneratedCodeAttribute(“MyTemplate”, “8.0.0.0”)> _


    <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> Partial Friend Class MyComputer


 


#If _MYCOMPUTERTYPE = “Windows” Then


        Inherits Global.Microsoft.VisualBasic.Devices.Computer


#ElseIf _MYCOMPUTERTYPE = “Web” Then


        Inherits Global.Microsoft.VisualBasic.Devices.ServerComputer


#End If


        <Global.System.Diagnostics.DebuggerHidden()> _


        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


        Public Sub New()


            MyBase.New()


        End Sub


    End Class


#End If


 


    <Global.Microsoft.VisualBasic.HideModuleName()> _


    <Global.System.CodeDom.Compiler.GeneratedCodeAttribute(“MyTemplate”, “8.0.0.0”)> _


    Friend Module MyProject


 


#If _MYCOMPUTERTYPE <> “” Then


        <Global.System.ComponentModel.Design.HelpKeyword(“My.Computer”)> _


        Friend ReadOnly Property Computer() As MyComputer


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_ComputerObjectProvider.GetInstance()


            End Get


        End Property


 


        Private ReadOnly m_ComputerObjectProvider As New ThreadSafeObjectProvider(Of MyComputer)


#End If


 


#If _MYAPPLICATIONTYPE = “Windows” Or _MYAPPLICATIONTYPE = “WindowsForms” Or _MYAPPLICATIONTYPE = “Console” Then


        <Global.System.ComponentModel.Design.HelpKeyword(“My.Application”)> _


        Friend ReadOnly Property Application() As MyApplication


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_AppObjectProvider.GetInstance()


            End Get


        End Property


        Private ReadOnly m_AppObjectProvider As New ThreadSafeObjectProvider(Of MyApplication)


#End If


 


#If _MYUSERTYPE = “Windows” Then


        <Global.System.ComponentModel.Design.HelpKeyword(“My.User”)> _


        Friend ReadOnly Property User() As Global.Microsoft.VisualBasic.ApplicationServices.User


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_UserObjectProvider.GetInstance()


            End Get


        End Property


        Private ReadOnly m_UserObjectProvider As New ThreadSafeObjectProvider(Of Global.Microsoft.VisualBasic.ApplicationServices.User)


#ElseIf _MYUSERTYPE = “Web” Then


        <Global.System.ComponentModel.Design.HelpKeyword(“My.User”)> _


        Friend ReadOnly Property User() As Global.Microsoft.VisualBasic.ApplicationServices.WebUser


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_UserObjectProvider.GetInstance()


            End Get


        End Property


        Private ReadOnly m_UserObjectProvider As New ThreadSafeObjectProvider(Of Global.Microsoft.VisualBasic.ApplicationServices.WebUser)


#End If


 


#If _MYFORMS = True Then


 


#Const STARTUP_MY_FORM_FACTORY = “My.MyProject.Forms”


 


        <Global.System.ComponentModel.Design.HelpKeyword(“My.Forms”)> _


        Friend ReadOnly Property Forms() As MyForms


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_MyFormsObjectProvider.GetInstance()


            End Get


        End Property


 


        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


        <Global.Microsoft.VisualBasic.MyGroupCollection(“System.Windows.Forms.Form”, “Create__Instance__”, “Dispose__Instance__”, “My.MyProject.Forms”)> _


        Friend NotInheritable Class MyForms


            <Global.System.Diagnostics.DebuggerHidden()> _


            Private Shared Function Create__Instance__(Of T As {New, Global.System.Windows.Forms.Form})(ByVal Instance As T) As T


                If Instance Is Nothing OrElse Instance.IsDisposed Then


                    If m_FormBeingCreated IsNot Nothing Then


                        If m_FormBeingCreated.ContainsKey(GetType(T)) = True Then


                            Throw New Global.System.InvalidOperationException(Global.Microsoft.VisualBasic.CompilerServices.Utils.GetResourceString(“WinForms_RecursiveFormCreate”))


                        End If


                    Else


                        m_FormBeingCreated = New Global.System.Collections.Hashtable()


                    End If


                    m_FormBeingCreated.Add(GetType(T), Nothing)


                    Try


                        Return New T()


                    Catch ex As Global.System.Reflection.TargetInvocationException When ex.InnerException IsNot Nothing


                        Dim BetterMessage As String = Global.Microsoft.VisualBasic.CompilerServices.Utils.GetResourceString(“WinForms_SeeInnerException”, ex.InnerException.Message)


                        Throw New Global.System.InvalidOperationException(BetterMessage, ex.InnerException)


                    Finally


                        m_FormBeingCreated.Remove(GetType(T))


                    End Try


                Else


                    Return Instance


                End If


            End Function


 


            <Global.System.Diagnostics.DebuggerHidden()> _


            Private Sub Dispose__Instance__(Of T As Global.System.Windows.Forms.Form)(ByRef instance As T)


                instance.Dispose()


                instance = Nothing


            End Sub


 


            <Global.System.Diagnostics.DebuggerHidden()> _


            <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


            Public Sub New()


               MyBase.New()


            End Sub


 


            <Global.System.ThreadStatic()> Private Shared m_FormBeingCreated As Global.System.Collections.Hashtable


 


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> Public Overrides Function Equals(ByVal o As Object) As Boolean


                Return MyBase.Equals(o)


            End Function


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> Public Overrides Function GetHashCode() As Integer


                Return MyBase.GetHashCode


            End Function


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _


            Friend Overloads Function [GetType]() As Global.System.Type


                Return GetType(MyForms)


            End Function


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> Public Overrides Function ToString() As String


                Return MyBase.ToString


            End Function


        End Class


 


        Private m_MyFormsObjectProvider As New ThreadSafeObjectProvider(Of MyForms)


 


#End If


 


#If _MYWEBSERVICES = True Then


 


        <Global.System.ComponentModel.Design.HelpKeyword(“My.WebServices”)> _


        Friend ReadOnly Property WebServices() As MyWebServices


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_MyWebServicesObjectProvider.GetInstance()


            End Get


        End Property


 


        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


        <Global.Microsoft.VisualBasic.MyGroupCollection(“System.Web.Services.Protocols.SoapHttpClientProtocol”, “Create__Instance__”, “Dispose__Instance__”, “”)> _


        Friend NotInheritable Class MyWebServices


 


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never), Global.System.Diagnostics.DebuggerHidden()> _


            Public Overrides Function Equals(ByVal o As Object) As Boolean


                Return MyBase.Equals(o)


            End Function


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never), Global.System.Diagnostics.DebuggerHidden()> _


            Public Overrides Function GetHashCode() As Integer


                Return MyBase.GetHashCode


            End Function


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never), Global.System.Diagnostics.DebuggerHidden()> _


            Friend Overloads Function [GetType]() As Global.System.Type


                Return GetType(MyWebServices)


            End Function


            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never), Global.System.Diagnostics.DebuggerHidden()> _


            Public Overrides Function ToString() As String


                Return MyBase.ToString


            End Function


 


            <Global.System.Diagnostics.DebuggerHidden()> _


            Private Shared Function Create__Instance__(Of T As {New})(ByVal instance As T) As T


                If instance Is Nothing Then


                    Return New T()


                Else


                    Return instance


                End If


            End Function


 


            <Global.System.Diagnostics.DebuggerHidden()> _


            Private Sub Dispose__Instance__(Of T)(ByRef instance As T)


                instance = Nothing


            End Sub


 


            <Global.System.Diagnostics.DebuggerHidden()> _


            <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


            Public Sub New()


                MyBase.New()


            End Sub


        End Class


 


        Private ReadOnly m_MyWebServicesObjectProvider As New ThreadSafeObjectProvider(Of MyWebServices)


#End If


 


#If _MYTYPE = “Web” Then


 


        <Global.System.ComponentModel.Design.HelpKeyword(“My.Request”)> _


        Friend ReadOnly Property Request() As Global.System.Web.HttpRequest


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Dim CurrentContext As Global.System.Web.HttpContext = Global.System.Web.HttpContext.Current


                If CurrentContext IsNot Nothing Then


                    Return CurrentContext.Request


                End If


                Return Nothing


            End Get


        End Property


 


        <Global.System.ComponentModel.Design.HelpKeyword(“My.Response”)> _


        Friend ReadOnly Property Response() As Global.System.Web.HttpResponse


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Dim CurrentContext As Global.System.Web.HttpContext = Global.System.Web.HttpContext.Current


                If CurrentContext IsNot Nothing Then


                    Return CurrentContext.Response


                End If


                Return Nothing


            End Get


        End Property


 


        <Global.System.ComponentModel.Design.HelpKeyword(“My.Application.Log”)> _


        Friend ReadOnly Property Log() As Global.Microsoft.VisualBasic.Logging.AspLog


            <Global.System.Diagnostics.DebuggerHidden()> _


            Get


                Return m_LogObjectProvider.GetInstance()


            End Get


        End Property


 


        Private ReadOnly m_LogObjectProvider As New ThreadSafeObjectProvider(Of Global.Microsoft.VisualBasic.Logging.AspLog)


 


#End If  ‘_MYTYPE=”Web”


 


        <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


        <Global.System.Runtime.InteropServices.ComVisible(False)> _


        Friend NotInheritable Class ThreadSafeObjectProvider(Of T As New)


            Friend ReadOnly Property GetInstance() As T


#If TARGET = “library” Then


                <Global.System.Diagnostics.DebuggerHidden()> _


                Get


                    Dim Value As T = m_Context.Value


                    If Value Is Nothing Then


                        Value = New T


                        m_Context.Value() = Value


                    End If


                    Return Value


                End Get


#Else


                <Global.System.Diagnostics.DebuggerHidden()> _


                Get


                    If m_ThreadStaticValue Is Nothing Then m_ThreadStaticValue = New T


                    Return m_ThreadStaticValue


                End Get


#End If


            End Property


 


            <Global.System.Diagnostics.DebuggerHidden()> _


            <Global.System.ComponentModel.EditorBrowsableAttribute(Global.System.ComponentModel.EditorBrowsableState.Never)> _


            Public Sub New()


                MyBase.New()


            End Sub


 


#If TARGET = “library” Then


            Private ReadOnly m_Context As New Global.Microsoft.VisualBasic.MyServices.Internal.ContextValue(Of T)


#Else


            <Global.System.Runtime.CompilerServices.CompilerGenerated(), Global.System.ThreadStatic()> Private Shared m_ThreadStaticValue As T


#End If


        End Class


    End Module


End Namespace


#End If


 


</T.vb>


 

Comments (4)

  1. svalentine says:

    Excellent information! My projects are very concerned about the assembly’s footprint.

    I noticed after installing VS2005, that all of my assemblies grew by an average of around 40% due to this "My" namespace bloat.

    Since then, I’ve made it a common process to open each .vbproj file and change the <MyType> element from whatever to "Empty", but had no idea that I could do it any other way.

    The Custom Constant sounds like the better approach to pre-empting this stuff getting added to my assemblies.

    Thanks,

    Skip Valentine

  2. Chris Baughman says:

    I’m working in Excel with VB6.5 and am having a problem with the outline code that was already written.  It works from level 0 through level 7 and then I get an error message at level 8.  I’ve done a google search for the error only to find that it is pretty generic.  Could you point me in the right direction on how to fix the issue?  Thank you so much for your help.

    Christine.Baughman@cableone.biz

  3. Greg says:

    Removing unused functions and methods when static linking an application is one of the best ways to reduce the footprint and improve the reliability of your application.

    Most decent linkers from the 1980s onward did this with library and DLL type modules.

  4. Pedro says:

    I've been looking for a way to get rid of those extra classes to get a more accurate code-coverage report for our unit tests.  This is exactly what I needed.  Thanks!