Python GetResultFileNameW Help

General discussion related to "Everything".
Post Reply
lauinbeta
Posts: 3
Joined: Fri Jun 11, 2021 2:01 pm

Python GetResultFileNameW Help

Post by lauinbeta » Fri Jun 11, 2021 2:33 pm

Hello, I am just an amateur python programmer. I have been modifying the example python code found here https://www.voidtools.com/support/every ... dk/python/ to just display the Filename of the results but have been running into errors.

I only want to see the filename so I changed this code block like so:

Code: Select all

#show results
for i in range(num_results):
    
    filename = everything_dll.Everything_GetResultFileNameW(i) 
    print(ctypes.wstring_at(filename))
From what I understand from the sdk documentation, GetResultFileNameW returns a c_wchar_p? However from other error messages, I think what it actually returns is an int. I think this is the problem but I'm not sure how to fix it.

This is the error I get:

Code: Select all

Traceback (most recent call last):
  File "C:\Users\Vincent Lau\Documents\Valve Search\test.py", line 70, in <module>
    print(ctypes.wstring_at(filename))
  File "C:\Users\Vincent Lau\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 529, in wstring_at
    return _wstring_at(ptr, size)
OSError: exception: access violation reading 0x000000001B2ED620
I don't really know any C(++) so I'm at a loss as to what to do.

void
Site Admin
Posts: 7142
Joined: Fri Oct 16, 2009 11:31 pm

Re: Python GetResultFileNameW Help

Post by void » Sat Jun 12, 2021 12:17 am

The following worked for me:

Code: Select all

everything_dll.Everything_GetResultFileNameW.argtypes = [ctypes.c_int]

#show results
for i in range(num_results):

	basename = everything_dll.Everything_GetResultFileNameW(i)
	everything_dll.Everything_GetResultDateModified(i,date_modified_filetime)
	everything_dll.Everything_GetResultSize(i,file_size)
	print("Filename: {}\nDate Modified: {}\nSize: {} bytes\n".format(ctypes.wstring_at(basename),get_time(date_modified_filetime),file_size.value))
Tested with Python 3.7 x64

Everything_GetResultFileName

Full code:

Code: Select all

import ctypes
import datetime
import struct

#defines
EVERYTHING_REQUEST_FILE_NAME = 0x00000001
EVERYTHING_REQUEST_PATH = 0x00000002
EVERYTHING_REQUEST_FULL_PATH_AND_FILE_NAME = 0x00000004
EVERYTHING_REQUEST_EXTENSION = 0x00000008
EVERYTHING_REQUEST_SIZE = 0x00000010
EVERYTHING_REQUEST_DATE_CREATED = 0x00000020
EVERYTHING_REQUEST_DATE_MODIFIED = 0x00000040
EVERYTHING_REQUEST_DATE_ACCESSED = 0x00000080
EVERYTHING_REQUEST_ATTRIBUTES = 0x00000100
EVERYTHING_REQUEST_FILE_LIST_FILE_NAME = 0x00000200
EVERYTHING_REQUEST_RUN_COUNT = 0x00000400
EVERYTHING_REQUEST_DATE_RUN = 0x00000800
EVERYTHING_REQUEST_DATE_RECENTLY_CHANGED = 0x00001000
EVERYTHING_REQUEST_HIGHLIGHTED_FILE_NAME = 0x00002000
EVERYTHING_REQUEST_HIGHLIGHTED_PATH = 0x00004000
EVERYTHING_REQUEST_HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000

#dll imports
everything_dll = ctypes.WinDLL ("C:\\Users\\python\\Desktop\\dll\\Everything64.dll")
everything_dll.Everything_GetResultDateModified.argtypes = [ctypes.c_int,ctypes.POINTER(ctypes.c_ulonglong)]
everything_dll.Everything_GetResultSize.argtypes = [ctypes.c_int,ctypes.POINTER(ctypes.c_ulonglong)]
everything_dll.Everything_GetResultFileNameW.argtypes = [ctypes.c_int]
everything_dll.Everything_GetResultFileNameW.restype = ctypes.c_wchar_p

#setup search
everything_dll.Everything_SetSearchW("test.py")
everything_dll.Everything_SetRequestFlags(EVERYTHING_REQUEST_FILE_NAME | EVERYTHING_REQUEST_PATH | EVERYTHING_REQUEST_SIZE | EVERYTHING_REQUEST_DATE_MODIFIED)

#execute the query
everything_dll.Everything_QueryW(1)

#get the number of results
num_results = everything_dll.Everything_GetNumResults()

#show the number of results
print("Result Count: {}".format(num_results))

#convert a windows FILETIME to a python datetime
#https://stackoverflow.com/questions/39481221/convert-datetime-back-to-windows-64-bit-filetime
WINDOWS_TICKS = int(1/10**-7)  # 10,000,000 (100 nanoseconds or .1 microseconds)
WINDOWS_EPOCH = datetime.datetime.strptime('1601-01-01 00:00:00',
                                           '%Y-%m-%d %H:%M:%S')
POSIX_EPOCH = datetime.datetime.strptime('1970-01-01 00:00:00',
                                         '%Y-%m-%d %H:%M:%S')
EPOCH_DIFF = (POSIX_EPOCH - WINDOWS_EPOCH).total_seconds()  # 11644473600.0
WINDOWS_TICKS_TO_POSIX_EPOCH = EPOCH_DIFF * WINDOWS_TICKS  # 116444736000000000.0

def get_time(filetime):
    """Convert windows filetime winticks to python datetime.datetime."""
    winticks = struct.unpack('<Q', filetime)[0]
    microsecs = (winticks - WINDOWS_TICKS_TO_POSIX_EPOCH) / WINDOWS_TICKS
    return datetime.datetime.fromtimestamp(microsecs)

#create buffers
basename = ctypes.create_unicode_buffer(260)
date_modified_filetime = ctypes.c_ulonglong(1)
file_size = ctypes.c_ulonglong(1)

#show results
for i in range(num_results):

	basename = everything_dll.Everything_GetResultFileNameW(i)
	everything_dll.Everything_GetResultDateModified(i,date_modified_filetime)
	everything_dll.Everything_GetResultSize(i,file_size)
	print("Filename: {}\nDate Modified: {}\nSize: {} bytes\n".format(ctypes.wstring_at(basename),get_time(date_modified_filetime),file_size.value))
Last edited by void on Tue Jun 15, 2021 12:44 pm, edited 2 times in total.
Reason: added everything_dll.Everything_GetResultFileNameW.restype = ctypes.c_wchar_p

lauinbeta
Posts: 3
Joined: Fri Jun 11, 2021 2:01 pm

Re: Python GetResultFileNameW Help

Post by lauinbeta » Mon Jun 14, 2021 1:40 pm

void,

thanks for your response. What does this line do? Is it like staticly typing the output of GetResultFileNameW?

Code: Select all

everything_dll.Everything_GetResultFileNameW.argtypes = [ctypes.c_int]

I tried copy pasting your code and unfortunately, I still get the same error.

Code: Select all

PS C:\Users\Vincent Lau\Documents\Valve Search> python voids-code.py
Result Count: 33
Traceback (most recent call last):
  File "C:\Users\Vincent Lau\Documents\Valve Search\voids-code.py", line 69, in <module>
    print("Filename: {}\nDate Modified: {}\nSize: {} bytes\n".format(ctypes.wstring_at(basename),get_time(date_modified_filetime),file_size.value))
  File "C:\Users\Vincent Lau\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 529, in wstring_at
    return _wstring_at(ptr, size)
OSError: exception: access violation reading 0xFFFFFFFF9463BD40
PS C:\Users\Vincent Lau\Documents\Valve Search> 
I'm running python 3.9.5, but I tried uninstalling and running it on python 3.7.9. I also thought that maybe Everything64.dll being in the root of my C drive might be the problem so tried moving it to my user folder instead. Both of those still resulted in the same OSError. Any other thoughts as to what it could be?

void
Site Admin
Posts: 7142
Joined: Fri Oct 16, 2009 11:31 pm

Re: Python GetResultFileNameW Help

Post by void » Tue Jun 15, 2021 12:28 pm

thanks for your response. What does this line do? Is it like staticly typing the output of GetResultFileNameW?
everything_dll.Everything_GetResultFileNameW.argtypes = [ctypes.c_int]
This line declares the parameters to Everything_GetResultFileNameW, which takes a DWORD to specify the result index.

Please try the following delcaration:

everything_dll.Everything_GetResultFileNameW.argtypes = [ctypes.c_int]
everything_dll.Everything_GetResultFileNameW.restype = ctypes.c_wchar_p

The crash has something to do with python treating the return value as an int, when it should be a pointer.

lauinbeta
Posts: 3
Joined: Fri Jun 11, 2021 2:01 pm

Re: Python GetResultFileNameW Help

Post by lauinbeta » Tue Jun 15, 2021 1:15 pm

Code: Select all

everything_dll.Everything_GetResultFileNameW.restype = ctypes.c_wchar_p
This worked! Thank you so much!

Post Reply