Skip to content

Commit d7d936d

Browse files
authored
Merge pull request #53 from hakril/winapi-wide
Full unicode compatibility
2 parents 93540dd + 95c3f81 commit d7d936d

File tree

93 files changed

+19915
-17386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+19915
-17386
lines changed

.github/workflows/mypytest.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,8 @@ jobs:
6767
uses: EnricoMi/publish-unit-test-result-action/composite@v1
6868
if: always()
6969
with:
70-
files: junit/test-results.xml
70+
files: junit/test-results.xml
71+
check_name: PyTest Results for ${{ matrix.python-version}}-${{ matrix.python-bitness-to-test}}
72+
secondary_rate_limit_wait_seconds: 90
73+
seconds_between_github_writes: 10
74+
seconds_between_github_reads: 1

CHANGELOG

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
1.0.0:
2+
3+
Unicode everywhere:
4+
Whenever possible, PythonForWindows use W() (wide) APIs and returns unicode string.
5+
This means `str` for python3 & `unicode` for python2.7
6+
7+
windows.system.build_number now returns a int : the actual build number
8+
windows.system.build_number became windows.system.versionstr
9+
10+
111
Between 0.4 & 0.5:
212
== New features ==
313
- windows.security

README.md

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
# PythonForWindows
22

3-
[![Join the chat at https://gitter.im/PythonForWindows/general](https://badges.gitter.im/PythonForWindows/general.svg)](https://gitter.im/PythonForWindows/general)
43
[![Pytest](https://github.com/hakril/PythonForWindows/actions/workflows/mypytest.yml/badge.svg?branch=master)](https://github.com/hakril/PythonForWindows/actions/workflows/mypytest.yml)
54

65
PythonForWindows (PFW) is a base of code aimed to make interaction with `Windows` (on X86/X64) easier (for both 32 and 64 bits Python).
76
Its goal is to offer abstractions around some of the OS features in a (I hope) pythonic way.
87
It also tries to make the barrier between python and native execution thinner in both ways.
98
There is no external dependencies but it relies heavily on the `ctypes` module.
109

11-
12-
Some of this code is clean (IMHO) and some parts are just a wreck that works for now.
13-
Let's say that the codebase evolves with my needs and my curiosity.
10+
Let's say that the codebase evolves with my needs, my researches and my curiosity.
1411

1512
Complete online documentation is available [here][ONLINE_DOC]
1613
You can find some examples of code in the [samples directory][SAMPLE_DIR] or [online][ONLINE_SAMPLE].
1714

18-
PythonForWindows is principally known for its ALPC-RPC Client (see [samples](http://hakril.github.io/PythonForWindows/build/html/sample.html#windows-rpc)).
15+
PythonForWindows is principally known for:
16+
- its ALPC-RPC Client (see [samples](http://hakril.github.io/PythonForWindows/build/html/sample.html#windows-rpc))
17+
- its generated [ctypes definitions](https://github.com/hakril/PythonForWindows/tree/master/windows/generated_def).
1918

2019

21-
If you have any issue, question or suggestion do not hesitate to join [the Gitter channel](https://gitter.im/PythonForWindows/general).
20+
If you have any issue, question or suggestion do not hesitate to create an issue or reach me out.
2221
I am always glad to have feedbacks from people using this project.
2322

2423
## Installation
@@ -36,12 +35,17 @@ You can also install PythonForWindows by cloning it and using the ``setup.py`` s
3635
python setup.py install
3736
``
3837

39-
#### Python3
38+
## Encoding & unicode
39+
40+
PythonForWindows support `python2.7` & `python3` and is currently tested for `Python2.7`, `3.6` & `3.11` via [Github Workflow](https://github.com/hakril/PythonForWindows/actions/workflows/mypytest.yml)
41+
42+
Since 1.0.0, the code uses "wide APIs" whenever possible and accept/returns python3 `str` (py2.7 `unicode` type) almost everywhere. Any functions/APIs not accepting unicode string can be considered a bug if its not stated explicitly in the documentation.
4043

41-
PythonForWindows support python3 and is currently tested for Python2.7, 3.6 & 3.11 via [Github Workflow](https://github.com/hakril/PythonForWindows/actions/workflows/mypytest.yml)
42-
Regarding the handling of encoding in the project it's currently a mix of Ascii & Unicode that may be awkward on Python3 as automatic encoding/decoding is not present.
44+
### Python2
45+
46+
PythonForWindows continues to support python2.7 as its the only way to have it running on `Windows XP` & `Windows Server 2003` which are sadly still seen in production.
47+
Encoding errors at print time might be awkward for unicode string on python2, see the [PythonForWindows encoding guide](http://hakril.github.io/PythonForWindows/build/html/encoding.html) in the documentation.
4348

44-
The aim of passing the whole project under unicode is actvily ongoing.
4549

4650
## Overview
4751

@@ -531,6 +535,52 @@ The local debugger handles
531535
* Standard breakpoint ``int3``
532536
* Hardware Execution breakpoint ``DrX``
533537

538+
### Symbols
539+
540+
Classes around the Symbols APIs of `dbghelp.dll` are also implemented and can be used independently of the Debugger.
541+
The path of `dbghelp.dll` can also be given via the `PFW_DBGHELP_PATH` environment variable.
542+
543+
544+
```python
545+
# Python3
546+
547+
>>> from windows.debug import symbols
548+
>>> # symbols.set_dbghelp_path(MY_DBGHELP_PATH)
549+
>>> symbols.engine.options = 0 # Disable defered load
550+
>>> sh = symbols.VirtualSymbolHandler()
551+
>>> ntmod = sh.load_file(r"c:\windows\system32\ntdll.dll", addr=0x420000)
552+
>>> ntmod
553+
<SymbolModule name="ntdll" type=SymPdb pdb="ntdll.pdb" addr=0x420000>
554+
>>> ntmod.name
555+
'ntdll'
556+
>>> ntmod.path
557+
'c:\\windows\\system32\\ntdll.dll'
558+
>>> ntmod.pdb
559+
'c:\\Symbols\\ntdll.pdb\\8D5D5ED5D5B8AA609A82600C14E3004D1\\ntdll.pdb'
560+
>>> sym = sh["ntdll!LdrLoadDll"]
561+
>>> sym
562+
<SymbolInfoW name="LdrLoadDll" start=0x44a160 tag=SymTagFunction>
563+
>>> sym.fullname
564+
'ntdll!LdrLoadDll'
565+
>>> hex(sym.addr)
566+
'0x44a160'
567+
>>> sh.search("ntdll!*CreateFile")
568+
[<SymbolInfoW name="EtwpCreateFile" start=0x47d9ec tag=SymTagFunction>, <SymbolInfoW name="EtwpCreateFile" start=0x47d9ec tag=SymTagPublicSymbol>, <SymbolInfoW name="NtCreateFile" start=0x4c03e0 tag=SymTagPublicSymbol>, <SymbolInfoW name="ZwCreateFile" start=0x4c03e0 tag=SymTagPublicSymbol>, <SymbolInfoW name="__imp_NtCreateFile" start=0x55cb70 tag=SymTagPublicSymbol>]
569+
# Some types exploration
570+
>>> sh.get_type("ntdll!_PEB")
571+
<SymbolType name="_PEB" tag=_SymTagEnum.SymTagUDT(0xb)>
572+
>>> peb = _
573+
>>> peb = sh.get_type("ntdll!_PEB")
574+
>>> peb
575+
<SymbolType name="_PEB" tag=_SymTagEnum.SymTagUDT(0xb)>
576+
>>> peb.size
577+
2000
578+
>>> peb.children[:3]
579+
[<SymbolType name="InheritedAddressSpace" tag=_SymTagEnum.SymTagData(0x7)>, <SymbolType name="ReadImageFileExecOptions" tag=_SymTagEnum.SymTagData(0x7)>, <SymbolType name="BeingDebugged" tag=_SymTagEnum.SymTagData(0x7)>]
580+
>>> peb.children[2].offset
581+
2
582+
```
583+
534584
### Other stuff (see doc / samples)
535585

536586
- Network
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#define MM_SHARED_USER_DATA_VA 0x7FFE0000
2+
3+
#define XSTATE_LEGACY_FLOATING_POINT (0)
4+
#define XSTATE_LEGACY_SSE (1)
5+
#define XSTATE_GSSE (2)
6+
#define XSTATE_AVX (XSTATE_GSSE)
7+
#define XSTATE_MPX_BNDREGS (3)
8+
#define XSTATE_MPX_BNDCSR (4)
9+
#define XSTATE_AVX512_KMASK (5)
10+
#define XSTATE_AVX512_ZMM_H (6)
11+
#define XSTATE_AVX512_ZMM (7)
12+
#define XSTATE_IPT (8)
13+
#define XSTATE_LWP (62)
14+
#define MAXIMUM_XSTATE_FEATURES (64)

ctypes_generation/definitions/defines/services.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,36 @@
3838
#define SERVICE_USER_OWN_PROCESS 0x00000050
3939
#define SERVICE_USER_SHARE_PROCESS 0x00000060
4040

41+
42+
4143
#define SERVICE_WIN32 (SERVICE_WIN32_OWN_PROCESS | SERVICE_WIN32_SHARE_PROCESS)
4244

4345
#define SERVICE_INTERACTIVE_PROCESS 0x00000100
4446

47+
/*
48+
Legacy value changed with Win10 build 14942
49+
https://github.com/processhacker/processhacker/issues/120
50+
*/
4551
#define SERVICE_TYPE_ALL (SERVICE_WIN32 | SERVICE_ADAPTER | SERVICE_DRIVER | SERVICE_INTERACTIVE_PROCESS)
4652

4753

54+
/* New service Type from Win10 build 14942 */
55+
56+
#define SERVICE_USER_SERVICE 0x00000040
57+
#define SERVICE_USERSERVICE_INSTANCE 0x00000080
58+
#define SERVICE_USER_SHARE_PROCESS (SERVICE_USER_SERVICE |
59+
SERVICE_WIN32_SHARE_PROCESS)
60+
#define SERVICE_USER_OWN_PROCESS (SERVICE_USER_SERVICE |
61+
SERVICE_WIN32_OWN_PROCESS)
62+
#define SERVICE_PKG_SERVICE 0x00000200
63+
64+
65+
/* Make a value for the new SERVICE_TYPE_ALL ? */
66+
67+
68+
69+
70+
4871
#define SERVICE_BOOT_START 0x00000000
4972
#define SERVICE_SYSTEM_START 0x00000001
5073
#define SERVICE_AUTO_START 0x00000002

ctypes_generation/definitions/defines/windef.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,7 @@
668668

669669
#define TXFS_MINIVERSION_COMMITTED_VIEW (0x0000)
670670
#define TXFS_MINIVERSION_DIRTY_VIEW (0xFFFF)
671-
#define TXFS_MINIVERSION_DEFAULT_VIEW (0xFFFE)
671+
#define TXFS_MINIVERSION_DEFAULT_VIEW (0xFFFE)
672+
673+
674+
#define PROCESSOR_FEATURE_MAX 64

ctypes_generation/definitions/functions/symbols.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ BOOL WINAPI SymFromAddr(
2929
_Inout_ PSYMBOL_INFO Symbol
3030
);
3131

32+
BOOL WINAPI SymFromAddrW(
33+
[in] HANDLE hProcess,
34+
[in] DWORD64 Address,
35+
[out, optional] PDWORD64 Displacement,
36+
[in, out] PSYMBOL_INFOW Symbol
37+
);
38+
3239

3340
BOOL WINAPI SymGetModuleInfo64(
3441
_In_ HANDLE hProcess,
@@ -48,6 +55,12 @@ BOOL WINAPI SymInitialize(
4855
_In_ BOOL fInvadeProcess
4956
);
5057

58+
BOOL WINAPI SymInitializeW(
59+
[in] HANDLE hProcess,
60+
[in, optional] PCWSTR UserSearchPath,
61+
[in] BOOL fInvadeProcess
62+
);
63+
5164
BOOL WINAPI SymFromName(
5265
_In_ HANDLE hProcess,
5366
_In_ LPCSTR Name,
@@ -111,12 +124,26 @@ BOOL WINAPI SymEnumTypesByName(
111124
_In_ PVOID UserContext
112125
);
113126

127+
BOOL WINAPI SymEnumTypesByNameW(
128+
[in] HANDLE hProcess,
129+
[in] ULONG64 BaseOfDll,
130+
[in, optional] PCWSTR mask,
131+
[in] PVOID EnumSymbolsCallback,
132+
[in] PVOID UserContext
133+
);
134+
114135
BOOL WINAPI SymEnumerateModules64(
115136
_In_ HANDLE hProcess,
116137
_In_ PVOID EnumModulesCallback,
117138
_In_ PVOID UserContext
118139
);
119140

141+
BOOL WINAPI SymEnumerateModulesW64(
142+
[in] HANDLE hProcess,
143+
[in] PSYM_ENUMMODULES_CALLBACKW64 EnumModulesCallback,
144+
[in, optional] PVOID UserContext
145+
);
146+
120147
BOOL SymNext(
121148
HANDLE hProcess,
122149
PSYMBOL_INFO si
@@ -266,6 +293,13 @@ BOOL WINAPI SymGetTypeFromName(
266293
_Inout_ PSYMBOL_INFO Symbol
267294
);
268295

296+
BOOL WINAPI SymGetTypeFromNameW(
297+
[in] HANDLE hProcess,
298+
[in] ULONG64 BaseOfDll,
299+
[in] PCWSTR Name,
300+
[in, out] PSYMBOL_INFOW Symbol
301+
);
302+
269303

270304
BOOL WINAPI SymGetTypeInfo(
271305
_In_ HANDLE hProcess,

ctypes_generation/definitions/functions/syscall.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ NTSTATUS WINAPI NtQuerySystemInformation(
182182
);
183183

184184

185+
NTSTATUS WINAPI NtQuerySystemInformationEx(
186+
_In_ SYSTEM_INFORMATION_CLASS SystemInformationClass,
187+
_In_reads_bytes_(InputBufferLength) PVOID InputBuffer,
188+
_In_ ULONG InputBufferLength,
189+
_Out_writes_bytes_opt_(SystemInformationLength) PVOID SystemInformation,
190+
_In_ ULONG SystemInformationLength,
191+
_Out_opt_ PULONG ReturnLength
192+
);
193+
194+
185195
NTSTATUS WINAPI NtQueryInformationProcess(
186196
_In_ HANDLE ProcessHandle,
187197
_In_ PROCESSINFOCLASS ProcessInformationClass,

ctypes_generation/definitions/functions/winfunc.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ BOOL AllocConsole();
450450

451451
BOOL FreeConsole();
452452

453+
UINT WINAPI GetConsoleOutputCP();
454+
UINT WINAPI GetConsoleCP();
455+
453456
HANDLE WINAPI GetStdHandle(
454457
_In_ DWORD nStdHandle
455458
);

ctypes_generation/definitions/simple_types.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ PTRANSLATE_ADDRESS_ROUTINE64 = PVOID # StackWalk
240240
PSYMBOL_REGISTERED_CALLBACK64 = PVOID # Symbols
241241
PSYMBOL_REGISTERED_CALLBACK = PVOID # Symbols
242242
PSYM_ENUMPROCESSES_CALLBACK = PVOID # Symbols
243+
PSYM_ENUMMODULES_CALLBACKW64 = PVOID # Symbols
243244
ENUMRESNAMEPROCA = PVOID # Resources
244245
ENUMRESNAMEPROCW = PVOID # Resources
245246
ENUMRESTYPEPROCA = PVOID # Resources

0 commit comments

Comments
 (0)