911 URGENT HELP PLS: LoadLibrary EXE problem
problem: use LoadLibrary to load an EXE and call the EXE entry point manually - got access violation
description: when i call the entrypoint, the target exe program fails at line 199 of ...\crt\src\crtexe.c (c-runtime source code) with an access violation in ntdll.dll. i've attached the code segment i used below as well as the line of c-runtime code that failed... looking forward to your expert advice...
thanks so much!!!
peter
====== loadlibrary and calling entry point of exe =====
typedef DWORD (* INITPROC)();
...
char exe[] = "C:\\Documents and
Settings\\Administrator\\Desktop\\shareware\\test\\imghlptst\\exe.exe";
LOADED_IMAGE li;
if ( MapAndLoad( exe, 0, &li, FALSE, TRUE) )
{
DWORD p;
HMODULE hp = LoadLibrary(exe);
p = DWORD(hp);
p += li.FileHeader->OptionalHeader.AddressOfEntryPoint;
INITPROC proc = (INITPROC)p;
(*proc)();
...
============================================
====== c-runtime location that fails =================
#ifdef WPRFLAG
void wmainCRTStartup(
#else /* WPRFLAG */
void mainCRTStartup(
#endif /* WPRFLAG */
#endif /* _WINMAIN_ */
void
)
{
int argc; /* three standard arguments to main */
_TSCHAR **argv;
_TSCHAR **envp;
int mainret;
#ifdef _WINMAIN_
_TUCHAR *lpszCommandLine;
STARTUPINFO StartupInfo;
#endif /* _WINMAIN_ */
_startupinfo startinfo;
. . .
__try {
/*
* Set __app_type properly
*/
#ifdef _WINMAIN_
__set_app_type(_GUI_APP);// <----------------- FAILED
#else /* _WINMAIN_ */
__set_app_type(_CONSOLE_APP);
#endif /* _WINMAIN_ */
================================
thanks!!!
peter
Re: 911 URGENT HELP PLS: LoadLibrary EXE problem
Hi,
To run a process, they want you to use CreateProcess(). To call an exported function from a module, they want you to use LoadLibrary and then GetProcAddress(). If you just want to run a program, then use CreateProcess(). If you just want to execute an exported function, then use LoadLibrary followed by GetProcAddress().
You can use the utility dumpbin [it comes with Visual C++] to show the exports of a dll or exe:
dumpbin /exports somemodule.dll
--Paul
Re: 911 URGENT HELP PLS: LoadLibrary EXE problem
thanks paul, i know what you mean... but i am trying to run a EXE from the context of another EXE... that's why i am not using createprocess..
thanks!