When looking at the two examples from the earlier section in detail, you can see that we use
None
as one of the parameters for a dynamically linked C library. This is possible because
None
,
integers
,
longs
,
byte strings
, and
unicode strings
are the native Python objects that can be directly used as the parameters in these function calls.
None
is passed as a C,
NULL pointer
,
byte strings
, and
unicode strings
are passed as pointers to the memory block that contains their data (
char *
or
wchar_t *
). Python
integers
and Python
longs
are passed as the platform's default C
int type
, their value is masked to fit into the C type. A complete overview of the Python types and their corresponding ctype types can be seen in
Table 1
:
Defining Unions and Structures
Unions
and
Structures
are important data types because they are frequently used throughout the
libc
on Linux and also in the Microsoft Win32 API.
Unions are simply a group of variables, which can be of the same or different data types, where all of its members share the same memory location. By storing variables in this way, unions allow you to specify the same value in different types. For the upcoming example, we will change from the interactive Python shell to the atom editor on our Ubuntu lab environment. You just need to open atom editor, type in the following code, and save it under the name
new_evidence.py
:
If you assign the
evidence
union's member variable
evidence_int
a value of
42
, you can then use the
evidence_char
member to display the character representation of that number, as shown in the following example:
As you can see in the preceding example, by assigning the union a single value, you get three different representations of that value. For
int
and
long
, the displayed output is obvious but for the
evidence_char
variable, it could be a bit confusing. In this case,
'*'
is the ASCII character with the value of the equivalent of decimal
42
. The
evidence_char
member variable is a good example of how to define an
array
in ctypes. In ctypes, an array is defined by multiplying a type by the number of elements that you want to allocate in the array. In this example, a four-element character array was defined for the member variable
evidence_char
.
A structure is very similar to unions, but the members do not share the same memory location. You can access any of the member variables in the structure using dot notation, such as
case.name
. This would access the
name
variable contained in the
case
structure. The following is a very brief example of how to create a
structure
(or
struct,
as they are often called) with three members:
name
,
number
, and
investigator_name
so that all can be accessed by the dot notation:
Tip
Downloading the example code
You can download the example code files from your account at
http://www.packtpub.com
for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit
http://www.packtpub.com/support
and register to have the files e-mailed directly to you.