from ctypes import *
from capstone import *
k32 = windll.kernel32
dbghelp = cdll.LoadLibrary('dbghelp.dll')
handle = k32.LoadLibraryEx(modulePath, 0, 1) #DONT_RESOLVE_DLL_REFERENCES
hproc = k32.GetCurrentProcess()
ret = dbghelp.SymInitialize(hproc, sympath, 1)
SymInitialize returns 0
GetLastError returns -1073741813
What I'm doing wrong, same code written in C++ works just fine.
I specified exact function signature, now it works.
from ctypes.wintypes import HANDLE, LPCSTR, UINT
si = windll.dbghelp.SymInitialize
si.argtypes = [HANDLE, LPCSTR, UINT]
Related
This is the code I am using, but how can i convert the memory address with a pointer I have created? I am using this way of reading memory, so i can get 64 bit values, but it is kind of annoying to look up the new memory addresses each time I start the .exe.
import ctypes as c
from ctypes import wintypes as w
from ctypes import *
from ctypes.wintypes import *
pid = 14840
k32 = c.WinDLL('kernel32', use_last_error=True)
OpenProcess = k32.OpenProcess
OpenProcess.argtypes = w.DWORD,w.BOOL,w.DWORD
OpenProcess.restype = w.HANDLE
ReadProcessMemory = k32.ReadProcessMemory
ReadProcessMemory.argtypes = w.HANDLE,w.LPCVOID,w.LPVOID,c.c_size_t,c.POINTER(c.c_size_t)
ReadProcessMemory.restype = w.BOOL
CloseHandle = k32.CloseHandle
CloseHandle.argtypes = [w.HANDLE]
CloseHandle.restype = w.BOOL
processHandle = OpenProcess(0x10, False, pid)
# POINTER: 0x00007FF6D4CA0000 + 0x02172560
while 1:
addr = 0x00007FF6D4CA0000
data = c.c_ulonglong()
bytesRead = c.c_ulonglong()
result = ReadProcessMemory(processHandle, c.c_void_p(addr), c.byref(data), c.sizeof(data),
c.byref(bytesRead))
e = c.get_last_error()
print('result: {}, err code: {}, bytesRead: {}'.format(result,e,bytesRead.value))
print('data: {:016X}h'.format(data.value))
CloseHandle(processHandle)
Not wx, gtk3, pyqt etc...
I need something like:
cef.Initialize(settings=settings)
window_info = cef.WindowInfo()
browser = cef.CreateBrowserSync(url="localhost:8080/", window_title="Hello World!" icon="myicon.png")
On Linux execute xseticon program programmtically using os.system() function or similar, see: http://www.leonerd.org.uk/code/xseticon/ .
On Windows use ctypes built-in Python module to execute native win32 functions. Example code below. The _hWnd variable holds window handle which can be obtained by calling browser.GetWindowHandle().
from ctypes import *
from ctypes.wintypes import *
from os import path
import platform
LRESULT = c_int64 if platform.architecture()[0] == "64bit" else c_long
SendMessage = windll.user32.SendMessageW
SendMessage.restype = LRESULT
SendMessage.argtypes = [HWND, UINT, WPARAM, LPARAM]
GetModuleHandle = windll.kernel32.GetModuleHandleW
GetModuleHandle.restype = HMODULE
GetModuleHandle.argtypes = [LPCWSTR]
IMAGE_ICON = 1
LR_LOADFROMFILE = 0x00000010
LR_CREATEDIBSECTION = 0x00002000
LoadImage = windll.user32.LoadImageW
LoadImage.restype = HANDLE
LoadImage.argtypes = [HINSTANCE, LPCWSTR, UINT, c_int, c_int, UINT]
RelPath = lambda file : path.join(path.dirname(path.abspath(__file__)), file)
def AlterIcon(_hWnd, lpszIcon):
WM_SETICON = 0x0080
ICON_BIG = 1
hModel = GetModuleHandle(None)
hIcon = LoadImage(hModel,
RelPath(lpszIcon),
IMAGE_ICON,
0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION)
SendMessage(_hWnd, WM_SETICON, ICON_BIG, hIcon)
Ref: http://qaru.site/questions/7837596/how-to-include-image-in-message-box-using-ctypes-in-python
I am trying to capture the screen using only the ctypes modules. Unfortunately it ends on a segmentation fault. Argtypes and restypes are set correctly, I think. This is the code which crashes:
#!/usr/bin/env python
# coding: utf-8
from sys import maxsize
from ctypes import POINTER, Structure, c_double, byref, c_int32, c_uint32, c_float, cdll
from ctypes.util import find_library
# For tests only
from Quartz import CGDisplayBounds
CGFloat = c_double if maxsize > 2 ** 32 else c_float
class CGPoint(Structure):
_fields_ = [('x', CGFloat), ('y', CGFloat)]
class CGSize(Structure):
_fields_ = [('width', CGFloat), ('height', CGFloat)]
class CGRect(Structure):
_fields_ = [('origin', CGPoint), ('size', CGSize)]
def __repr__(self):
''' With or without this method, segfault. '''
ret = (self.origin.x, self.origin.y, self.size.width, self.size.height)
return ret.__repr__()
# Library
cgs = cdll.LoadLibrary(find_library('CoreGraphics'))
# Argtypes
cgs.CGGetActiveDisplayList.argtypes = \
[c_uint32, POINTER(c_uint32), POINTER(c_uint32)]
cgs.CGDisplayBounds.argtypes = [c_uint32]
# Restypes
cgs.CGGetActiveDisplayList.restypes = c_int32
cgs.CGDisplayBounds.restypes = CGRect
# Monitors
max_displays = 32
display_count = c_uint32(0)
active_displays = (c_uint32 * max_displays)()
cgs.CGGetActiveDisplayList(max_displays, active_displays, byref(display_count))
for idx in range(display_count.value):
display = active_displays[idx]
# This line works
print(CGDisplayBounds(display))
# SEGFAULT HERE!!!!
rect = cgs.CGDisplayBounds(display)
print(rect)
MacOS X version 10.11.3.
Python versions 2.7.10 and 2.6.9.
... Sorry, the error is when setting restype. There is a trailing "s".
The line :
cgs.CGDisplayBounds.restypes = CGRect
Should be:
cgs.CGDisplayBounds.restype = CGRect
How can i implement this function using python ctypes
extern int __stdcall GetRate(HANDLE hDev, int* pData)
How to set datatypes so that i can print pData value
If you want to call a function named GetRate, you can do it as:
from ctypes import *
from ctypes.wintypes import *
GetRate = windll.YOURLIB.GetRate
GetRate.restype = c_int
GetRate.argtypes = [HANDLE, POINTER(c_int)]
# now call GetRate as something like:
#
# hDev = ... # handle
# Data = c_int()
#
# GetRate(hDev, byref(Data)) # GetRate(hDev, &Data)
# print Data
but if you try to declare a callback, function pointer, you can do it as (I think you're looking for the first):
from ctypes import *
from ctypes.wintypes import *
def GetRate(hDev, pDate):
# Your implementation
return 0
# you'll need GETRATE to pass it in the argtypes to the target function
GETRATE = WINFUNCTYPE(c_int, HANDLE, POINTER(c_int))
pGetRate = GETRATE(GetRate)
# now you can pass pGetRate as a callback to another function
I've been trying to use the digi Advanced Device Discovery protocol library with python using ctypes.
the context:
Windows 7 x64
python 2.7.5
dll library
here's my current code:
guid = (0xbf6db409,0xc83d,0x44a3,0xa3,0x6d,0x21,0x79,0x7d,0x2f,0x73,0xf9)
class ADDP():
from ctypes import Structure
class GUID(Structure):
from ctypes.wintypes import DWORD,WORD,BYTE
_fields_ = [("Data1",DWORD),
("Data2",WORD),
("Data3",WORD),
("Data4",BYTE * 8)]
def __init__(self, guid):
from ctypes import windll, c_void_p, c_byte, pointer,c_char,POINTER
from ctypes.wintypes import HANDLE
import ctypes
self.dll = windll.LoadLibrary("D:\\Lib\\addp.dll")
self.guid = self.GUID()
self.guid.Data1 = guid[0]
self.guid.Data2 = guid[1]
self.guid.Data3 = guid[2]
self.guid.Data4 = (c_byte * 8)(guid[3],guid[4],guid[5],guid[6],guid[7],guid[8],guid[9],guid[10])
addpopen = self.dll[1]
addpopen.argtypes = [POINTER(self.GUID),]
addpopen.restype = c_void_p
#print addpopen.restype
self.handler = addpopen(pointer(self.guid))
if self.handler == None:
raise RuntimeError()
self.opened = False
else:
self.opened = True
def isOpen(self):
return self.opened
def Discover(self):
from ctypes import c_int
srch = self.dll[6]
srch.restype = c_int
print srch(self.handler,10,10)
def Close(self):
close = self.dll[3]
close.restype = None
self.opened = False
#print close(self.handler)
conn = ADDP(guid)
#print conn.handler
conn.Discover()
#conn.Close()
print conn.handler
i searched a lot for how to handle a handle returned from a c function, but couldn't find much about it, i read the ctypes docs for a while, and then inspected the header file too..
the handle is defined in the header file with
typedef void* addp_handle_t;
so i assumed i had to set 'restype' to 'c_void_p', the function always returns 'None'
its specified in the header file that it returns 'None' when an error has occurred, else it return the handle to ADDP session.
another thing, this dll does not export functions by name... i had to, more or less, guess what function is what by expected bytes in arguments.
any ideas on this?
i've found a project on google code but apparently it didn't go far...
if you need any other details, just say