[python-win32] embedding IPython in a windows dll
timr at probo.com
Tue Mar 11 00:43:21 CET 2008
mani sabri wrote:
> I want to run a Python shell from a DllMain() function.
It isn't safe to do this much processing in a DllMain. There is a thing
called the "loader lock" that is held while DllMain is called, and it
can prevent you from loading other DLLs. You might try spinning off a
thread to do your real work.
> This is what I came up with so far:
> BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID
> 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
> //HANDLE OutHand = GetStdHandle(STD_OUTPUT_HANDLE);
> //HANDLE InHand = GetStdHandle(STD_INPUT_HANDLE);
> //HANDLE ErrHand = GetStdHandle(STD_ERROR_HANDLE);
> //Redirecting console
Do you really think "w" is correct for all three of those?
> //the printf prints on the new console after redirection
> //opening stdout and stdin files to pass to python for redirection
> FILE* afile = fopen("CONOUT$", "w+");
> FILE* bfile = fopen("CONIN$", "w+");
Again, do you really think you want "w+ for both of these? How much
writing will you do to stdin?
> if (!Py_IsInitialized()) return(FALSE);
> //the bellow lines , instead of redirecting python crashes the dll
> PySys_SetObject("stdout", PyFile_FromFile(afile, "test","wb",
> PySys_SetObject("stdin", PyFile_FromFile(bfile, "CONIN$","wb",
Again, you're probably going to want to read from stdin, not write to
it. And you don't really need to do the fopen yourself:
PyFileObject* xout = PyFile_FromString( "CONOUT$", "wb" );
PyFileObject* xin = PyFile_FromString( "CONIN$", "rb" );
PySys_SetObject( "stdout", xout );
PySys_SetObject( "__stdout__", xout );
PySys_SetObject( "stdin", xin );
PySys_SetObject( "__stdin__", xin );
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32