help with debugging a ctypes problem

Discussion in 'Python' started by gap, Sep 23, 2006.

  1. gap

    gap Guest

    I'm no c programmer, and I'm a ctypes newbie. I'll frame my problem as
    simply as I can. Sorry if it's too much or not enough info. I don't
    expect an explicit answer (but maybe), just help figuring out how to debug.

    WinXP, python 2.4.2

    I'm using ctypes to access functions in a commercial dll. A certain
    function takes five arguments, foo(a, b, c, d, e). The last argument,
    e, is an integer that serves as a bit coded options flag. Zero means no
    option, 1+2+4 means the first three options combined, etc.

    I can call foo() successfully if e=c_int(0). However, if e=3, the
    function does not work. Execution of the python program continues after
    the call, no errors are raised, but foo() has not done its thing.

    If I now call bar(x, y, z) after foo(), two things happen.
    1. The dll raises an error reporting that argument x is invalid
    2. Python reports that too many bytes (4) have been passed to bar()

    I've checked the types of all the variables a million times, and they
    are correct according to the docs. The vendor sent me a c program that
    they claim works. It looks like a translation of mine. I can't run
    theirs, because I can't figure out how to compile it (I'm don't do c; I
    do have MinGW; the code was designed for MSVC; I'm lost).

    I'm suspecting that foo() screws up a stack somewhere. That might
    explain why bar() chokes on the *first* parameter, while it thinks that
    the number of arguments is wrong when it gets to the *last*.

    How can I debug this? Can I view the appropriate stack? What/where
    would I find it? Any other ideas or advice? etc?

    thanks,
    -gary
     
    gap, Sep 23, 2006
    #1
    1. Advertising

  2. gap

    Guest

    > help figuring out how to debug ... ctypes ...
    > a commercial dll. A certain function takes five arguments, foo(a, b, c, d, e).
    > Can I view the appropriate stack? ... Any other ideas or advice? etc?


    Did you call the foo of _cdecl ctypes.cdll or the foo of _stdcall =
    ctypes.windll?

    What is the sizeof each argument type? Preferably fetched by compiling
    some C to printf them?

    Would it help to call C in another Dll to return the stack pointer, so
    Python could print the stack?
     
    , Sep 23, 2006
    #2
    1. Advertising

  3. gap

    gap Guest

    wrote:
    >> help figuring out how to debug ... ctypes ...
    >> a commercial dll. A certain function takes five arguments, foo(a, b, c, d, e).
    >> Can I view the appropriate stack? ... Any other ideas or advice? etc?

    >
    > Did you call the foo of _cdecl ctypes.cdll or the foo of _stdcall =
    > ctypes.windll?
    >
    > What is the sizeof each argument type? Preferably fetched by compiling
    > some C to printf them?
    >
    > Would it help to call C in another Dll to return the stack pointer, so
    > Python could print the stack?
    >


    Uh... windll, I think. I'm really green here.

    But there is news: If simply leave off the last argument of foo() and
    bar(), each works as expected. That is, I call foo() with four instead
    of the five that the docs and the c example call for, and I call bar()
    with one argument instead of the two that are called for. Everything
    works, but you gotta wonder.

    Meanwhile, I upgraded from ctypes 0.9.6 to 1.0.0, but I don't think
    that that changed anything.

    I'll see if I can implement your suggestions, and if I manage it, I'll
    report back.

    thanks,
    -g
     
    gap, Sep 24, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johann Blake
    Replies:
    1
    Views:
    1,240
    Hermit Dave
    Jan 5, 2004
  2. Henk Punt
    Replies:
    0
    Views:
    424
    Henk Punt
    Jul 23, 2004
  3. jacob navia

    Debugging C vs debugging C++

    jacob navia, Oct 26, 2006, in forum: C Programming
    Replies:
    11
    Views:
    623
    Ian Collins
    Oct 27, 2006
  4. Replies:
    2
    Views:
    329
  5. Replies:
    0
    Views:
    538
Loading...

Share This Page