This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ctypes: need example how to pass raw data from Python
Type: Stage:
Components: ctypes, Documentation Versions: Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: amaury.forgeotdarc, docs@python, techtonik
Priority: normal Keywords:

Created on 2011-07-03 08:02 by techtonik, last changed 2022-04-11 14:57 by admin.

Messages (3)
msg139675 - (view) Author: anatoly techtonik (techtonik) Date: 2011-07-03 08:02
ctypes documentation lacks an example, how to pass a raw data block from Python to a C function. For example, how to pass this chunk:

data = open('somefile', 'rb').read()

to this function:

int checkBuffer(LPSTR lpData, DWORD dwBufferLength);

msg139676 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-07-03 08:52
Can you suggest a patch?
msg139686 - (view) Author: anatoly techtonik (techtonik) Date: 2011-07-03 12:24
ISTM that this code works ok.

data = open('data.raw', 'rb').read()
ret = checkBuffer(data, len(data))

You need to make sure that checkBuffer doesn't try to modify memory though. For mutable memory blocks use create_string_buffer().
Date User Action Args
2022-04-11 14:57:19adminsetgithub: 56685
2011-07-03 12:24:43techtoniksetmessages: + msg139686
2011-07-03 08:52:25amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg139676
2011-07-03 08:02:36techtonikcreate