From: George VS on
Hi group,

I am trying to load MFC exe module as dynamic link library and call an
exported function from it:
LoadLibrary("myapp1.exe")
Myapp1Test();
The exe is loaded successfully and the function is entered,
but whichever function is called exception raises.
It seems as virtual addresses in the loaded exe mess up with loading
application;

Is there any standard/documented way to load exe as DLL?
How is the CRT initialized in this manner?

Thanks,
George VS.



From: Vinzenz Feenstra on
George VS schrieb:
> Hi group,
Hi George


> I am trying to load MFC exe module as dynamic link library and call an
> exported function from it:
> LoadLibrary("myapp1.exe")
> Myapp1Test();

Did you load the adress of the exported function? It seems that you're
using a header file and call the function.

e.g. you Function is void Myapp1Test();

Then you will have to get the address of the function by GetProcAdress:

HMODULE hmod = LoadLibrary("myapp1.exe");
if(hmod){
typedef void (*MyFunc_t)();
MyFunc_t pMyapp1Test =
reinterpret_cast<MyFunc_t>(GetProcAdress(hmod,"Myapp1Test"));
if(pMyapp1Test){
pMyapp1Test(); // now you can call the function
}
}


--
Regards,
Vinzenz Feenstra

And now visit my WeBlog < http://blog.evilissimo.net > ;)
From: George VS on
hi,

the import is successful either implicit (_declspec(dllimport)) and explicit
(GetProcAdress). The function is being entered correctly and exited correctly
if no code inside (that is no stack or pfn add problems), but the addresses
of called functions are inproper (see exerpt of disasembly print, watch for
"test" literal address is unproper, also call to void instead MessageBox).

If FIXED base addres is set in linkiing myapp2.exe "test" literals and
__imp__MessageBoxA are properly resolved, but calling MessageBox jumps to
void;

MYAPP2API void Myapp2Test()
{

MessageBox(0, "test", "test", 0);
00911050 push 0
00911052 push offset CWnd::CreateEx+4 (4023F8h)
00911057 push offset CWnd::CreateEx+4 (4023F8h)
0091105C push 0
0091105E call dword ptr [CWnd::CreateControlSite+2 (4022D0h)]
}
00911064 ret

and here it is with BASE set:

MYAPP2API void Myapp2Test()
{

MessageBox(0, "test", "test", 0);
00911050 push 0
00911052 push offset string "test" (9123F8h)
00911057 push offset string "test" (9123F8h)
0091105C push 0
0091105E call dword ptr [__imp__MessageBoxA(a)16 (9122D0h)]
}
00911064 ret

it seems ok, but calling __imp__MessageBoxA goes to

00002F28 ???


It seems exe is not relocated during load (1), and __imp__xxx are not
addressed correctly?
Also I am affraid CRT_INIT() is not being called?


Regards,
George VS.


"Vinzenz Feenstra" wrote:

> George VS schrieb:
> > Hi group,
> Hi George
>
>
> > I am trying to load MFC exe module as dynamic link library and call an
> > exported function from it:
> > LoadLibrary("myapp1.exe")
> > Myapp1Test();
>
> Did you load the adress of the exported function? It seems that you're
> using a header file and call the function.
>
> e.g. you Function is void Myapp1Test();
>
> Then you will have to get the address of the function by GetProcAdress:
>
> HMODULE hmod = LoadLibrary("myapp1.exe");
> if(hmod){
> typedef void (*MyFunc_t)();
> MyFunc_t pMyapp1Test =
> reinterpret_cast<MyFunc_t>(GetProcAdress(hmod,"Myapp1Test"));
> if(pMyapp1Test){
> pMyapp1Test(); // now you can call the function
> }
> }
>
>
> --
> Regards,
> Vinzenz Feenstra
>
> And now visit my WeBlog < http://blog.evilissimo.net > ;)
>
From: Tim Roberts on
George VS <GeorgeVS(a)discussions.microsoft.com> wrote:
>
>I am trying to load MFC exe module as dynamic link library and call an
>exported function from it:
> LoadLibrary("myapp1.exe")
> Myapp1Test();
>The exe is loaded successfully and the function is entered,
>but whichever function is called exception raises.
>It seems as virtual addresses in the loaded exe mess up with loading
>application;

That's correct. You can load an EXE with LoadLibrary, but it will be
loaded as a chunk of inanimate data. It will not be relocated or processed
like a DLL.

Think about what it would mean otherwise. When you call LoadLibrary, the
loader calls the function at the initial transfer address. For a DLL, that
means the DllEntryPoint/DllMain function gets called. For an EXE, that
means calling mainCRTstartup, which calls the "main" function. That means
that LoadLibrary("myapp1.exe") would end up running the whole application.

>Is there any standard/documented way to load exe as DLL?

No. You can do the relocation yourself, but you will only end up hurting
yourself. The right thing to do is to extract the desired function into a
separate DLL and call it from both places.
--
- Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.