Win32 DLL problem: Extension crashes when using FILE *

rwgk at my-deja.com rwgk at my-deja.com
Tue Aug 15 01:24:00 EDT 2000


Thanks for the responses to my posting! The "vital information" is
indeed in the readme.txt file. On my first pass I got confused with
the VC6 differences.

Attached is my own version of instructions for compiling extensions
for Python with Visual C++ 6.0. I have tested this with both
Release and Debug builds of Python. Hope others find this helpful.

Ralf


This file is based on Python-1.6b1/PC/example_nt/example.txt.


Example Python extension for Windows 98 & Microsoft Visual C++ 6.0
==================================================================

Creating a brand new project (Release build)
--------------------------------------------

Create a new directory, e.g. "C:\users\jack\example."
Copy the files "example.c" and "example.def" into that directory.

Start -> Programs -> Microsoft Visual C++ 6.0 -> Microsoft Visual C++
6.0

>From the main VC60 menu select File->New. Select "Win32 Dynamic-Link
Library" from the list in the Projects Tab. Next set the "Location",
e.g., "C:\users\jack." After that enter the "Project name", e.g.
"example." (As soon as you start typing the Project name, the Location
box will be updated. I.e., the Location will automatically become
"C:\users\jack\example.") Verify that "Create new workspace" is checked
and click "OK."

A new dialog appears, with the header "Win32 Dynamic-Link Library -
Step 1 of 1." Verify that "An empty DLL project" is checked and click
"Finish."

A new dialog "New Project Information" appears. Click "OK."

>From the main VC60 menu select Project -> Add To Project -> Files.
Use the file browser to select "C:\users\jack\example\example.c" and
click "OK." Repeat the step to add "example.def" to the project.

>From the main VC60 menu select Project -> Settings. The "Project
Settings" dialog appears.

In the upper left hand corner of the "Project Settings" dialog
set "Settings For: All Configurations."

Click the "C/C++" tab in the "Project Settings" dialog. At the top of
the page select the "Category: Code Generation". In the box labeled
"Use run-time library", choose "Multithreaded DLL". Select the
"Category: Preprocessor." In the "Additional Include Directories" box
enter the path to the Python.h include file. E.g., "C:\Program
Files\Python\include." (Unfortunately there is no file browser for this
box. Use the Windows Start -> Find dialog to search for Python.h if you
do not know to path.)

Click the "Link" tab of the "Project Settings" dialog. At the top
of the page, select the "Category: General." In the "Object/library
modules" box add "python15.lib" to the end of the list. Select the
"Category: Input." In the "Additional library path" box enter the path
to python15.lib. E.g., "C:\Program Files\Python\libs."

Close the "Project Settings" dialog by clicking "OK."

>From the main VC60 menu select Project -> Set Active Configuration.
Select "example - Win32 Release" from the list of "Project
configurations." Click "OK" to close the dialog.

>From the main VC60 menu select Build -> Build example.dll. Wait for
the build to finish and you should be able to find "example.dll" in the
Release subdirectoy.

C:\users\jack\example\Release> python
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import example
>>> example.foo()
Hello world!
>>> example.object(42)
n = 42
>>>

Remark: It is possible to avoid the "example.def" file by adding the
option "/export:initexample" to the end of the "Project Options" under
the "Link" tab in the "Project Settings" dialog.


Configuration continued: Debug build
------------------------------------

>From the main VC60 menu select Project -> Set Active Configuration.
Select "example - Win32 Debug" from the list of "Project
configurations." Click "OK" to close the dialog.

>From the main VC60 menu select Project -> Settings. The "Project
Settings" dialog appears.

Click the "C/C++" tab in the "Project Settings" dialog. At the top of
the page select the "Category: Code Generation". In the box labeled
"Use run-time library", choose "Debug Multithreaded DLL".

Click the "Link" tab of the "Project Settings" dialog. At the top of
the page, select the "Category: General." In the "Object/library
modules" box change "python15.lib" to "python15_d.lib." In the "Output
file name" box change "Debug/example.dll" to Debug/example_d.dll."

Close the "Project Settings" dialog by clicking "OK."

>From the main VC60 menu select Build -> Build example.dll. Wait for
the build to finish and you should be able to find "example_d.dll" in
the Debug subdirectoy:


C:\users\jack\example\Debug> python_d
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import example
>>> example.foo()
Hello world!
>>> example.object(42)
n = 42
>>>

Note that python_d.exe is not in the standard binary distribution for
Win32.

WARNING: If your Debug build produces a module without the "_d"
attached to the name, you will be able to import this module with
python from a Release build. Some functions of your module will work
fine. However, other functions, for example PRI_MyPyObj() in example.c,
will cause a crash when accessing the FILE * pointer. To avoid this
situation, take great care to attach "_d" to all your modules from a
Debug build.


Compiling from the command line
-------------------------------

To setup the VC60 command line environment, run the batch file:
"C:\Program Files\Microsoft Visual Studio\VC98\Bin\vcvars32.bat"
This batch file can be added to the preferences of the "MS-DOS Prompt"
program (click on the Properties icon in the toolbar; under the Program
tab, enter the batch file name in the "Batch file" box). You need to
include the quotes.

Use Visual C++ 6.0 to create a makefile (Project -> Export Makefile).
By default the file name is example.mak.

Follow the instructions at the top of the exported makefile.




Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list