1313
1414typedef int (*addProc)(int );
1515typedef int (*addNumberProc)(int , int );
16+ #ifdef _WIN64
17+ typedef void (*throwExceptionProc)(void );
18+ #endif
1619
1720// Thanks to Tim Cooper (from http://stackoverflow.com/a/8584708)
1821const char *sstrstr (const char *haystack, const char *needle, size_t length) {
@@ -283,6 +286,70 @@ BOOL LoadExportsFromMemory(char *filename)
283286 return result;
284287}
285288
289+ #ifdef _WIN64
290+ BOOL LoadExceptionsFromMemory (char *filename)
291+ {
292+ FILE *fp;
293+ unsigned char *data=NULL ;
294+ long size;
295+ size_t read;
296+ HMEMORYMODULE handle = NULL ;
297+ throwExceptionProc throwException;
298+ BOOL result = TRUE ;
299+
300+ fp = fopen (filename, " rb" );
301+ if (fp == NULL )
302+ {
303+ printf (" Can't open DLL file \" %s\" ." , filename);
304+ result = FALSE ;
305+ goto exit;
306+ }
307+
308+ fseek (fp, 0 , SEEK_END);
309+ size = ftell (fp);
310+ assert (size > 0 );
311+ data = (unsigned char *)malloc (size);
312+ assert (data != NULL );
313+ fseek (fp, 0 , SEEK_SET);
314+ read = fread (data, 1 , size, fp);
315+ assert (read == static_cast <size_t >(size));
316+ fclose (fp);
317+
318+ handle = MemoryLoadLibrary (data, size);
319+ if (handle == NULL )
320+ {
321+ _tprintf (_T (" Can't load library from memory.\n " ));
322+ result = FALSE ;
323+ goto exit;
324+ }
325+
326+ throwException = (throwExceptionProc)MemoryGetProcAddress (handle, " throwException" );
327+ if (!throwException) {
328+ _tprintf (_T (" MemoryGetProcAddress(\" throwException\" ) returned NULL\n " ));
329+ result = FALSE ;
330+ goto exit;
331+ }
332+
333+ try {
334+ throwException ();
335+ _tprintf (_T (" Should have caught exception.\n " ));
336+ result = FALSE ;
337+ goto exit;
338+ } catch (int e) {
339+ if (e != 42 ) {
340+ _tprintf (_T (" Should have caught exception 42, got %d instead\n " ), e);
341+ result = FALSE ;
342+ goto exit;
343+ }
344+ }
345+
346+ exit:
347+ MemoryFreeLibrary (handle);
348+ free (data);
349+ return result;
350+ }
351+ #endif
352+
286353int main (int argc, char * argv[])
287354{
288355 if (argc < 2 ) {
@@ -298,6 +365,11 @@ int main(int argc, char* argv[])
298365 if (!LoadExportsFromMemory (argv[1 ])) {
299366 return 2 ;
300367 }
368+ #ifdef _WIN64
369+ if (!LoadExceptionsFromMemory (argv[1 ])) {
370+ return 2 ;
371+ }
372+ #endif
301373 }
302374
303375 return 0 ;
0 commit comments