Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Ngen on setup, VB to C# translation

Ngen on setup, VB to C# translation

Scheduled Pinned Locked Moved C#
csharpvisual-studiocomtutorial
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bekjong
    wrote on last edited by
    #1

    Hi all, I was looking for a way to Ngen my program on the target system at setup. I found an example on MSDN[^] in Visual Basic. Although I don't know anything about VB, translation usually isn't too hard, but this time I'm finding it difficult. If anyone could lend me a hand at this that would be great. This is the code concerned:

    Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" _
    ( System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
    ByVal Buffer As System.Text.StringBuilder, _
    ByVal BufferLength As Integer, ByRef Length As Integer) As Integer

    _
    Public Overrides Sub Install(ByVal savedState As _
    System.Collections.IDictionary)

    MyBase.Install(savedState)
    Dim Args As String = Me.Context.Parameters.Item("Args")

    If Args = "" Then
    Throw New InstallException("No arguments specified")
    End If

    ' Gets the path to the Framework directory.
    Dim Path As New System.Text.StringBuilder(1024)
    Dim Size As Integer
    GetCORSystemDirectory(Path, Path.Capacity, Size)

    Dim P As Process
    ' Quotes the arguments, in case they have a space in them.
    Dim Si As New ProcessStartInfo(Path.ToString() & "ngen.exe", Chr(34) _
    & Args & Chr(34))
    Si.WindowStyle = ProcessWindowStyle.Hidden
    Try
    P = Process.Start(Si)
    P.WaitForExit()
    Catch e As Exception
    Throw New InstallException(e.Message)
    End Try
    End Sub

    Might be best to check out the code from the MSDN example though, since it provides the proper context. Thanks in advance!

    Standards are great! Everybody should have one!

    D 1 Reply Last reply
    0
    • B Bekjong

      Hi all, I was looking for a way to Ngen my program on the target system at setup. I found an example on MSDN[^] in Visual Basic. Although I don't know anything about VB, translation usually isn't too hard, but this time I'm finding it difficult. If anyone could lend me a hand at this that would be great. This is the code concerned:

      Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" _
      ( System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
      ByVal Buffer As System.Text.StringBuilder, _
      ByVal BufferLength As Integer, ByRef Length As Integer) As Integer

      _
      Public Overrides Sub Install(ByVal savedState As _
      System.Collections.IDictionary)

      MyBase.Install(savedState)
      Dim Args As String = Me.Context.Parameters.Item("Args")

      If Args = "" Then
      Throw New InstallException("No arguments specified")
      End If

      ' Gets the path to the Framework directory.
      Dim Path As New System.Text.StringBuilder(1024)
      Dim Size As Integer
      GetCORSystemDirectory(Path, Path.Capacity, Size)

      Dim P As Process
      ' Quotes the arguments, in case they have a space in them.
      Dim Si As New ProcessStartInfo(Path.ToString() & "ngen.exe", Chr(34) _
      & Args & Chr(34))
      Si.WindowStyle = ProcessWindowStyle.Hidden
      Try
      P = Process.Start(Si)
      P.WaitForExit()
      Catch e As Exception
      Throw New InstallException(e.Message)
      End Try
      End Sub

      Might be best to check out the code from the MSDN example though, since it provides the proper context. Thanks in advance!

      Standards are great! Everybody should have one!

      D Offline
      D Offline
      Declan Bright
      wrote on last edited by
      #2

      Give this a go: vb-to-csharp[^] It will get you most of the way.

      Declan Bright www.declanbright.com

      B 1 Reply Last reply
      0
      • D Declan Bright

        Give this a go: vb-to-csharp[^] It will get you most of the way.

        Declan Bright www.declanbright.com

        B Offline
        B Offline
        Bekjong
        wrote on last edited by
        #3

        Thanks, didn't know this site. This actually gives me all but the part I don't understand, which is:

        Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" _
        ( System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
        ByVal Buffer As System.Text.StringBuilder, _
        ByVal BufferLength As Integer, ByRef Length As Integer) As Integer

        Looks like some kind of P/Invoke signature import or something :confused:. The converter you gave me gets me a "stack empty" error.

        Standards are great! Everybody should have one!

        G 1 Reply Last reply
        0
        • B Bekjong

          Thanks, didn't know this site. This actually gives me all but the part I don't understand, which is:

          Private Declare Function GetCORSystemDirectory Lib "mscoree.dll" _
          ( System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
          ByVal Buffer As System.Text.StringBuilder, _
          ByVal BufferLength As Integer, ByRef Length As Integer) As Integer

          Looks like some kind of P/Invoke signature import or something :confused:. The converter you gave me gets me a "stack empty" error.

          Standards are great! Everybody should have one!

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          Yes, it looks like a P/Invoke signature. Did you try converting it to c# yourself?

          #region signature my articles #endregion

          B 2 Replies Last reply
          0
          • G Giorgi Dalakishvili

            Yes, it looks like a P/Invoke signature. Did you try converting it to c# yourself?

            #region signature my articles #endregion

            B Offline
            B Offline
            Bekjong
            wrote on last edited by
            #5

            [Message Deleted]

            G 1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              Yes, it looks like a P/Invoke signature. Did you try converting it to c# yourself?

              #region signature my articles #endregion

              B Offline
              B Offline
              Bekjong
              wrote on last edited by
              #6

              Ok, I actually got it to compile using: [DllImport("mscoree.dll")] static extern int GetCORSystemDirectory( [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] StringBuilder buffer, Int32 buffer_length, out Int32 length ); And then calling the function like this: System.Text.StringBuilder Path = new System.Text.StringBuilder(1024); int Size; GetCORSystemDirectory(Path, Path.Capacity, out Size); Still doesn't seem to be working though, can you tell me if the signature is right?

              Standards are great! Everybody should have one!

              G 1 Reply Last reply
              0
              • B Bekjong

                [Message Deleted]

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                In the original there is System.Text.StringBuilder, why did you change it with just String? If you've never worked with P/Invoke, I suggest you have a look at this: P/Invoke[^]

                #region signature my articles #endregion

                B 1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  In the original there is System.Text.StringBuilder, why did you change it with just String? If you've never worked with P/Invoke, I suggest you have a look at this: P/Invoke[^]

                  #region signature my articles #endregion

                  B Offline
                  B Offline
                  Bekjong
                  wrote on last edited by
                  #8

                  You're absolutely right. Should have noticed that. Take a look at my second try (below) please.

                  Standards are great! Everybody should have one!

                  1 Reply Last reply
                  0
                  • B Bekjong

                    Ok, I actually got it to compile using: [DllImport("mscoree.dll")] static extern int GetCORSystemDirectory( [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] StringBuilder buffer, Int32 buffer_length, out Int32 length ); And then calling the function like this: System.Text.StringBuilder Path = new System.Text.StringBuilder(1024); int Size; GetCORSystemDirectory(Path, Path.Capacity, out Size); Still doesn't seem to be working though, can you tell me if the signature is right?

                    Standards are great! Everybody should have one!

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #9

                    You should also have notices that original code uses System.Runtime.InteropServices.UnmanagedType.LPWStr and not System.Runtime.InteropServices.UnmanagedType.LPStr Also, third parameter of GetCORSystemDirectory Function is DWORD* so you should pass a reference to int.

                    #region signature my articles #endregion

                    B 1 Reply Last reply
                    0
                    • G Giorgi Dalakishvili

                      You should also have notices that original code uses System.Runtime.InteropServices.UnmanagedType.LPWStr and not System.Runtime.InteropServices.UnmanagedType.LPStr Also, third parameter of GetCORSystemDirectory Function is DWORD* so you should pass a reference to int.

                      #region signature my articles #endregion

                      B Offline
                      B Offline
                      Bekjong
                      wrote on last edited by
                      #10

                      Ok, thanks for the advice I've updated the code to:

                      [DllImport("mscoree.dll")]
                      static extern int GetCORSystemDirectory(
                      [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] StringBuilder buffer,
                      Int32 buffer_length,
                      ref Int32 length
                      );

                      And the call is now like this:

                      // Gets the path to the Framework directory.
                      System.Text.StringBuilder Path = new System.Text.StringBuilder(1024);
                      int Size = 0;
                      GetCORSystemDirectory(Path, Path.Capacity, ref Size);

                      Still it's not working :(. If I could only set a breakpoint to find out why, but this doesn't seem to be supported for installer projects...

                      Standards are great! Everybody should have one!

                      G 1 Reply Last reply
                      0
                      • B Bekjong

                        Ok, thanks for the advice I've updated the code to:

                        [DllImport("mscoree.dll")]
                        static extern int GetCORSystemDirectory(
                        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] StringBuilder buffer,
                        Int32 buffer_length,
                        ref Int32 length
                        );

                        And the call is now like this:

                        // Gets the path to the Framework directory.
                        System.Text.StringBuilder Path = new System.Text.StringBuilder(1024);
                        int Size = 0;
                        GetCORSystemDirectory(Path, Path.Capacity, ref Size);

                        Still it's not working :(. If I could only set a breakpoint to find out why, but this doesn't seem to be supported for installer projects...

                        Standards are great! Everybody should have one!

                        G Offline
                        G Offline
                        Giorgi Dalakishvili
                        wrote on last edited by
                        #11

                        Are you getting an error/exception or is it returning wrong data? Did you turn the stringbuilder into string?

                        #region signature my articles #endregion

                        B 1 Reply Last reply
                        0
                        • G Giorgi Dalakishvili

                          Are you getting an error/exception or is it returning wrong data? Did you turn the stringbuilder into string?

                          #region signature my articles #endregion

                          B Offline
                          B Offline
                          Bekjong
                          wrote on last edited by
                          #12

                          I'm on Vista and getting code 2869, no further explanation. I've googled it for a bit and now suspect it's a security problem. This would mean the p/invoke call is actually good now. This is the code:

                          using System;
                          using System.Collections.Generic;
                          using System.ComponentModel;
                          using System.Configuration.Install;
                          using System.Runtime.InteropServices;
                          using System.Text;
                          using System.Diagnostics;

                          namespace InstallationHelper
                          {
                          [RunInstaller(true)]
                          public partial class InstallationHelper : Installer
                          {
                          [DllImport("mscoree.dll")]
                          static extern int GetCORSystemDirectory(
                          [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] StringBuilder buffer,
                          Int32 buffer_length,
                          ref Int32 length
                          );

                              public InstallationHelper()
                              {
                                  InitializeComponent();
                              }
                          
                              \[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)\]
                              public override void Install(System.Collections.IDictionary savedState)
                              {
                                  base.Install(savedState);
                                  string Args = this.Context.Parameters\["Args"\];
                          
                                  if (Args == "")
                                  {
                                      throw new InstallException("No arguments specified");
                                  }
                          
                                  // Gets the path to the Framework directory.
                                  System.Text.StringBuilder Path = new System.Text.StringBuilder(1024);
                                  int Size = 0;
                                  GetCORSystemDirectory(Path, Path.Capacity, ref Size);
                          
                                  Process P;
                                  // Quotes the arguments, in case they have a space in them.
                                  ProcessStartInfo Si = new ProcessStartInfo(Path.ToString() + "ngen.exe", "\\"" + Args + "\\"");
                                  Si.WindowStyle = ProcessWindowStyle.Maximized;
                                  try
                                  {
                                      P = Process.Start(Si);
                                      P.WaitForExit();
                                  }
                                  catch (Exception e)
                                  {
                                      throw new InstallException(e.Message);
                                  }
                              }
                          
                          
                          }
                          

                          }

                          Standards are great! Everybody should have one!

                          G 1 Reply Last reply
                          0
                          • B Bekjong

                            I'm on Vista and getting code 2869, no further explanation. I've googled it for a bit and now suspect it's a security problem. This would mean the p/invoke call is actually good now. This is the code:

                            using System;
                            using System.Collections.Generic;
                            using System.ComponentModel;
                            using System.Configuration.Install;
                            using System.Runtime.InteropServices;
                            using System.Text;
                            using System.Diagnostics;

                            namespace InstallationHelper
                            {
                            [RunInstaller(true)]
                            public partial class InstallationHelper : Installer
                            {
                            [DllImport("mscoree.dll")]
                            static extern int GetCORSystemDirectory(
                            [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] StringBuilder buffer,
                            Int32 buffer_length,
                            ref Int32 length
                            );

                                public InstallationHelper()
                                {
                                    InitializeComponent();
                                }
                            
                                \[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)\]
                                public override void Install(System.Collections.IDictionary savedState)
                                {
                                    base.Install(savedState);
                                    string Args = this.Context.Parameters\["Args"\];
                            
                                    if (Args == "")
                                    {
                                        throw new InstallException("No arguments specified");
                                    }
                            
                                    // Gets the path to the Framework directory.
                                    System.Text.StringBuilder Path = new System.Text.StringBuilder(1024);
                                    int Size = 0;
                                    GetCORSystemDirectory(Path, Path.Capacity, ref Size);
                            
                                    Process P;
                                    // Quotes the arguments, in case they have a space in them.
                                    ProcessStartInfo Si = new ProcessStartInfo(Path.ToString() + "ngen.exe", "\\"" + Args + "\\"");
                                    Si.WindowStyle = ProcessWindowStyle.Maximized;
                                    try
                                    {
                                        P = Process.Start(Si);
                                        P.WaitForExit();
                                    }
                                    catch (Exception e)
                                    {
                                        throw new InstallException(e.Message);
                                    }
                                }
                            
                            
                            }
                            

                            }

                            Standards are great! Everybody should have one!

                            G Offline
                            G Offline
                            Giorgi Dalakishvili
                            wrote on last edited by
                            #13

                            Yes, according to google it looks like a problem with permissions and vista. You could try running it on XP or calling unmanaged function from a normal windows application so that you can set breakpoint.

                            #region signature my articles #endregion

                            B 1 Reply Last reply
                            0
                            • G Giorgi Dalakishvili

                              Yes, according to google it looks like a problem with permissions and vista. You could try running it on XP or calling unmanaged function from a normal windows application so that you can set breakpoint.

                              #region signature my articles #endregion

                              B Offline
                              B Offline
                              Bekjong
                              wrote on last edited by
                              #14

                              Ok, problem solved! Cleaning up seems to be the right thing to do here: Just removing the constructor was enough to get the thing going for some reason :). Thanks a bunch Giorgi, you've been a real help!

                              Standards are great! Everybody should have one!

                              G 1 Reply Last reply
                              0
                              • B Bekjong

                                Ok, problem solved! Cleaning up seems to be the right thing to do here: Just removing the constructor was enough to get the thing going for some reason :). Thanks a bunch Giorgi, you've been a real help!

                                Standards are great! Everybody should have one!

                                G Offline
                                G Offline
                                Giorgi Dalakishvili
                                wrote on last edited by
                                #15

                                Glad to help you :)

                                #region signature my articles #endregion

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups