NO

Author Topic: Python3.dll  (Read 5014 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2123
Python3.dll
« on: December 03, 2023, 06:42:07 PM »
In other site people want to use python dll.
I share that research at here too
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")
// https://docs.python.org/3/c-api/
typedef void (PY_INITIALIZE)(void);
typedef PY_INITIALIZE *LPPY_INITIALIZE;
// PyRun_SimpleString
typedef int PYRUN_SIMPLESTRING(char *);
typedef PYRUN_SIMPLESTRING *LPPYRUN_SIMPLESTRING;

int __cdecl main(void)
{
    static LPPY_INITIALIZE Py_Initialize;
    static LPPYRUN_SIMPLESTRING PyRun_SimpleString;
    char szVersion[50];
    char szPythonDll[260] = "bin64/Python3.dll";
    HMODULE hMod = LoadLibrary(szPythonDll);    //
    if (hMod) {
        int nLen = GetModuleFileName(hMod, szPythonDll, 260);
        LoadString(hMod, 1000, szVersion, 50);
        printf("%s\n", szVersion);
        FreeLibrary(hMod);
        szPythonDll[nLen-5] = szVersion[0];
        szPythonDll[nLen-4] = szVersion[2];
        szPythonDll[nLen-3] = 0;
        printf("%s\n", szPythonDll);
        hMod = LoadLibrary(szPythonDll);
    }
    if (hMod) {
        printf("%s loaded\n", szPythonDll);
        Py_Initialize = (LPPY_INITIALIZE)GetProcAddress(hMod, "Py_Initialize");
        if (Py_Initialize) {
            Py_Initialize();
            PyRun_SimpleString = (LPPYRUN_SIMPLESTRING)GetProcAddress(hMod, "PyRun_SimpleString");
            if (PyRun_SimpleString) {
                PyRun_SimpleString("print('Python Print test')");
            } else printf("PyRun_SimpleString not found\n");
        } else printf("Py_Initialize not found\n");
        FreeLibrary(hMod);
    } else printf("Python3x.dll not loaded\n");
    return 0;
}
Code: [Select]
    char szVersion[50];
    char szPythonDll[260] = "bin64_310/Python3.dll";
    HMODULE hMod = LoadLibrary(szPythonDll);
    if (hMod) {
        int nLen = GetModuleFileName(hMod, szPythonDll, 260);
        printf("%s\n",GetVersionInfo(hMod, "FileVersion", szVersion, sizeof(szVersion)));
        FreeLibrary(hMod);
        szPythonDll[nLen-5] = szVersion[0];
        szPythonDll[nLen-4] = szVersion[2];
        if (szVersion[3] != '.') szPythonDll[nLen-3] = szVersion[3]; else szPythonDll[nLen-3] = 0;
        szPythonDll[nLen-2] = 0;
        printf("%s\n", szPythonDll);
        hMod = LoadLibrary(szPythonDll);
    }
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winver.h>
#include <stdio.h>

#pragma comment(lib, "version.lib")
#pragma comment(lib, "user32.lib")

LPSTR GetVersionInfo(HMODULE hMod, TCHAR *szValue, TCHAR *szBuffer, ULONG nLength)
{
    LPSTR csRet;

    csRet = NULL;
    HRSRC hVersion = FindResource(hMod, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
    DWORD dwSize = SizeofResource(hMod, hVersion);
    if (hVersion != NULL)
    {
        HGLOBAL hGlobal = LoadResource(hMod, hVersion);

        if (hGlobal != NULL)
        {
            //LPVOID ver = LockResource(hGlobal);
            LPVOID ver = LocalAlloc(LPTR, dwSize*2);

            if (ver != NULL)
            {
                memcpy(ver, hGlobal, dwSize);
                DWORD *codepage;
                UINT len;
                char fmt[0x40];
                PVOID ptr = 0;
                if (VerQueryValue(ver, "\\VarFileInfo\\Translation", (LPVOID) & codepage, &len))
                {
                    //wsprintf(fmt, "\\StringFileInfo\\%04x%04x\\%s", (*codepage) & 0xFFFF, (*codepage) >> 16, csEntry);
                    wsprintf(fmt, "\\StringFileInfo\\%08x\\%s", (*codepage) << 16 | (*codepage) >> 16, szValue);
                    if (VerQueryValue(ver, fmt, &ptr, &len))
                    {
                        lstrcpyn(szBuffer, (TCHAR*)ptr, min(nLength, len));
                        csRet = szBuffer;
                    }
                }
                LocalFree(ver);
            }
            FreeResource(hGlobal);
        }
    }
    return csRet;
}
Code: [Select]
char szVersion[50];
char szPythonDll[260] = "bin64_310/Python3.dll";
wchar_t wszPath[260];
HMODULE hMod = LoadLibrary(szPythonDll);
if (hMod) {
int nLen = GetModuleFileNameW(hMod, wszPath, 260);
while (wszPath[--nLen] != '\\');
if (nLen) wszPath[nLen+1] = 0;
AddDllDirectory(wszPath);
nLen = GetModuleFileName(hMod, szPythonDll, 260);
printf("%s\n",GetVersionInfo(hMod, "FileVersion", szVersion, sizeof(szVersion)));
FreeLibrary(hMod);
szPythonDll[nLen-5] = szVersion[0];
szPythonDll[nLen-4] = szVersion[2];
if (szVersion[3] != '.') szPythonDll[nLen-3] = szVersion[3]; else szPythonDll[nLen-3] = 0;
szPythonDll[nLen-2] = 0;
printf("%s\n", szPythonDll);
//hMod = LoadLibrary(szPythonDll);
//if (!hMod)
hMod = LoadLibraryEx(szPythonDll, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_USER_DIRS);
}
Code: [Select]
int nLen = GetModuleFileNameW(hMod, wszPath, 260);
while (wszPath[--nLen] != '\\');
if (nLen) wszPath[nLen+1] = 0;
SetEnvironmentVariableW(L"PATH", wszPath);
with one buffer
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>

int __cdecl main(void)
{
char szPath[260];
HANDLE hMod = GetModuleHandle(NULL);
int nLen = GetModuleFileName(hMod, szPath, 260);
int nPos = nLen;
while (szPath[--nPos] != '\\');
if (nPos) {
szPath[nPos] = 0;
puts(szPath);
szPath[nPos] = '\\';
puts(szPath);
}
return 0;
}

A missing dll for Windows 7, if want to test 9,10,11 versions
https://github.com/nalexandru/api-ms-win-core-path-HACK

Python Releases for Windows
« Last Edit: December 04, 2023, 03:20:51 PM by TimoVJL »
May the source be with you

Offline HellOfMice

  • Member
  • *
  • Posts: 122
  • Never be pleased, always improve
Re: Python3.dll
« Reply #1 on: October 25, 2024, 04:17:58 PM »
Very interesting.


thank you :o :o
--------------------------------
Kenavo