Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:03:17+00:00 2026-05-23T07:03:17+00:00

I’m trying to use Python’s ctypes to work with a DLL, but I occasionally

  • 0

I’m trying to use Python’s ctypes to work with a DLL, but I occasionally run into a problem when I try to call a function that’s passed as a pointer to another function.

A little background…I’m trying to build a userspace filesystem using Dokan (version 0.6.0). Somewhat loosely stated, Dokan is basically FUSE for Windows. I’ve wrapped the dokan header file using ctypes (similar to pydokan). That header file contains the definition for a function pointer that looks like this

typedef int (WINAPI *PFillFindData) (PWIN32_FIND_DATAW, PDOKAN_FILE_INFO);

It also contains the prototype of another function

int (DOKAN_CALLBACK *FindFilesWithPattern) (
     LPCWSTR,
     LPCWSTR,
     PFillFindData,
     PDOKAN_FILE_INFO);

The corresponding ctypes definitions look like this

PFillFindData = ctypes.WINFUNCTYPE(ctypes.c_int,
                                   PWIN32_FIND_DATAW, 
                                   PDOKAN_FILE_INFO)

FindFilesWithPattern = ctypes.WINFUNCTYPE(ctypes.c_int,
                                          ctypes.c_wchar_p,
                                          ctypes.c_wchar_p,
                                          PFillFindData,
                                          PDOKAN_FILE_INFO)

The implementation of this latter function (FindFilesWithPattern) must call the FillFindData function that it is passed. A basic implemenation looks like this

def FindFilesWithPattern(self,
                         FileName,
                         SearchPattern,
                         FillFindData,
                         DokanFileInfo):
    if FileName == '\\':
        File = WIN32_FIND_DATAW(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY, 
                                FILETIME(1, 1),
                                FILETIME(1, 1), 
                                FILETIME(1, 1), 
                                0, 
                                len(self.HelloWorldText), 
                                0, 
                                0, 
                                'Hello_World.txt',
                                'Hello_~1.txt')
        pFile = PWIN32_FIND_DATAW(File)
        FillFindData(pFile, DokanFileInfo)
        return 0
    else:
        return -ERROR_FILE_NOT_FOUND

When this function gets called, I occasionally get the following error:

Traceback (most recent call last):
    File "_ctypes/callbacks.c", line 313, in 'calling callback function'
    File "src/test.py", line 385, in FindFilesWithPattern
        FillFindData(pFile, DokanFileInfo)
WindowsError: exception: access violation reading 0x0000000000000008

I am vexed. At first glance this looks like I’m trying to access memory that’s out of range. But this error only occurs occasionally. Sometimes everything works fine, and the results come back as expected. (To clarify, when I say occasionally, I mean that the error occurs on some runs of the program and not on others. The error seems to occur or not occur consistently within a single run.)

I wondered then if perhaps I was getting an error code back rather than a memory address. I found here that if this was an error code, then it might indicate “not enough memory”. When I look at the system monitor, this doesn’t seem to be a problem though. I’ve tried running various memory profilers like Heapy and Meliae, but neither of them seem to work with Python 2.7 on Windows 64-bit.

My next best guess was that this is an issue using a 64-bit OS. Perhaps the type being used for the function pointer isn’t sufficient to properly address it. After doing some Googling, it seems that others have had issues with using ctypes in Win64. I’ve built my Dokan library for a 64-bit architecture. Is there a problem with my python code then?

Any help would be very much appreciated. I’ve struggled with this for some time now.

A similar post can be found here. It doesn’t seem terribly analogous though.

Note: In the python code you’ll see some types that aren’t defined here (e.g. PDOKAN_FILE_INFO). Those are either structures or pointers to structures that I didn’t include for the sake of brevity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:03:18+00:00Added an answer on May 23, 2026 at 7:03 am

    How do you instantiate and use your callback? A callback must remain in scope for the lifetime it may be called. See this answer. These


    Edit

    Clarifying what I think the issue is…

    I see you have to implement a DOKAN_OPERATIONS structure that implements callbacks like FindFilesWithPattern, then call DokanMain with that structure to mount the filesystem. When you fill out the structure you should be creating a type for the callback, then instantiating the callback with a Python function. Something like (pseudocode):

    #Create the callback pointer type
    PFINDFILESWITHPATTERN = ctypes.WINFUNCTYPE(ctypes.c_int,
                                              ctypes.c_wchar_p,
                                              ctypes.c_wchar_p,
                                              PFillFindData,
                                              PDOKAN_FILE_INFO) 
    
    # Implement in Python
    def FindFilesWithPatternImpl(...):
        # implementation
    
    # Create a callback pointer object
    FindFilesWithPattern = PFINDFILESIWTHPATTERN(FindFilesWithPatternImpl)
    
    # Create the required structure listing the callback
    dokan_op = DOKAN_OPERATIONS(...,FindFilesWithPattern,...)
    
    # Register the callbacks
    DokanMain(byref(dokan_op))
    

    You must keep a reference to object dokan_op for the lifetime the callback could be used. If you implement mounting Dokan similar to below:

    def mount():
        # Create structure locally
        dokan_op = DOKAN_OPERATIONS(...)
        # Spin off thread to mount Dokan
        threading.Thread(DokanMain,args=(byref(dokan_op),))
    
    mount()
    

    Once mount() returns dokan_op will be destructed. This is the scenario described by my original answer and would cause the error you are seeing. I’m theorizing your problem since I don’t know what the rest of the code looks like, but I think the way you’ve implemented FindFilesWithPattern in Python is correct, esp. since you say it works intermittently. I’ve had the failure you’re seeing before and the above scenario or something like it will cause the error you’re seeing.

    Hope this helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.