DLL in Python

Mike Müller mmueller at dgfz.de
Tue Oct 24 10:21:46 EDT 2000


Alex,

thanks for your advise.
I tried to compile the following *.cpp file, which is just a cut and paste
from what you posted previously:

include <stdio.h>
#include <Python.h>

PyObject* module_object;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    if(ul_reason_for_call == DLL_PROCESS_ATTACH) {
        Py_Initialize();
        if(PyErr_Occurred()) {
            PyErr_Print();
            return FALSE;
        }
        module_object = PyImport_ImportModule("testdll");
        if(PyErr_Occurred()) {
            PyErr_Print();
            return FALSE;
        }
        if(!module_object) return FALSE;
    }
    return TRUE;
}

__declspec(dllexport)
int test_int4(int x, int y)
{
    PyErr_Clear();
    PyObject* result = PyObject_CallMethod(
        module_object, "test_int4", "ii", x, y);
    if(!result) {
        /* disaster -- make it known somehow, e.g: */
        fprintf(stderr, "Error calling test_int4(%d,%d)\n", x, y);
        if(PyErr_Occurred()) {
            PyErr_Print();
        }
        return 0;
    } else {
        PyObject* pintresult = PyNumber_Int(result);
        Py_DECREF(result);
        if(!pintresult) {
            /* other disaster -- make it known somehow, e.g: */
            fprintf(stderr, "test_int4(%d,%d) returned non-int\n", x, y);
            if(PyErr_Occurred()) {
                PyErr_Print();
            }
            return 0;
        } else {
            int intresult = PyInt_AS_LONG(pintresult);
            Py_DECREF(pintresult);
            return intresult;
        }
    }
}

Trying to compile this  I get the following (German orginal with my
translation between the lines):

--------------------Configuration: Int4Py - Win32 Debug--------------------
Compiling...
int4fromPy.cpp
g:\projekte\spree\dll\int4py\int4frompy.cpp(6) : error C2146: Syntaxfehler :
Fehlendes ';' vor Bezeichner 'APIENTRY'
syntax error: Missing  ';' in front of identifier 'APIENTRY'
g:\projekte\spree\dll\int4py\int4frompy.cpp(6) : error C2501: 'BOOL' :
Fehlende Speicherklasse oder Typbezeichner
'BOOL' :  Missing memory class or type identifier
g:\projekte\spree\dll\int4py\int4frompy.cpp(6) : fatal error C1004:
Unerwartetes Dateiende gefunden
Unexpected EOF found
Error executing cl.exe.

Int4Py.dll - 3 error(s), 0 warning(s)

The translation may sound akward but that is all I can come up with.

I looked through the VC++ help system and found an example showing the
approach above.
I am not sure if used a wrong option. Both, Multithreaded DLL and  Debug
Multithreaded DLL
yield the same result. Fiddling for several hours with some other things I
found in the help did not
change anything.

What could be the reason?

Thanks.
Mike






More information about the Python-list mailing list