views:

90

answers:

2

Hi,

Recently I was trying to add unit tests to an existing binary by creating a extra (DLLMain) entry point to an application that already has a main entry point (it is a console exe). The application seemed to compile correctly although I was unable to use it as a DLL from my python unit test framework, all attempts to use the exe as a dll failed.

Has anyone any ideas or experience in adding extra application entry point with any input as to why this would or wouldn't work?

+1  A: 

I don't know for sure, but I would guess that Windows simply refuses to load an EXE in-process and a DLL as a new process, plain and simple.

These questions appear to contain more detail:

The simplest way to get both behaviours in one executable image is to design it as a DLL, then use rundll32.exe to execute it standalone. There's no need to write your own wrapper.

Christian Hayter
+3  A: 

There are some problems which you should solve to implement what you want:

  • The exe must have relocation table (use linker switch /FIXED:NO)
  • The exe must exports at least one function - it's clear how to do this.

I recommend use DUMPBIN.EXE with no some switches (/headers, /exports and without switches) to examine the exe headers. You can compare the structure of your application with Winword.exe or outlook.exe which exports some functions.

If all this will not helps, I'll try to write a test EXE application which can be loaded as an exe and post the code here.

UPDATED: Just now verified my suggestion. It works. File Loadable.c looks like following

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

EXTERN_C int __declspec(dllexport) WINAPI Sum (int x, int y);

EXTERN_C int __declspec(dllexport) WINAPI Sum (int x, int y)
{
    return x + y;
}

int main() 
{
    printf ("2+3=%d\n", Sum(2,3));
}

The only important linker switch is /FIXED:NO which one can find in advanced part of linker settings. The program can run and produced the output "2+3=5".

Another EXE loaded the EXE as a DLL and calls Sum function:

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

typedef int (WINAPI *PFN_SUM) (int x, int y);

int main()
{
    HMODULE hModule = LoadLibrary (TEXT("C:\\Oleg\\ExeAsDll\\Loadable.exe"));
    PFN_SUM fnSum = (PFN_SUM) GetProcAddress (hModule, "_Sum@8");
    int res = fnSum (5,4);
    printf ("5+4=%d\n", res);
    return 0;
}

The program also can run and produced the output "5+4=9".

Oleg