Re: loadlibrary mfc exe

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



George VS <GeorgeVS@xxxxxxxxxxxxxxxxxxxxxxxxx> 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@xxxxxxxxx
Providenza & Boekelheide, Inc.
.



Relevant Pages

  • Re: using exe as dll
    ... > The whole problem about exe which can be used as a DLL is that system ... > when loading EXE assumes ... > that it is resource-only DLL so it doesn't need any relocation ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Editors
    ... some strip them on exe files by default. ... "preferred base address" and, yup, relocations are completely ... DLL files, though, aren't loaded into their own address space but are ... EXE's "entry-point", though, is not "special" in that it's just an ...
    (alt.lang.asm)
  • Re: DLL pass vector by value crash
    ... The prototype for testfunc is a by-value copy, so it has to make a copy of the value. ... Are you using static linking for either the .exe (if so, it probably won't work correctly, ... clearly using the shared CRT DLL for the DLL you are constructing. ... no crash occurs in either config. ...
    (microsoft.public.vc.mfc)
  • Re: DLL pass vector by value crash
    ... stack of the exe and may allocate default member element on the exe heap. ... If this is a dangerous situation, the simple app I set up calling testFunc() ... in a dll did not crash. ... msvcr90.dll!free and access the source code of free.c. ...
    (microsoft.public.vc.mfc)
  • Re: Using same interfaces for in-proc vs. out-proc
    ... for each server) might work better after all. ... TLB,>you reference it in VB and gain access to all ... both EXE and DLL versions must use the same source code; ...
    (microsoft.public.vc.atl)