Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Win32 API with Python?

Tags:

python

winapi

api

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

like image 255
kolistivra Avatar asked Jun 21 '09 23:06

kolistivra


People also ask

What is Win32 API in Python?

Python Python Win32api. Created: March-05, 2022. The win32api module provides various libraries and objects utilized to deal with the Windows system's Application Programming Interface (API).

Can you use Windows API with Python?

Interpreted languages like Python can also be used for API hooking and have some advantages over compiled languages. In this article, we explore when and why it's best to choose Python for hooking Windows APIs and explain how to use this language for setting hooks based on easy-to-follow examples.


2 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.

like image 85
Eli Bendersky Avatar answered Oct 26 '22 10:10

Eli Bendersky


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.

like image 29
Alex Martelli Avatar answered Oct 26 '22 10:10

Alex Martelli