/[OhmsLawCalc]/trunk/OhmsLawCalc/NativeMethods.cs
ViewVC logotype

Contents of /trunk/OhmsLawCalc/NativeMethods.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26 - (show annotations) (download)
Wed Jan 29 08:13:02 2014 UTC (5 years, 7 months ago) by william
File size: 1783 byte(s)

1 //#define ENABLE_NEW_CONSOLE_CREATION // when defined a new console will be created for the application if it is not already attached to one
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Runtime.InteropServices;
6 using System.Text;
7
8 namespace OhmsLawCalc
9 {
10 internal static class NativeMethods
11 {
12 const uint ATTACH_PARENT_PROCESS = 0x0ffffffff; // default value if not specifing a process ID
13 const int ERROR_ACCESS_DENIED = 5; // process was already attached to another console
14 [DllImport("kernel32")]
15 static private extern bool AllocConsole();
16 [DllImport("kernel32.dll", SetLastError = true)]
17 static private extern bool AttachConsole(uint dwProcessId);
18 [DllImport("kernel32.dll", SetLastError = true)]
19 static private extern int FreeConsole();
20 static public void ReleaseConsole() { FreeConsole(); }
21 static public void AttachToConsole(bool create_console)
22 {
23 #if !ENABLE_NEW_CONSOLE_CREATION
24 create_console = false; // force's no new console to be created
25 #endif
26 if (!AttachConsole(ATTACH_PARENT_PROCESS)) { if (create_console) { if (!AllocConsole()) { } } }
27 else
28 {
29 if (!AttachConsole(ATTACH_PARENT_PROCESS) && Marshal.GetLastWin32Error() == ERROR_ACCESS_DENIED)
30 {
31 // could not attach to console
32 if (create_console)
33 {
34 if (!AllocConsole()) { }
35 else { if (!AttachConsole(ATTACH_PARENT_PROCESS) && Marshal.GetLastWin32Error() == ERROR_ACCESS_DENIED) { } }
36 }
37 }
38 }
39 }
40 }
41 }

  ViewVC Help
Powered by ViewVC 1.1.22