forked from apache/logging-log4net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
177 lines (159 loc) · 7.95 KB
/
NativeMethods.cs
File metadata and controls
177 lines (159 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using log4net.Appender;
using System;
using System.Runtime.InteropServices;
namespace log4net.Util
{
/// <summary>
/// Native Methods
/// </summary>
/// <author>Jan Friedrich</author>
internal static class NativeMethods
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool DuplicateToken(IntPtr existingTokenHandle, int securityImpersonationLevel, ref IntPtr duplicateTokenHandle);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern int GetConsoleOutputCP();
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool SetConsoleTextAttribute(
IntPtr consoleHandle,
ushort attributes);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool GetConsoleScreenBufferInfo(
IntPtr consoleHandle,
out ConsoleScreenBufferInfo bufferInfo);
internal const uint StdOutputHandle = unchecked((uint)-11);
internal const uint StdErrorHandle = unchecked((uint)-12);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr GetStdHandle(uint type);
[StructLayout(LayoutKind.Sequential)]
internal struct Coord
{
public ushort x;
public ushort y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SmallRect
{
public ushort Left;
public ushort Top;
public ushort Right;
public ushort Bottom;
}
[StructLayout(LayoutKind.Sequential)]
internal struct ConsoleScreenBufferInfo
{
public Coord dwSize;
public Coord dwCursorPosition;
public ushort wAttributes;
public SmallRect srWindow;
public Coord dwMaximumWindowSize;
}
/// <summary>
/// Formats a message string.
/// </summary>
/// <param name="dwFlags">Formatting options, and how to interpret the <paramref name="lpSource" /> parameter.</param>
/// <param name="lpSource">Location of the message definition.</param>
/// <param name="dwMessageId">Message identifier for the requested message.</param>
/// <param name="dwLanguageId">Language identifier for the requested message.</param>
/// <param name="lpBuffer">If <paramref name="dwFlags" /> includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the <c>LocalAlloc</c> function, and places the pointer to the buffer at the address specified in <paramref name="lpBuffer" />.</param>
/// <param name="nSize">If the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is not set, this parameter specifies the maximum number of TCHARs that can be stored in the output buffer. If FORMAT_MESSAGE_ALLOCATE_BUFFER is set, this parameter specifies the minimum number of TCHARs to allocate for an output buffer.</param>
/// <param name="arguments">Pointer to an array of values that are used as insert values in the formatted message.</param>
/// <remarks>
/// <para>
/// The function requires a message definition as input. The message definition can come from a
/// buffer passed into the function. It can come from a message table resource in an
/// already-loaded module. Or the caller can ask the function to search the system's message
/// table resource(s) for the message definition. The function finds the message definition
/// in a message table resource based on a message identifier and a language identifier.
/// The function copies the formatted message text to an output buffer, processing any embedded
/// insert sequences if requested.
/// </para>
/// <para>
/// To prevent the usage of unsafe code, this stub does not support inserting values in the formatted message.
/// </para>
/// </remarks>
/// <returns>
/// <para>
/// If the function succeeds, the return value is the number of TCHARs stored in the output
/// buffer, excluding the terminating null character.
/// </para>
/// <para>
/// If the function fails, the return value is zero. To get extended error information,
/// call <see cref="Marshal.GetLastWin32Error()" />.
/// </para>
/// </returns>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern int FormatMessage(
int dwFlags,
ref IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
ref string lpBuffer,
int nSize,
IntPtr arguments);
/// <summary>
/// Stub for OutputDebugString native method
/// </summary>
/// <param name="message">the string to output</param>
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern void OutputDebugString(string message);
/// <summary>
/// Open connection to system logger.
/// </summary>
[DllImport("libc")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5392:Use DefaultDllImportSearchPaths attribute for P/Invokes", Justification = "Only Linux")]
internal static extern void openlog(IntPtr ident, int option, LocalSyslogAppender.SyslogFacility facility);
/// <summary>
/// Generate a log message.
/// </summary>
/// <remarks>
/// <para>
/// The libc syslog method takes a format string and a variable argument list similar
/// to the classic printf function. As this type of vararg list is not supported
/// by C# we need to specify the arguments explicitly. Here we have specified the
/// format string with a single message argument. The caller must set the format
/// string to <c>"%s"</c>.
/// </para>
/// </remarks>
[DllImport("libc", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5392:Use DefaultDllImportSearchPaths attribute for P/Invokes", Justification = "Only Linux")]
internal static extern void syslog(int priority, string format, string message);
/// <summary>
/// Close descriptor used to write to system logger.
/// </summary>
[DllImport("libc")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5392:Use DefaultDllImportSearchPaths attribute for P/Invokes", Justification = "Only Linux")]
internal static extern void closelog();
}
}