how to get data out of voidptr with Python 3.6.3 - python-3.x

Hello my Problem is to get my data out of an sip.voidptr.
Before I used Python 2.7 and this code:
def winEvent(self, message):
WM_COPYDATA = 0x004A
if message.message == WM_COPYDATA:
if sys.version.find("64 bit") != -1:
structure = ctypes.string_at( message.lParam, 24 )
data = struct.unpack("QQP", structure)
unused, str_len, str_adr = data
my_string = ctypes.string_at( str_adr, str_len )
Now I use Python 3 and had changed from "winevent" to "event_Type".
Does anybody have an idea or an example how to get the data out of the sip.voidptr?
The error massage i get is:
File "C:\OSGEO4~1\apps\Python36\lib\ctypes\__init__.py", line 488, in string_at
return _string_at(ptr, size)
OSError: exception: access violation reading 0x0000000000000001
and before I get as my_string:
Name ID
1 1_2
2 1_3
3 1_4
4 1_5

Related

Choose encoding when converting to Sqlite database

I am converting Mbox files to Sqlite db. I do not arrive to encode the db file into utf-8.
The Python console displays the following message when converting to db:
Error binding parameter 1 - probably unsupported type.
When I visualize my data on DB Browser for SQlite, special characters don't appear and the � symbol shows up instead.
I first convert .text files to Mbox files with the following function:
def makeMBox(fIn,fOut):
if not os.path.exists(fIn):
return False
if os.path.exists(fOut):
return False
out = open(fOut,"w")
lineNum = 0
# detect encoding
readsource = open(fIn,'rt').__next__
#fInCodec = tokenize.detect_encoding(readsource)[0]
fInCodec = 'UTF-8'
for line in open(fIn,'rt', encoding=fInCodec, errors="replace"):
if line.find("From ") == 0:
if lineNum != 0:
out.write("\n")
lineNum +=1
line = line.replace(" at ", "#")
out.write(line)
out.close()
return True
Then, I convert to sqlite db:
for k in dates:
db = sqlite_utils.Database("Courriels_Sqlite/Echanges_Discussion.db")
mbox = mailbox.mbox("Courriels_MBox/"+k+".mbox")
def to_insert():
for message in mbox.values():
Dionyversite = dict(message.items())
Dionyversite["payload"] = message.get_payload()
yield Dionyversite
try:
db["Dionyversite"].upsert_all(to_insert(), alter = True, pk = "Message-ID")
except sql.InterfaceError as e:
print(e)
Thank you for your help.
I found how to fix it:
def to_insert():
for message in mbox.values():
Dionyversite = dict(message.items())
Dionyversite["payload"] = message.get_payload(decode = True)
yield Dionyversite
``
As you can see, I add `decode = True` inside `get_payload`of the `to_insert`function.

Python ctypes EnumThreadWindows failing with error 87 (ERROR_INVALID_PARAMETER)

I can't seem to get EnumThreadWindows to work. It keeps failing with error 87. Code:
error = ctypes.WinDLL('Kernel32').GetLastError
enum_func = ctypes.WINFUNCTYPE(wintypes.BOOL,
wintypes.HWND,
wintypes.LPARAM)
def callback(hwnd, lParam):
length = ctypes.WinDLL('User32').GetWindowTextLengthW(hwnd) + 1
buf = ctypes.create_unicode_buffer(length)
ctypes.WinDLL('User32').GetWindowTextW(hwnd, buf, length)
print(buf.value)
worker = enum_func(callback)
test = ctypes.WinDLL('User32').EnumThreadWindows(6000, worker, None)
print(error(test))
I've tried pid = wintypes.DWORD(6000), test = ctypes.WinDLL('User32').EnumThreadWindows(pid.value, worker, None) to no avail.
What am I doing wrong?
Here's working code. Make sure to pass a valid thread ID.
You might be interested in the fact that an LPARAM can be anything, including a python object, so if you pass a Python object it can be manipulated in the callback:
import ctypes
from ctypes import wintypes
from collections import namedtuple
Window = namedtuple('Window','hwnd title')
WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL,
wintypes.HWND,
ctypes.py_object) # to allow any Python object.
u32 = ctypes.WinDLL('user32',use_last_error=True) # to ensure GetLastError was captured
# Declaring arguments and return type helps catch errors and support 64-bit.
# HWND is 64-bit on 64-bit Python, and could get truncated if left to ctypes default
# of c_int (32-bit). This code works on Python 2.7 and 3.9.
u32.GetWindowTextLengthW.argtypes = wintypes.HWND,
u32.GetWindowTextLengthW.restype = ctypes.c_int
u32.GetWindowTextW.argtypes = wintypes.HWND,wintypes.LPWSTR,ctypes.c_int
u32.GetWindowTextW.restype = ctypes.c_int
u32.EnumThreadWindows.argtypes = wintypes.DWORD,WNDENUMPROC,ctypes.py_object # to pass Python object
u32.EnumThreadWindows.restype = wintypes.BOOL
#WNDENUMPROC # decorator makes this a ctypes-compatible function
def callback(hwnd, lParam):
length = u32.GetWindowTextLengthW(hwnd) + 1
buf = ctypes.create_unicode_buffer(length)
u32.GetWindowTextW(hwnd, buf, length)
lParam.append(Window(hwnd,buf.value)) # append data to the callback parameter
return True # return True to continue enumeration
result = [] # A python object
if u32.EnumThreadWindows(6332, callback, result): # 6332 was a thread in my explore.exe
for wnd in result: # list results when enumeration complete
print(wnd)
else:
print('error:',ctypes.get_last_error()) # print error of EnumThreadWindows.
# Python could use a Win32 function that fails in
# between the ctypes call and calling GetLastError
# directly.
Output:
Window(hwnd=65832, title='')
Window(hwnd=65838, title='')
Window(hwnd=131174, title='')
Window(hwnd=65682, title='')
Window(hwnd=65678, title='')
Window(hwnd=65826, title='Program Manager')
Window(hwnd=196928, title='MSCTFIME UI')
Window(hwnd=65680, title='Default IME')

md5(str.encode(var1)).hexdigest() giving hex value as 382fbe213f159eecf85facb256f265d0 - how to know the var1?

While running the test case, my code is failing as the expected value in hex is different then my answer.
for example, my ws_std value is 13.06 i.e. var1
md5(str.encode(var1)).hexdigest() giving hex value as 382fbe213f159eecf85facb256f265d0
But I am not sure if it matches with the hex value.
Getting error in below code :-
variables = ["ws_std", "p_range", "corr", "dew_month", "max_gust_month", "max_gust_value", "avg_temp", "temp_range", "max_p_range_day", "num_days_std", "median_b_days"]
answers = [ws_std, p_range, corr, dew_month, max_gust_month, max_gust_value, avg_temp, temp_range, max_p_range_day, num_days_std, median_b_days]
answer_dict = dict()
for var, ans in zip(variables, answers):
answer_dict[var] = md5(str.encode(ans)).hexdigest()
with open('test_files/hash.pk', 'rb') as file:
hash_dict = pickle.load(file)
def test_ws_std():
assert hash_dict["ws_std"] == answer_dict["ws_std"]
Error Code:-
========================================================== FAILURES ==========================================================
________________________________________________________ test_ws_std _________________________________________________________
def test_ws_std():
> assert hash_dict["ws_std"] == answer_dict["ws_std"]
E AssertionError: assert 'c8cc550afa85...2c6946c238f36' == '382fbe213f159...facb256f265d0'
E - c8cc550afa85496c4ee2c6946c238f36
E + 382fbe213f159eecf85facb256f265d0
test.py:40: AssertionError
see comments below .
output required : ws_std

converting python 2 to python 3('PROCESS_ALL_ACCESS' is not defined)

Here is my code that isn't working.
I do not know how to define it?
('PROCESS_ALL_ACCESS' is not defined)
import sys
from ctypes import *
PAGE_READWRITE = 0x04
PROCESS_ALL_ACCES = ( 0x000F0000 | 0x00100000 | 0xFFF )
VIRTUAL_MEM = ( 0x1000 | 0x2000 )
kernel32 = windll.kernel32
pid = sys.argv[0]
dll_path = sys.argv[1:]
dll_len = len(dll_path)
# Get a handle to the process we are injecting into.
h_process = kernel32.OpenProcess( PROCESS_ALL_ACCESS, False, int(pid) )
if not h_process:
print("[*] Couldn't acquire a handle to PID: %s") % pid
sys.exit(0)
# Allocate some space for the DLL path
arg_address = kernel32.VirtualAllocEx(h_process, 0, dll_len, VIRTUAL_MEM,
PAGE_READWRITE)
# Write the DLL path into the allocated space
written = c_int(0)
kernel32.WriteProcessMemory(h_process, arg_address, dll_path, dll_len,
byref(written))
# We need to resolve the address for LoadLibraryA
h_kernel32 = kernel32.GetModuleHandleA("kernel32.dll")
h_loadlib = kernel32.GetProcAddress(h_kernel32, "LoadLibraryA")
# Now we try to create the remote thread, with the entry point set
# to LoadLibraryA and a pointer to DLL path as its single parameter
thread_id = c_ulong(0)
if not kerel32.CreateRemoteThread(h_process,
None,
0,
h_loadlib,
arg_address,
0,
byref(thread_id)):
print("[*] Failed to inject the DLL. Exiting.")
sys.exit(0)
print("[*] Temote thread with ID 0x%08x created.") % thread_id.value
I Keep on getting this error 'PROCESS_ALL_ACCESS'
in alot of my code i'm converting from "Gray Hat Python"
File "C:\Python\Python36-32\dll_injector.py", line 14, in <module>
h_process = kernel32.OpenProcess( PROCESS_ALL_ACCESS, False, int(pid) )
NameError: name 'PROCESS_ALL_ACCESS' is not defined
I don't know if you're still trying to solve this, but you have a spelling error.
In your 5th line, you have this:
PROCESS_ALL_ACCES = ( 0x000F0000 | 0x00100000 | 0xFFF )
You're missing an S

load .npy file from google cloud storage with tensorflow

i'm trying to load .npy files from my google cloud storage to my model i followed this example here Load numpy array in google-cloud-ml job
but i get this error
'utf-8' codec can't decode byte 0x93 in
position 0: invalid start byte
can you help me please ??
here is sample from the code
Here i read the file
with file_io.FileIO(metadata_filename, 'r') as f:
self._metadata = [line.strip().split('|') for line in f]
and here i start processing on it
if self._offset >= len(self._metadata):
self._offset = 0
random.shuffle(self._metadata)
meta = self._metadata[self._offset]
self._offset += 1
text = meta[3]
if self._cmudict and random.random() < _p_cmudict:
text = ' '.join([self._maybe_get_arpabet(word) for word in text.split(' ')])
input_data = np.asarray(text_to_sequence(text, self._cleaner_names), dtype=np.int32)
f = StringIO(file_io.read_file_to_string(
os.path.join('gs://path',meta[0]))
linear_target = tf.Variable(initial_value=np.load(f), name='linear_target')
s = StringIO(file_io.read_file_to_string(
os.path.join('gs://path',meta[1])))
mel_target = tf.Variable(initial_value=np.load(s), name='mel_target')
return (input_data, mel_target, linear_target, len(linear_target))
and this is a sample from the data sample
This is likely because your file doesn't contain utf-8 encoded text.
Its possible, you may need to initialize the file_io.FileIO instance as a binary file using mode = 'rb', or set binary_mode = True in the call to read_file_to_string.
This will cause data that is read to be returned as a sequence of bytes, rather than a string.

Resources