2

Is it possible to run a .py script from CLI using python27.dll? I have tried this:

rundll32.exe python27.dll,PyRun_SimpleString "import myScript.py"

but seems not to work.

The situation is that I can install all python modules I want, but no executables, so I cannot install full Python.

1
  • you can convert into executable file then you do not need to install full python setup for execution.If you are using window convert into .exe format. check the video ==>youtube.com/watch?v=vPzc4OelblQ Commented Oct 11, 2017 at 13:43

1 Answer 1

3

You can't do this. Why?

Windows contains a command-line utility program named rundll32.exe that allows you to invoke a function exported from a 32-bit DLL using the following syntax:

RUNDLL.EXE <dllname>,<entrypoint> <optional arguments>

But, according to MSDN:

Rundll32 programs do not allow you to call any exported function from any DLL

[..]

The programs only allow you to call functions from a DLL that are explicitly written to be called by them.

The dll must export the following prototype to support it:

void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst,
                         LPSTR lpszCmdLine, int nCmdShow);

Since python.dll doesn't export such an entry point, you have to write a wrapper application in C/C++ that loads the dll and uses it, for example (here is a snippet from such an application):

// load the Python DLL
#ifdef _DEBUG
LPCWSTR pDllName = L"python27_d.dll" ;
#else
LPCWSTR pDllName = L"python27.dll" ;
#endif

HMODULE hModule = LoadLibrary( pDllName ) ;
assert( hModule != NULL ) ;
 
// locate the Py_InitializeEx() function
FARPROC pInitializeExFn = GetProcAddress( hModule , "Py_InitializeEx" ) ;
assert( pInitializeExFn != NULL ) ;
 
// call Py_InitializeEx()
typedef void (*PINITIALIZEEXFN)( int ) ;
((PINITIALIZEEXFN)pInitializeExFn)( 0 ) ;

FILE* fp ;
errno_t rc = fopen_s( &fp , pFilename , "r" ) ;
assert( rc == 0 && fp != NULL ) ;

[..] // go on to load PyRun_SimpleFile
if ( 0 == PyRun_SimpleFile( fp , pFilename )
    printf("Successfully executed script %s!\n", pFilename);

Origin: Awasu.com first and second tutorials

Sign up to request clarification or add additional context in comments.

4 Comments

<<A .dll file cannot be loaded using rundll32.exe>>: I don't think so, rundll32.exe is done for that purpose exactly, I think.
@luca.vercelli, I now see that my explanation wasn't clear enough, so I elaborated
You've provided excellent sources, however I can't seem to find a source to verify that "python.dll doesn't export such an entry point"?
@micsthepick, this was a long time ago. You can check that yourself by examining your Python DLL. See this thread for tools that can help enumerate all the exported functions from a DLL: stackoverflow.com/questions/1548637/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.