ActiveState Code

Recipe 286159: Using ctypes to manipulate Windows registry and to register DLLs


Indicates the essentials of creating keys, or otherwise manipulating the Windows registry, using ctypes; also of registering a DLL.

Hurrah for ctypes!

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Creating the following registry key
# HKEY_CURRENT_USER\Please_Delete_Me\Level_1\Level_2

from ctypes import *
import win32con

advapi32 = oledll . LoadLibrary ( 'advapi32.dll' )
newKey = c_ulong ( )

nextKey = win32con . HKEY_CURRENT_USER
for subKey in [ 'Please_Delete_Me', 'Level_1', 'Level_2' ] :
    advapi32 . RegCreateKeyA ( nextKey, c_char_p ( subKey ), byref ( newKey ) )
    nextKey = newKey

advapi32 . RegCloseKey ( newKey )

# Registering and Unregistering a DLL

from ctypes import windll

dll = windll[<path-to-dll> ]
result = dll.DllRegisterServer()
result = dll.DllUnregisterServer()

Discussion

Reference for function calls used in above code, and others, is available at:

http://msdn.microsoft.com/library/en-us/sysinfo/base/registry_functions.asp

Reference to use of Dll[Un]registerServer (due to Thomas Heller):

http://aspn.activestate.com/ASPN/Mail/Message/ctypes-users/2102585

Sign in to comment