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 6473731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:30:06+00:00 2026-05-25T06:30:06+00:00

I am trying this simple ctypes example and getting the error mentioned >>> from

  • 0

I am trying this simple ctypes example and getting the error mentioned

>>> from ctypes import create_string_buffer
>>> str = create_string_buffer("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\ctypes\__init__.py", line 59, in create_string_buffer
buf.value = init
TypeError: str/bytes expected instead of str instance

Does anyone know what am I doing wrong?

On the same note, I am trying to pass a pointer to a string to a C function from my python code so that I can perform some string operation there and return another string. Can someone give me some sample code on how to do that?

extern "C" __declspec(dllexport) char * echo(char* c)
{
      // do stuff
    return c;
}
  • 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-25T06:30:07+00:00Added an answer on May 25, 2026 at 6:30 am

    With regards to getting it working, if you pass it a bytes object, it works:

    >>> import ctypes
    >>> ctypes.create_string_buffer(b'hello')
    <ctypes.c_char_Array_6 object at 0x25258c0>
    

    Looking at the code for create_string_buffer:

    def create_string_buffer(init, size=None):
        """create_string_buffer(aBytes) -> character array
        create_string_buffer(anInteger) -> character array
        create_string_buffer(aString, anInteger) -> character array
        """
        if isinstance(init, (str, bytes)):
            if size is None:
                size = len(init)+1
            buftype = c_char * size
            buf = buftype()
            buf.value = init
            return buf
        elif isinstance(init, int):
            buftype = c_char * init
            buf = buftype()
            return buf
        raise TypeError(init)
    

    Doing it directly,

    >>> (ctypes.c_char * 10)().value = b'123456789'
    

    This works fine.

    >>> (ctypes.c_char * 10)().value = '123456789'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: str/bytes expected instead of str instance
    

    This shows that same behaviour. Looks to me as though you’ve found a bug.

    Time to visit http://bugs.python.org. There are a few bugs related to c_char and create_string_buffer which are in the same field, but none reporting that giving it a str now fails (but there are definite examples showing it used to work in Py3K).

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

Sidebar

Related Questions

I have been working on my Ruby. When trying to execute this simple example
I'm trying to implement this simple example of how to synchronize threads using pthread
I'm trying to run this simple query over a JDBC connection: select _fk_DeptBillTo from
Have been struggling all day trying to make this simple example work using socket.io.
I am just trying to compile this simple example : http://msdn.microsoft.com/en-us/library/ms533895(VS.85).aspx what needs to
I'm trying this simple query: SELECT * FROM `Users` WHERE MATCH (`User`) AGAINST ('User')
I'm trying to get this simple PowerShell script working, but I think something is
I'm trying to get this simple program to work on windows, but it crashes:
I am trying to understand this simple hashlib code in Python that has been
I am trying to create this simple application in c#: when the user double

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.