Saturday, February 19, 2011

Win32 Api Usage using Ctypes

from ctypes import *
class Win32api():
def __init__(self):
#msdn for what dll to use
self.SetWindowText = windll.user32.SetWindowTextA
self.FindWindow = windll.user32.FindWindowA
def String(self,s):
return c_char_p(s)
def Int(self,i):
return c_int(i)
def Long(self,l):
return c_long(l)
#small test open command prompt type title lol
test = Win32api()
#none in python is == NULL in C
ret = test.FindWindow(None,test.String("lol"))
#ret holds the window handle HWND(which is really a long/int)
test.SetWindowText(ret,test.String("Command Prompt :D"))
print("done")
view raw ctypes.py hosted with ❤ by GitHub

This utilizes the Ctypes library that allows you to use functions and methods from both Dlls and .so files in both the windows and *nix Operating system i assume it would also work in mac since its based on unix
This test simply uses the FindWindow Function with SetWindowText to change the title of the Window that was found.
This is great if you want to make alot of Api Calls but dont want to program in C just for the purpose of using windows api calls
for refrence on how to use the functions you should use msdn and search the function

No comments:

Post a Comment