[python-win32] Embedded Python & win32api

Howard Lightstone howard at eegsoftware.com
Sat Jun 21 21:17:24 CEST 2008


> > I'm getting crasy about embedding python-win32 in a c++ application.
--snip--

I had the same issue.  Here is the sequence I evolved from both 2.2 and 2.5
implementations (and working in production code although this code IS
just at part of the stuff):


// some tables I use
char *plines[]=
{
    "import sys\n",
    "",
    "@",
    "@\\dlls",
    "@\\libs",
    "@\\win32\\lib",
    "@\\win32",
//  my special stuff was here - notably my DebugWin to catch 
// stdout/stderr and pop them up in a tk window
    NULL
};

// a utility function
char *
doubleup(char *input)
{
    static char ans[MAX_PATH];
    char *a = ans;

    while (*input)
	{
	if (*input == '\\')
	    *a++ = '\\';
	*a++ = *input++;
        if (a >= &ans[MAX_PATH-1])
	    break;
	}
    *a++=0;	// ensure 0 termination
    return ans;
}


// the fragment from my main where I set things up
// please note..... not every variable is defined in this fragment!!


	p=getenv("PATH");
	if (!p)
	{
		MessageBox(GetDesktopWindow(),
                              "No PATH found!!!","***** Failure",MB_OK);
		exit(4);
	}
	if (strlen(p) >= sizeof(lpath))
	{
		MessageBox(GetDesktopWindow(),"PATH too 
long","*****Failure",MB_OK);
		exit(4);
	}
	strncpy(lpath,"PATH=",sizeof(lpath));
	strncpy(ulpath,p,sizeof(ulpath));
	_strupr(ulpath);
	i=0;
	if (!strstr(ulpath,"mylocalpath\\PYTHON25\\DLLS"))
	{
		i=1;
	}
	strncat(lpath,p,sizeof(lpath));
	strncpy(local,StartupDir,sizeof(local));
	_strupr(local);
	if (!strstr(ulpath,local))
	{
		i |= 2;
	}
	// make sure I come first in paths- 
            // otherwise stupid Windoze can't find some DLLs if the are included
	// by other DLLs since the search path is DIFFERENT than that used by 
            //the first inclusion (usually)

	if (i)
	{
	    ulpath[0]=0;
	    if (i & 2)
	    {   // put me first
		strncat(ulpath,local,sizeof(ulpath);
		strncat(ulpath,";",sizeof(ulpath));
	    }
	    if (i & 1)
	    { // put my local Python dirs at the the beginning of the path!!!!
	    	
strncat(ulpath,"mylocalpath\\Python25;mylocalpath\\Python25\\DLLs;",
                             sizeof(ulpath));
	    }
 	    strncat(ulpath,lpath,sizeof(ulpath));

	    _putenv(lpath);
	}
		
	_putenv("PYTHONHOME=mylocalpath\\Python25");
        _putenv("PYTHONPATH="); // remove from environment - it never worked 
correctly

//using TCL???
	_putenv("TCL_LIBRARY=mylocalpath\\python25\\tcl\\tcl8.4");

// moving on
	// put my startup path here too so I can 'find' modules to import
	strncpy(local,"sys.path.insert(0,   '");
	strcat(local,doubleup(StartupDir));
	strcat(local,"')\n");
	plines[1]=_strdup(local);



// alt this and now we are finally ready to do some "pythoning"

	    /* Pass argv[0] to the Python interpreter */
	Py_SetProgramName(filename);	// this is the "startup" filename 
with a path on it

	/* Initialize the Python interpreter.  Required. */
	Py_Initialize();
	// initialize thread support
	PyEval_InitThreads();

// different logic goes in here if if using Python before 2.5

	PySys_SetArgv(1, argvx);	// some array is REQUIRED else error 
on argv !!!!!

// this pre-imports some modules for me 

	for (i=0;plines[i];++i)
	{
	    char *pl;
	    pl=plines[i];
	    if (pl[0] == '@')
	    {
		strcpy(local,"sys.path.append('");
		strcat(local,doubleup("mylocalpath\\Python25));
		strcat(local,doubleup(&pl[1]));
		strcat(local,"')\n");
		pl=local;   
	    }
	    // execute some python code
	    emsg[0]=0;
	    if (PyRun_SimpleString(pl) == -1)
	    {

		PyErr_Fetch (&ptype, &pvalue, &ptrace);

		sprintf(local,"err on %s \n\n%s",plines[i],emsg);
		MessageBox(NULL,local,"Error",MB_OK );
	    }
	}

// this point, the interpreter is working and has had some basic modules imported

// I have callbacks from python into my C++ code so....
            PyImport_AddModule("pyaccess");
           Py_InitModule("pyaccess", pyaccess_methods);

// start me up
	ans=PyRun_SimpleString("import mymainpythonmodule\n");

// note that all my Python code actually runs in this import (mymainpythonmodule)
// but something else could run/rerun at this point



More information about the python-win32 mailing list