Problem with calling function from dll

Discussion in 'Python' started by deepblu@poczta.fm, Dec 13, 2012.

  1. Guest

    I have problem with using function from dll.

    import ctypes

    b = ctypes.windll.LoadLibrary("kernel32")

    a = ""

    b.GetComputerNameA(a,20)


    But I got exception:

    Traceback (most recent call last):
    File "<pyshell#323>", line 1, in <module>
    b.GetComputerNameA(a,20)
    WindowsError: exception: access violation reading 0x00000014

    Even when I write:
    a = ctypes.c_char_p('.' * 20)
    I got the same result.

    Here I found this function description:
    http://sd2cx1.webring.org/l/rd?ring...ty.net/~azakrze3/html/win32_api_functios.html


    Please help.
     
    , Dec 13, 2012
    #1
    1. Advertising

  2. Am 13.12.2012 08:40, schrieb :
    > I have problem with using function from dll.
    >
    > import ctypes
    >
    > b = ctypes.windll.LoadLibrary("kernel32")
    >
    > a = ""
    >
    > b.GetComputerNameA(a,20)


    GetComputerNameA takes a pointer to a writable char string. You give it
    a pointer to an immutable string. You will have to create a buffer first
    and pass that to the function. Also, I would use GetComputerNameW
    instead, although it makes little difference for this name (provided
    that's the hostname, which may only contain an ASCII subset).


    > But I got exception:
    >
    > Traceback (most recent call last):
    > File "<pyshell#323>", line 1, in <module>
    > b.GetComputerNameA(a,20)
    > WindowsError: exception: access violation reading 0x00000014


    Here is something else that is wrong: The address 0x00000014 that the
    function tries to access is neither the address of a mutable nor an
    immutable string but simply the value 20 that you passed as second
    parameter. In other words, the way that the parameters are passed to the
    function is wrong.


    > Even when I write:
    > a = ctypes.c_char_p('.' * 20)
    > I got the same result.


    This looks much better than passing the string above, but it still seems
    the (hopefully correct) parameters are passed wrongly.

    > Here I found this function description:
    > http://sd2cx1.webring.org/l/rd?ring...ty.net/~azakrze3/html/win32_api_functios.html


    Use the primary source for such info:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85).aspx

    One possibly important point there is the WINAPI part, which describes
    how parameters are passed to and from the function, which might be the
    only cause that this doesn't work. However, instead of try-and-error,
    rather go to the documentation of the ctypes API and search for WINAPI.
    There, you will find an example that uses a function from the win32 API,
    too. A bit further down, you will also find a create_string_buffer()
    function that could be useful for you.

    http://docs.python.org/2/library/ctypes.html


    Greetings!

    Uli
     
    Ulrich Eckhardt, Dec 13, 2012
    #2
    1. Advertising

  3. Guest

    sb = ctypes.create_string_buffer(20)
    i = ctypes.byref(ctypes.c_int(20))
    w = b.GetComputerNameA(sb,i)

    w -> 1
    sb.value -> nazwa komputera

    :)
     
    , Dec 13, 2012
    #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. Anushi
    Replies:
    5
    Views:
    9,014
  2. Andreas Lagemann
    Replies:
    8
    Views:
    540
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    462
  4. Capsule

    calling c# dll from c++ dll

    Capsule, Oct 18, 2005, in forum: C++
    Replies:
    3
    Views:
    995
    Victor Bazarov
    Oct 18, 2005
  5. H. Simpson
    Replies:
    4
    Views:
    348
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page