winpr: correctly implement APC
[platform/upstream/freerdp.git] / winpr / include / winpr / thread.h
1 /**
2  * WinPR: Windows Portable Runtime
3  * Process Thread Functions
4  *
5  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #ifndef WINPR_THREAD_H
21 #define WINPR_THREAD_H
22
23 #include <winpr/winpr.h>
24 #include <winpr/wtypes.h>
25
26 #include <winpr/spec.h>
27 #include <winpr/handle.h>
28
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #endif
33
34 #ifndef _WIN32
35
36         typedef struct _STARTUPINFOA
37         {
38                 DWORD cb;
39                 LPSTR lpReserved;
40                 LPSTR lpDesktop;
41                 LPSTR lpTitle;
42                 DWORD dwX;
43                 DWORD dwY;
44                 DWORD dwXSize;
45                 DWORD dwYSize;
46                 DWORD dwXCountChars;
47                 DWORD dwYCountChars;
48                 DWORD dwFillAttribute;
49                 DWORD dwFlags;
50                 WORD wShowWindow;
51                 WORD cbReserved2;
52                 LPBYTE lpReserved2;
53                 HANDLE hStdInput;
54                 HANDLE hStdOutput;
55                 HANDLE hStdError;
56         } STARTUPINFOA, *LPSTARTUPINFOA;
57
58         typedef struct _STARTUPINFOW
59         {
60                 DWORD cb;
61                 LPWSTR lpReserved;
62                 LPWSTR lpDesktop;
63                 LPWSTR lpTitle;
64                 DWORD dwX;
65                 DWORD dwY;
66                 DWORD dwXSize;
67                 DWORD dwYSize;
68                 DWORD dwXCountChars;
69                 DWORD dwYCountChars;
70                 DWORD dwFillAttribute;
71                 DWORD dwFlags;
72                 WORD wShowWindow;
73                 WORD cbReserved2;
74                 LPBYTE lpReserved2;
75                 HANDLE hStdInput;
76                 HANDLE hStdOutput;
77                 HANDLE hStdError;
78         } STARTUPINFOW, *LPSTARTUPINFOW;
79
80 #ifdef UNICODE
81         typedef STARTUPINFOW STARTUPINFO;
82         typedef LPSTARTUPINFOW LPSTARTUPINFO;
83 #else
84         typedef STARTUPINFOA STARTUPINFO;
85         typedef LPSTARTUPINFOA LPSTARTUPINFO;
86 #endif
87
88 #define STARTF_USESHOWWINDOW 0x00000001
89 #define STARTF_USESIZE 0x00000002
90 #define STARTF_USEPOSITION 0x00000004
91 #define STARTF_USECOUNTCHARS 0x00000008
92 #define STARTF_USEFILLATTRIBUTE 0x00000010
93 #define STARTF_RUNFULLSCREEN 0x00000020
94 #define STARTF_FORCEONFEEDBACK 0x00000040
95 #define STARTF_FORCEOFFFEEDBACK 0x00000080
96 #define STARTF_USESTDHANDLES 0x00000100
97 #define STARTF_USEHOTKEY 0x00000200
98 #define STARTF_TITLEISLINKNAME 0x00000800
99 #define STARTF_TITLEISAPPID 0x00001000
100 #define STARTF_PREVENTPINNING 0x00002000
101
102         /* Process */
103
104 #define LOGON_WITH_PROFILE 0x00000001
105 #define LOGON_NETCREDENTIALS_ONLY 0x00000002
106 #define LOGON_ZERO_PASSWORD_BUFFER 0x80000000
107
108         WINPR_API BOOL CreateProcessA(LPCSTR lpApplicationName, LPSTR lpCommandLine,
109                                       LPSECURITY_ATTRIBUTES lpProcessAttributes,
110                                       LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
111                                       DWORD dwCreationFlags, LPVOID lpEnvironment,
112                                       LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo,
113                                       LPPROCESS_INFORMATION lpProcessInformation);
114
115         WINPR_API BOOL CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
116                                       LPSECURITY_ATTRIBUTES lpProcessAttributes,
117                                       LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
118                                       DWORD dwCreationFlags, LPVOID lpEnvironment,
119                                       LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
120                                       LPPROCESS_INFORMATION lpProcessInformation);
121
122         WINPR_API BOOL CreateProcessAsUserA(HANDLE hToken, LPCSTR lpApplicationName,
123                                             LPSTR lpCommandLine,
124                                             LPSECURITY_ATTRIBUTES lpProcessAttributes,
125                                             LPSECURITY_ATTRIBUTES lpThreadAttributes,
126                                             BOOL bInheritHandles, DWORD dwCreationFlags,
127                                             LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
128                                             LPSTARTUPINFOA lpStartupInfo,
129                                             LPPROCESS_INFORMATION lpProcessInformation);
130
131         WINPR_API BOOL CreateProcessAsUserW(HANDLE hToken, LPCWSTR lpApplicationName,
132                                             LPWSTR lpCommandLine,
133                                             LPSECURITY_ATTRIBUTES lpProcessAttributes,
134                                             LPSECURITY_ATTRIBUTES lpThreadAttributes,
135                                             BOOL bInheritHandles, DWORD dwCreationFlags,
136                                             LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
137                                             LPSTARTUPINFOW lpStartupInfo,
138                                             LPPROCESS_INFORMATION lpProcessInformation);
139
140         WINPR_API BOOL CreateProcessWithLogonA(LPCSTR lpUsername, LPCSTR lpDomain, LPCSTR lpPassword,
141                                                DWORD dwLogonFlags, LPCSTR lpApplicationName,
142                                                LPSTR lpCommandLine, DWORD dwCreationFlags,
143                                                LPVOID lpEnvironment, LPCSTR lpCurrentDirectory,
144                                                LPSTARTUPINFOA lpStartupInfo,
145                                                LPPROCESS_INFORMATION lpProcessInformation);
146
147         WINPR_API BOOL CreateProcessWithLogonW(LPCWSTR lpUsername, LPCWSTR lpDomain, LPCWSTR lpPassword,
148                                                DWORD dwLogonFlags, LPCWSTR lpApplicationName,
149                                                LPWSTR lpCommandLine, DWORD dwCreationFlags,
150                                                LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
151                                                LPSTARTUPINFOW lpStartupInfo,
152                                                LPPROCESS_INFORMATION lpProcessInformation);
153
154         WINPR_API BOOL CreateProcessWithTokenA(HANDLE hToken, DWORD dwLogonFlags,
155                                                LPCSTR lpApplicationName, LPSTR lpCommandLine,
156                                                DWORD dwCreationFlags, LPVOID lpEnvironment,
157                                                LPCSTR lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo,
158                                                LPPROCESS_INFORMATION lpProcessInformation);
159
160         WINPR_API BOOL CreateProcessWithTokenW(HANDLE hToken, DWORD dwLogonFlags,
161                                                LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
162                                                DWORD dwCreationFlags, LPVOID lpEnvironment,
163                                                LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo,
164                                                LPPROCESS_INFORMATION lpProcessInformation);
165
166 #ifdef UNICODE
167 #define CreateProcess CreateProcessW
168 #define CreateProcessAsUser CreateProcessAsUserW
169 #define CreateProcessWithLogon CreateProcessWithLogonW
170 #define CreateProcessWithToken CreateProcessWithTokenW
171 #else
172 #define CreateProcess CreateProcessA
173 #define CreateProcessAsUser CreateProcessAsUserA
174 #define CreateProcessWithLogon CreateProcessWithLogonA
175 #define CreateProcessWithToken CreateProcessWithTokenA
176 #endif
177
178         DECLSPEC_NORETURN WINPR_API VOID ExitProcess(UINT uExitCode);
179         WINPR_API BOOL GetExitCodeProcess(HANDLE hProcess, LPDWORD lpExitCode);
180
181         WINPR_API HANDLE _GetCurrentProcess(void);
182         WINPR_API DWORD GetCurrentProcessId(void);
183
184         WINPR_API BOOL TerminateProcess(HANDLE hProcess, UINT uExitCode);
185
186         /* Process Argument Vector Parsing */
187
188         WINPR_API LPWSTR* CommandLineToArgvW(LPCWSTR lpCmdLine, int* pNumArgs);
189
190 #ifdef UNICODE
191 #define CommandLineToArgv CommandLineToArgvW
192 #else
193 #define CommandLineToArgv CommandLineToArgvA
194 #endif
195
196         /* Thread */
197
198 #define CREATE_SUSPENDED 0x00000004
199 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
200
201         WINPR_API HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize,
202                                       LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter,
203                                       DWORD dwCreationFlags, LPDWORD lpThreadId);
204
205         WINPR_API HANDLE CreateRemoteThread(HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes,
206                                             SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
207                                             LPVOID lpParameter, DWORD dwCreationFlags,
208                                             LPDWORD lpThreadId);
209
210         WINPR_API DECLSPEC_NORETURN VOID ExitThread(DWORD dwExitCode);
211         WINPR_API BOOL GetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode);
212
213         WINPR_API HANDLE _GetCurrentThread(void);
214         WINPR_API DWORD GetCurrentThreadId(void);
215
216         typedef void (*PAPCFUNC)(ULONG_PTR Parameter);
217         WINPR_API DWORD QueueUserAPC(PAPCFUNC pfnAPC, HANDLE hThread, ULONG_PTR dwData);
218
219         WINPR_API DWORD ResumeThread(HANDLE hThread);
220         WINPR_API DWORD SuspendThread(HANDLE hThread);
221         WINPR_API BOOL SwitchToThread(void);
222
223         WINPR_API BOOL TerminateThread(HANDLE hThread, DWORD dwExitCode);
224
225         /* Processor */
226
227         WINPR_API DWORD GetCurrentProcessorNumber(void);
228
229         /* Thread-Local Storage */
230
231 #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
232
233         WINPR_API DWORD TlsAlloc(void);
234         WINPR_API LPVOID TlsGetValue(DWORD dwTlsIndex);
235         WINPR_API BOOL TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue);
236         WINPR_API BOOL TlsFree(DWORD dwTlsIndex);
237
238 #else
239
240 /*
241  * GetCurrentProcess / GetCurrentThread cause a conflict on Mac OS X
242  */
243 #define _GetCurrentProcess GetCurrentProcess
244 #define _GetCurrentThread GetCurrentThread
245
246 #endif
247
248         /* CommandLineToArgvA is not present in the original Windows API, WinPR always exports it */
249
250         WINPR_API LPSTR* CommandLineToArgvA(LPCSTR lpCmdLine, int* pNumArgs);
251
252 #if defined(WITH_DEBUG_THREADS)
253         WINPR_API VOID DumpThreadHandles(void);
254 #endif
255
256 #ifdef __cplusplus
257 }
258 #endif
259
260 #endif /* WINPR_THREAD_H */