ctypes useage

All new tutorial are submitted here.
They will be moved to the main Tutorials section once they have been approved by mods and admins.

ctypes useage

Postby mouseroot » Mon Feb 18, 2013 1:32 am

with ctypes you can call DLL methods from either local or
system dlls

using windll you can call methods from (kernel32.dll and user32.dll aswell as other dlls that can be found
in your system32 folder)

2 other conventions also exist that allow you to call local dlls and OLE dlls(sadly i dont have any code for these 2)

my favorite example is as follows:
Code: Select all
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"))


you can test this code right now by opening a command prompt window
and then type title lol
this will change the command prompt title to "lol"
now run the following script and it should change the title to "Command Prompt :D"

this is a small example of the power of ctypes if you can imagine a program that uses the win32 api alot
you can recreate it in python even with the proper return value types

If i can find it I have also been able to create and register a win32 gui inside python
including the WindowsProcedure.
Posts: 15
Joined: Sat Feb 16, 2013 1:14 am

Return to Tutorial Submissions

Who is online

Users browsing this forum: No registered users and 0 guests