Domanda

I am trying to pass numpy arrays to C and do some calculations there.

I am building the executable as exe and export some functions which Python then calls. The procedure works but I am unable to use standard functions like printf(). It works if I build the binary as a dll. From searching on the internet, I understood that the dll loading and exe loading (maincrtstartup?) do initializations for runtime libraries. Is it possible to replicate what they do so that I can use printf()?

Python code

import  numpy as np
import ctypes
from numpy.ctypeslib import ndpointer

lib = ctypes.cdll.LoadLibrary('pycon.exe')

fun = lib.fun
fun.restype = None
fun.argtypes = [ndpointer(ctypes.c_double),
                ctypes.c_size_t]



data = np.ones((5,5)).astype('double')
def wfun(d):
    fun(d, d.size)
print data
wfun(data)
print data

C code

#include <stdio.h>
#include <math.h>
extern "C" __declspec(dllexport) 
void fun(double *datav, size_t size) {
    //printf("crash");
    double * data = (double *) datav;
    int i;
    for (i = 0; i < size; ++i)
        data[i] = 1.7159*tanh(0.66666667*data[i]);
}


extern "C" __declspec(dllexport) 
void init() {
    //what to put here?
}


int main(int argc, char* argv[])
{
    return 0;
}
È stato utile?

Soluzione

i have realized that treating an exe as a dll is far more involved than i imagined. i ended up with the following workaround.

python

import  numpy as np
import ctypes as C

lib = C.cdll.LoadLibrary('pycon.exe')
lib.init( C.windll.kernel32.GetProcAddress( C.windll.msvcrt._handle,'printf'))

d = np.ones((5)).astype('double')
lib.fun(d.ctypes.data_as(C.POINTER(C.c_double)), C.c_size_t(5))    

c

#include <stdio.h>
#include <math.h>

int (*pyprintf)(const char *,...) =0;

extern "C" __declspec(dllexport) 
void fun(double *datav, size_t size) {
        double * data = (double *) datav;
        int i;
        for (i = 0; i < size; ++i)
        {
            data[i] = 1.7159*tanh(0.66666667*data[i]);
            pyprintf("workign! %f\n",data[i]);
        }
}

extern "C" __declspec(dllexport) 
void init(int (*_printfPtr)(const char *,...)) {
        pyprintf=_printfPtr;
}

int main(int argc, char* argv[])
{
    printf("exe");
    return 0;
}

edit

curiosity has gotten the better of me and the rabbit hole grows, now it's slightly less kludgy than above and seems to run both as exe and dll with a bit more loading code.

requirements

  • 'exe' must only have dependency on kernel32
  • pefile package
  • linker flags /FIXED:NO -entry:_DllMainCRTStartup@12

python

import  numpy as np
import ctypes as C
import pefile

def unprotect( address): 
    crap = C.byref(C.create_string_buffer("\x00"*4))
    res = C.windll.kernel32.VirtualProtect(address, 0x1000, 0x40, crap)

lib = C.cdll.LoadLibrary('pycon.exe')
k32 = C.windll.kernel32
pe =  pefile.PE('pycon.exe')


for entry in pe.DIRECTORY_ENTRY_IMPORT:
  #print entry.dll
  #assuming that the exe only has dependency on kernel32
  for imp in entry.imports:
    diff = imp.address-0x400000
    memptr = k32.GetProcAddress(k32._handle,imp.name)
    unprotect(lib._handle+diff)
    fptr = (C.c_uint).from_address(lib._handle+diff)
    fptr.value = memptr

d = np.ones((5)).astype('double')
lib.init()
lib.fun(d.ctypes.data_as(C.POINTER(C.c_double)), C.c_size_t(5))   
print d

c

#include <stdio.h>
#include <math.h>
#include <windows.h>

extern "C" BOOL WINAPI  _CRT_INIT(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);

extern "C" __declspec(dllexport) 
    void fun(double *datav, size_t size) {
        double * data = (double *) datav;
        int i;
        for (i = 0; i < size; ++i)
        {
            data[i] = 1.7159*tanh(0.66666667*data[i]);
            printf("workign! %f\n",data[i]);
        }
        fflush(stdout);
}

extern "C" __declspec(dllexport) 
    void init() {
        _CRT_INIT(NULL,DLL_PROCESS_ATTACH,NULL);
}


BOOL APIENTRY DllMain( HMODULE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved
                      )
{
    _CRT_INIT(NULL,DLL_PROCESS_ATTACH,NULL);
    printf("main exe");
    return TRUE;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top