Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i have realized that treating an exe as a dll is far more involved than i imagined. i ended up with the following workaround.</p> <p>python</p> <pre><code>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)) </code></pre> <p>c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; 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 &lt; 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; } </code></pre> <h1>edit</h1> <p>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.</p> <h2>requirements</h2> <ul> <li>'exe' must only have dependency on kernel32</li> <li>pefile package</li> <li>linker flags <code>/FIXED:NO -entry:_DllMainCRTStartup@12</code></li> </ul> <p>python</p> <pre><code>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 </code></pre> <p>c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; #include &lt;windows.h&gt; 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 &lt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload