C++ Builder and extension modules

vmarkwart at my-deja.com vmarkwart at my-deja.com
Wed Feb 16 23:36:23 EST 2000

I'm (trying) to use Borland's C++ Builder to create an extension module
(see code below). after converting python15.lib to omf format it
compiles with no errors and even works. However, when an exception
occurs Python dies informing me that "The instruction at xxx referenced
memory at xxxx. The memory could not be written"
There's probably something really simple I'm ommitting. Could someone
help please?


#include "python/Python.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <vcl.h>
extern "C" __declspec(dllexport) void  _stdcall inittools(void);

#pragma hdrstop
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//   If your DLL exports any functions that pass String objects (or
//   classes containing nested Strings) as parameter or function
//   you will need to add the library MEMMGR.LIB to both the DLL project
//   any other projects that use the DLL.  You will also need to use
//   if any other projects which use the DLL will be perfomring new or
//   operations on any non-TObject-derived classes which are exported
from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//   To avoid using BORLNDMM.DLL, pass string information using "char *"
//   ShortString parameters.
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
        return 1;

static PyObject *                                 /* returns object */
tools_getUNC(PyObject *self, PyObject *args)       /* self not used */
{                                                 /* args from python */
  char *varName,  *res;
  long err = 0;
  unsigned long size = 249;
  res=(char *)calloc(size +1, sizeof(char));
  PyObject *returnObj = NULL;                         /* null=exception

  if (PyArg_Parse(args, "s", &varName))
    err= WNetGetConnection(varName, res, &size);
//    res = (ExpandUNCFileName(varName)).c_str();
    if (err==0 )
      returnObj = Py_BuildValue("s", res);   /* Python -> C */
      PyErr_SetString(PyExc_TypeError, "NetError");
    PyErr_SetString(PyExc_TypeError, "Usage: getUNC(path) Example:
getUNC ('E:')   ");

  return returnObj;

static struct PyMethodDef tools_methods[] = {
    {"getUNC", tools_getUNC},
    {NULL, NULL}

void _stdcall inittools()                  /* on first import */
    (void) Py_InitModule("tools", tools_methods);


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

More information about the Python-list mailing list