[python-win32] embedding IPython in a windows dll
mani sabri
mani.sabri at gmail.com
Mon Mar 10 23:54:51 CET 2008
>-----Original Message-----
>From: python-win32-bounces at python.org [mailto:python-win32-
>I have no idea what you are asking.
I want to run a Python shell from a DllMain() function.
This is what I came up with so far:
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID
lpReserved)
{
//----
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
if (!Py_IsInitialized())
{
BOOL res = AllocConsole();
//I was going to use the handles but I heard its not the way to do
that
//HANDLE OutHand = GetStdHandle(STD_OUTPUT_HANDLE);
//HANDLE InHand = GetStdHandle(STD_INPUT_HANDLE);
//HANDLE ErrHand = GetStdHandle(STD_ERROR_HANDLE);
//Redirecting console
freopen("CONOUT$","w",stdout);
freopen("CONIN$","w",stdin);
freopen("CONERR$","w",stderr);
//Another way to redirect console
//std::ofstream console_out("CONOUT$");
//std::ifstream console_in("CONIN$");
//std::ofstream console_err("CONOUT$");
//the printf prints on the new console after redirection
printf("test");
//opening stdout and stdin files to pass to python for redirection
FILE* afile = fopen("CONOUT$", "w+");
FILE* bfile = fopen("CONIN$", "w+");
Py_Initialize();
if (!Py_IsInitialized()) return(FALSE);
//the bellow lines , instead of redirecting python crashes the dll
PySys_SetObject("stdout", PyFile_FromFile(afile, "test","wb",
fclose));
PySys_SetObject("stdin", PyFile_FromFile(bfile, "CONIN$","wb",
fclose));
//these lines produces no output
PyRun_SimpleString("print 'hello'\n");
PyRun_SimpleString("from IPython.Shell import IPShellEmbed\n");
PyRun_SimpleString("ipshell = IPShellEmbed()\n");
PyRun_SimpleString("ipshell()\n");
}
break;
}
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
{
FreeConsole();
Py_Finalize();
break;
}
}
//----
return(TRUE);
}
>Are you trying to give access to a Python shell inside of another
>application? If so, then AllocConsole is not what you want. The
>wxPython GUI framework includes several tools to do exactly that. They
>put up an editable window, which is often more useful than a barebones
>console.
As you said all I was able to accomplish with AllocConsole was to redirect
the printf not the python
>If I misunderstand you, try asking your question again, and be specific.
If it's not clear please don't hesitate to ask.
Best regards,
Mani
More information about the python-win32
mailing list