Win32 embedding and extending

Michael Ingram michael.ingram at echostar.com
Tue May 21 14:13:41 EDT 2002


Python newbie.

I'm embedding the Python 2.2 interpreter in a VC++ SDI app.  I'm also
building Python dll's that wrap existing C Matrox Imaging Library (MIL)
code.

Using swig to wrap the dll's has helped tremendously and everything
works great.  However, because swig has worked so well, I don't really
have a firm grasp of how marshalling between the two languages is
actually performed.

Two questions:

1.) I would like to redirect the interpreter's output to one of my
    SDI's CRichEditCtrl controls.  Is this the intention of Win32ui?

2.) I need to free (delete) MIL objects that were allocated in the
    extension dll's, in the event the interpreter is aborted by the  
    SDI.  This amounts (I feel), to another SDI object being updated
    by the current Python process.  In other words, #1 with out
    the Win32ui or MFC part.

Any philosophy on the two issues?

######################## SAMPLE SDI CODE  ########################## 

UINT PythonThread( void *arglist )
{
	
	CAutopyView* pView = (CAutopyView*)arglist;
	if(pView->m_ScriptRichEditCtl.GetModify())
	{
	   CFile* TempFile = new CFile(TEXT((LPCTSTR)pView->m_CurrentFile),
                    CFile::modeCreate | CFile::modeWrite );
	   EDITSTREAM es;
	   es.dwCookie = (DWORD)TempFile; 
	   es.pfnCallback = MyStreamOutCallback; 
	   pView->m_ScriptRichEditCtl.StreamOut(SF_TEXT, es);
	   TempFile->Close();
	   delete TempFile;
	   pView->m_ScriptRichEditCtl.SetModify( FALSE );
	}
		
	if(FILE* fptr = fopen( (LPCTSTR)pView->m_CurrentFile, "r") )
	{
           Py_Initialize();
	   PyRun_SimpleFile(fptr, pView->m_CurrentFile.GetBuffer(0));
	   Py_Finalize();
	   fclose( fptr );
	}
	else
	   pView->MessageBox( "Error Running Script");
	
	return 0;
}

######################## SAMPLE script using extension dll's ###############

import MILpy
import time
#import win32gui
#import win32ui

def main():
	MILpy.SetupMIL() # Allocates some MIL constructs
	x = 20
	while x > 0:
		x = x - 1
		MILpy.Grab()
	
	MILpy.GrabContinuous()
	time.sleep(10)

	MILpy.FreeMIL()  # Free's the MIL constructs

if __name__=='__main__':
	main()



More information about the Python-list mailing list