How can I use win32 API in Python?
What is the best and easiest way to do it?
Can you please provide some examples?

- 1,185
- 3
- 12
- 25

- 4,229
- 9
- 45
- 58
-
7yes, i actually did it before asking the question, and found several results. my aim is to start learning the most recommended solution to my problem. besides, not such question has been asked in stack overflow before, so I thought good answers from more experienced programmers can show the right path to take for beginners like me =) – kolistivra Jun 21 '09 at 23:53
-
13I googled and followed the links and I still don't know how. – Nosredna Jun 21 '09 at 23:57
-
Is there a specific use case? what are you trying to do that is not part of the standard python library? – SingleNegationElimination Jun 22 '09 at 01:30
-
@SashaChedygov you're welcome! It's never late to learn mistakes. – ulidtko Jul 07 '12 at 08:27
-
Just reading down through the comments and @SashaChedygov's one stuck out. Believe it or not, I got here by Googling. This is now (14 years later?) the topmost entry in the list of found articles. Ironic? – KDM Aug 24 '18 at 18:23
3 Answers
PyWin32 is the way to go - but how to use it? One approach is to begin with a concrete problem you're having and attempting to solve it. PyWin32 provides bindings for the Win32 API functions for which there are many, and you really have to pick a specific goal first.
In my Python 2.5 installation (ActiveState on Windows) the win32 package has a Demos folder packed with sample code of various parts of the library.
For example, here's CopyFileEx.py:
import win32file, win32api
import os
def ProgressRoutine(TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred,
StreamNumber, CallbackReason, SourceFile, DestinationFile, Data):
print Data
print TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred, StreamNumber, CallbackReason, SourceFile, DestinationFile
##if TotalBytesTransferred > 100000:
## return win32file.PROGRESS_STOP
return win32file.PROGRESS_CONTINUE
temp_dir=win32api.GetTempPath()
fsrc=win32api.GetTempFileName(temp_dir,'cfe')[0]
fdst=win32api.GetTempFileName(temp_dir,'cfe')[0]
print fsrc, fdst
f=open(fsrc,'w')
f.write('xxxxxxxxxxxxxxxx\n'*32768)
f.close()
## add a couple of extra data streams
f=open(fsrc+':stream_y','w')
f.write('yyyyyyyyyyyyyyyy\n'*32768)
f.close()
f=open(fsrc+':stream_z','w')
f.write('zzzzzzzzzzzzzzzz\n'*32768)
f.close()
operation_desc='Copying '+fsrc+' to '+fdst
win32file.CopyFileEx(fsrc, fdst, ProgressRoutine, operation_desc, False, win32file.COPY_FILE_RESTARTABLE)
It shows how to use the CopyFileEx function with a few others (such as GetTempPath and GetTempFileName). From this example you can get a "general feel" of how to work with this library.

- 263,248
- 89
- 350
- 412
-
3Style remark: 3 `open(fn, 'w').write('data')` lines in CPython communicate the same message as 9 'open,write,close' lines. – jfs Jun 22 '09 at 03:15
PyWin32, as mentioned by @chaos, is probably the most popular choice; the alternative is ctypes which is part of Python's standard library. For example, print ctypes.windll.kernel32.GetModuleHandleA(None)
will show the module-handle of the current module (EXE or DLL). A more extensive example of using ctypes to get at win32 APIs is here.

- 4,514
- 25
- 38

- 854,459
- 170
- 1,222
- 1,395
-
@RadimCernej: Try a google search for `import ctypes github`. You will get many hits. Or try this example on GitHub that uses `from ctypes import *`: https://github.com/erochest/snippets/blob/master/win32named.py – kevinarpe Apr 21 '15 at 11:38
-
They have not gotten pyWin32 to work with the MSYS2 build of Python. CTypes does. – Benilda Key Nov 10 '16 at 18:17
The important functions that you can to use in win32 Python are the message boxes, this is classical example of OK or Cancel.
result = win32api.MessageBox(None,"Do you want to open a file?", "title",1)
if result == 1:
print 'Ok'
elif result == 2:
print 'cancel'
The collection:
win32api.MessageBox(0,"msgbox", "title")
win32api.MessageBox(0,"ok cancel?", "title",1)
win32api.MessageBox(0,"abort retry ignore?", "title",2)
win32api.MessageBox(0,"yes no cancel?", "title",3)

- 4,083
- 5
- 40
- 47
-
Is there an option to display a hyperlink in such a MessageBox (or similar) with win32api? – bomben Aug 13 '20 at 13:19
-
1