Having trouble using CTypes with a custom function

Discussion in 'Python' started by tkondal@gmail.com, Oct 4, 2006.

  1. Guest

    Hi all.

    I just started looking at Python's ctypes lib and I am having trouble
    using it for a function.

    For starters, here's my Python code:


    from ctypes import*;
    myStringDLL= cdll.LoadLibrary("myStringDLL.dll");

    GetMyString = getattr(myStringDLL,
    "?GetMyString@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z")

    strString = create_string_buffer('\000' * 256);
    GetMyString.restype = c_int;
    GetMyString.argtypes = [c_char_p];

    bResult = GetMyString (strSerialNumber);

    print (bResult);
    print (strSerialNumber);

    #C++ Prototype of the function I want to call:
    #bool GetMyString (string& stringParam) ;




    I do not have access to the source code of this function so don't ask
    me to try different things in C++. This DLL is working fine.

    The problem that I have is that print (strSerialNumber) does not seem
    to print the correct string. What I get is some garbage value of
    unprintable characters. Am I using this the correct way?

    Thanks.
    , Oct 4, 2006
    #1
    1. Advertising

  2. Chris Mellon Guest

    On 4 Oct 2006 11:18:16 -0700, <> wrote:
    > Hi all.
    >
    > I just started looking at Python's ctypes lib and I am having trouble
    > using it for a function.
    >
    > For starters, here's my Python code:
    >
    >
    > from ctypes import*;
    > myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
    >
    > GetMyString = getattr(myStringDLL,
    > "?GetMyString@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z")
    >
    > strString = create_string_buffer('\000' * 256);
    > GetMyString.restype = c_int;
    > GetMyString.argtypes = [c_char_p];
    >
    > bResult = GetMyString (strSerialNumber);
    >
    > print (bResult);
    > print (strSerialNumber);
    >
    > #C++ Prototype of the function I want to call:
    > #bool GetMyString (string& stringParam) ;
    >
    >
    >
    >
    > I do not have access to the source code of this function so don't ask
    > me to try different things in C++. This DLL is working fine.
    >
    > The problem that I have is that print (strSerialNumber) does not seem
    > to print the correct string. What I get is some garbage value of
    > unprintable characters. Am I using this the correct way?
    >





    This function is expecting a C++ std::string object, not a regular C
    style string. You'll need a wrapper function, and one which uses the
    same compiler and STL as the C++ source.

    > Thanks.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Chris Mellon, Oct 4, 2006
    #2
    1. Advertising

  3. Guest

    Would you have any example of a wrapper for such data types?

    Thanks.

    Chris Mellon wrote:
    > On 4 Oct 2006 11:18:16 -0700, <> wrote:
    > > Hi all.
    > >
    > > I just started looking at Python's ctypes lib and I am having trouble
    > > using it for a function.
    > >
    > > For starters, here's my Python code:
    > >
    > >
    > > from ctypes import*;
    > > myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
    > >
    > > GetMyString = getattr(myStringDLL,
    > > "?GetMyString@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z")
    > >
    > > strString = create_string_buffer('\000' * 256);
    > > GetMyString.restype = c_int;
    > > GetMyString.argtypes = [c_char_p];
    > >
    > > bResult = GetMyString (strSerialNumber);
    > >
    > > print (bResult);
    > > print (strSerialNumber);
    > >
    > > #C++ Prototype of the function I want to call:
    > > #bool GetMyString (string& stringParam) ;
    > >
    > >
    > >
    > >
    > > I do not have access to the source code of this function so don't ask
    > > me to try different things in C++. This DLL is working fine.
    > >
    > > The problem that I have is that print (strSerialNumber) does not seem
    > > to print the correct string. What I get is some garbage value of
    > > unprintable characters. Am I using this the correct way?
    > >

    >
    >
    >
    >
    > This function is expecting a C++ std::string object, not a regular C
    > style string. You'll need a wrapper function, and one which uses the
    > same compiler and STL as the C++ source.
    >
    > > Thanks.
    > >
    > > --
    > > http://mail.python.org/mailman/listinfo/python-list
    > >
    , Oct 4, 2006
    #3
  4. Chris Mellon Guest

    <please don't top post>

    On 4 Oct 2006 11:35:11 -0700, <> wrote:
    > Would you have any example of a wrapper for such data types?
    >
    > Thanks.
    >
    > Chris Mellon wrote:
    > > On 4 Oct 2006 11:18:16 -0700, <> wrote:
    > > > Hi all.
    > > >
    > > > I just started looking at Python's ctypes lib and I am having trouble
    > > > using it for a function.
    > > >
    > > > For starters, here's my Python code:
    > > >
    > > >
    > > > from ctypes import*;
    > > > myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
    > > >
    > > > GetMyString = getattr(myStringDLL,
    > > > "?GetMyString@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z")
    > > >
    > > > strString = create_string_buffer('\000' * 256);
    > > > GetMyString.restype = c_int;
    > > > GetMyString.argtypes = [c_char_p];
    > > >
    > > > bResult = GetMyString (strSerialNumber);
    > > >
    > > > print (bResult);
    > > > print (strSerialNumber);
    > > >
    > > > #C++ Prototype of the function I want to call:
    > > > #bool GetMyString (string& stringParam) ;
    > > >
    > > >
    > > >
    > > >
    > > > I do not have access to the source code of this function so don't ask
    > > > me to try different things in C++. This DLL is working fine.
    > > >
    > > > The problem that I have is that print (strSerialNumber) does not seem
    > > > to print the correct string. What I get is some garbage value of
    > > > unprintable characters. Am I using this the correct way?
    > > >

    > >
    > >
    > >
    > >
    > > This function is expecting a C++ std::string object, not a regular C
    > > style string. You'll need a wrapper function, and one which uses the
    > > same compiler and STL as the C++ source.
    > >
    > > > Thanks.
    > > >
    > > > --
    > > > http://mail.python.org/mailman/listinfo/python-list
    > > >

    >


    in this case it'd be very simple:

    bool WrapGetMyString(char * c) {
    return GetMyString(c)
    }


    (Note: This isn't unicode safe).

    The C++ compiler will handle converting from the char * to the std::string.





    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Chris Mellon, Oct 4, 2006
    #4
  5. Guest

    Hi Chris.

    I know that it is easy to fix the problem using C++. However, I do not
    want to code a wrapper DLL. I was wondering if there was a workaround
    with Python. Everything has to be done in Python as we do not have the
    tools for C++ (and we are not planning on getting any).

    Thanks.

    Chris Mellon wrote:
    > <please don't top post>
    >
    > On 4 Oct 2006 11:35:11 -0700, <> wrote:
    > > Would you have any example of a wrapper for such data types?
    > >
    > > Thanks.
    > >
    > > Chris Mellon wrote:
    > > > On 4 Oct 2006 11:18:16 -0700, <> wrote:
    > > > > Hi all.
    > > > >
    > > > > I just started looking at Python's ctypes lib and I am having trouble
    > > > > using it for a function.
    > > > >
    > > > > For starters, here's my Python code:
    > > > >
    > > > >
    > > > > from ctypes import*;
    > > > > myStringDLL= cdll.LoadLibrary("myStringDLL.dll");
    > > > >
    > > > > GetMyString = getattr(myStringDLL,
    > > > > "?GetMyString@@YA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z")
    > > > >
    > > > > strString = create_string_buffer('\000' * 256);
    > > > > GetMyString.restype = c_int;
    > > > > GetMyString.argtypes = [c_char_p];
    > > > >
    > > > > bResult = GetMyString (strSerialNumber);
    > > > >
    > > > > print (bResult);
    > > > > print (strSerialNumber);
    > > > >
    > > > > #C++ Prototype of the function I want to call:
    > > > > #bool GetMyString (string& stringParam) ;
    > > > >
    > > > >
    > > > >
    > > > >
    > > > > I do not have access to the source code of this function so don't ask
    > > > > me to try different things in C++. This DLL is working fine.
    > > > >
    > > > > The problem that I have is that print (strSerialNumber) does not seem
    > > > > to print the correct string. What I get is some garbage value of
    > > > > unprintable characters. Am I using this the correct way?
    > > > >
    > > >
    > > >
    > > >
    > > >
    > > > This function is expecting a C++ std::string object, not a regular C
    > > > style string. You'll need a wrapper function, and one which uses the
    > > > same compiler and STL as the C++ source.
    > > >
    > > > > Thanks.
    > > > >
    > > > > --
    > > > > http://mail.python.org/mailman/listinfo/python-list
    > > > >

    > >

    >
    > in this case it'd be very simple:
    >
    > bool WrapGetMyString(char * c) {
    > return GetMyString(c)
    > }
    >
    >
    > (Note: This isn't unicode safe).
    >
    > The C++ compiler will handle converting from the char * to the std::string.
    >
    >
    >
    >
    >
    > > --
    > > http://mail.python.org/mailman/listinfo/python-list
    > >
    , Oct 4, 2006
    #5
  6. Guest

    wrote:
    > Hi all.
    >
    > I just started looking at Python's ctypes lib and I am having trouble
    > using it for a function.

    [...]
    > #C++ Prototype of the function I want to call:


    ctypes provides support for calling functions in C libraries, using C
    datatypes. I don't see any reason to believe it would work with other
    languages (e.g. C++).
    , Oct 4, 2006
    #6
    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. Kent P. Iler
    Replies:
    0
    Views:
    610
    Kent P. Iler
    Mar 7, 2004
  2. Henk Punt
    Replies:
    0
    Views:
    390
    Henk Punt
    Jul 23, 2004
  3. Stu
    Replies:
    2
    Views:
    244
  4. Matt
    Replies:
    5
    Views:
    295
  5. Replies:
    0
    Views:
    491
Loading...

Share This Page