Created
May 18, 2012 23:52
-
-
Save ElemarJR/2728219 to your computer and use it in GitHub Desktop.
Automação para o IISExpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
namespace Automation | |
{ | |
internal class NativeMethods | |
{ | |
// Methods | |
[DllImport("user32.dll", SetLastError = true)] | |
internal static extern IntPtr GetTopWindow(IntPtr hWnd); | |
[DllImport("user32.dll", SetLastError = true)] | |
internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); | |
[DllImport("user32.dll", SetLastError = true)] | |
internal static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint lpdwProcessId); | |
[DllImport("user32.dll", SetLastError = true)] | |
internal static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); | |
} | |
internal class IISExpressProcessEnvelope : IDisposable | |
{ | |
private readonly Process process; | |
public IISExpressProcessEnvelope(Process process) | |
{ | |
this.process = process; | |
} | |
#region IDisposable Members | |
public void Dispose() | |
{ | |
NativeMethods.PostMessage( | |
GetHandleRef(), | |
0x12, | |
IntPtr.Zero, | |
IntPtr.Zero | |
); | |
process.Dispose(); | |
} | |
#endregion | |
private HandleRef GetHandleRef() | |
{ | |
IntPtr ptr = NativeMethods.GetTopWindow(IntPtr.Zero); | |
while (ptr != IntPtr.Zero) | |
{ | |
uint num; | |
NativeMethods.GetWindowThreadProcessId(ptr, out num); | |
if (process.Id == num) | |
return new HandleRef(null, ptr); | |
ptr = NativeMethods.GetWindow(ptr, 2); | |
} | |
throw new NullReferenceException("Process window not found."); | |
} | |
} | |
public class IISExpress : IDisposable | |
{ | |
private readonly IISExpressProcessEnvelope process; | |
public IISExpress(int port, string path) | |
{ | |
var info = new ProcessStartInfo | |
{ | |
FileName = @"c:\Program Files (x86)\IIS Express\iisexpress.exe", | |
Arguments = string.Format("/port:{0} /path:{1}", port, path) | |
}; | |
process = new IISExpressProcessEnvelope(Process.Start(info)); | |
} | |
#region IDisposable Members | |
public void Dispose() | |
{ | |
process.Dispose(); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment