Ngen on setup, VB to C# translation
-
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 SubMight 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!
-
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 SubMight 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!
Give this a go: vb-to-csharp[^] It will get you most of the way.
Declan Bright www.declanbright.com
-
Give this a go: vb-to-csharp[^] It will get you most of the way.
Declan Bright www.declanbright.com
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 IntegerLooks 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!
-
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 IntegerLooks 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!
Yes, it looks like a P/Invoke signature. Did you try converting it to c# yourself?
#region signature my articles #endregion
-
Yes, it looks like a P/Invoke signature. Did you try converting it to c# yourself?
#region signature my articles #endregion
-
Yes, it looks like a P/Invoke signature. Did you try converting it to c# yourself?
#region signature my articles #endregion
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!
-
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
-
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
-
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!
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
-
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
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!
-
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!
Are you getting an error/exception or is it returning wrong data? Did you turn the stringbuilder into string?
#region signature my articles #endregion
-
Are you getting an error/exception or is it returning wrong data? Did you turn the stringbuilder into string?
#region signature my articles #endregion
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!
-
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!
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
-
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
-
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!
Glad to help you :)
#region signature my articles #endregion