Problem in Accessing C++ Class Instance Global Variable (near success, help please!!)

Alan Pong alanpong at hkstar.com
Fri Apr 18 10:31:56 CEST 2003


python 2.3, vc++6, win32 exe project

I has one global c++ class instance variable.  I want to call its
member
function through wrapper function which is accessed by python.
However, as seen in wrapper.cpp, it give me another new variable copy
so that phython actually accessing another instance.

Does anybody has idea to fix it?
thanks.
rgds.
alan

-- test.cpp (will be compiled into test.exe)
CTest gtest; //one and only one global variable
main()
{
	gtest.SetString("abcd");
	Py_Initialize();
	PyImport_AddModule( "myext1" );
	PyRun_SimpleFile( fp, "test.py" );
	prinf("%s",gtest.GetString());	//still "abcd", unchange
}


-- wrapper.cpp (will be compiled into test.exe)
extern CTest gtest;

extern "C" CTest * getInstance()
{
	return &gtest;  //***Question is here: a new variable!!! why?***
}

extern "C" void SetString(char* str)
{
	gtest.SetString(str); 
	messagebox(null,str,null,0); //shows "2345",i.e. the script works!
}

-- myext1.c (will be compiled into myext1.dll)
extern char *getInstance();
extern void SetString(char *str);

static PyObject *
import_getInstance( PyObject *self, PyObject *args )
{
  char *obj = getInstance();
  return Py_BuildValue("i", obj);
}

static PyObject *import_SetString(PyObject *self, PyObject *args )
{
	char *msg;

	if (!PyArg_ParseTuple(args, "s", &msg))
	    return NULL;
	SetString(msg);
	Py_INCREF(Py_None); 
	return Py_None;
}

-- test.py
from myext1 import *
SetString("2345")

-- END




More information about the Python-list mailing list