简体   繁体   中英

Using Loadlibrary(“cmd.exe”) but not work

as we all know when we start a CMD.exe it will appear a console window and start with lines like:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\hey>

but when i crate a windows console project in VS and my code like this:

int _tmain(int argc, _TCHAR* argv[])
{
    auto h = LoadLibrary(__TEXT("cmd.exe"));
    Sleep(99999);
}

just turns out a black window.no lines out!

as i expect,i can load this PE(windows executable format) file in my process so i do not have to start a new cmd.exe and redirect its stdIO to the process which start cmd.exe.(i know Loadlibrary with an exe file could start a exe in calling process without creating a new process) and why is Loadlibrary not working?(it did not appear any words in the console window)

(i know Loadlibrary with an exe file could start a exe in calling process without creating a new process)

No, it can't.

You can pass the name of an EXE file to LoadLibraryEx if you use the LOAD_LIBRARY_AS_DATAFILE flag, in order to access its resources, but LoadLibrary neither runs the code in an EXE nor prepares the code for being run.

The entry point for an EXE is designed for having its own process. (I'm talking about the real entry point, which is usually provided by a language support library. It may have a name such as wmainCRT and its address, not the address of user-provided main() , appears in the PE header). Typically it exits by calling ExitProcess() , which will have catastrophic effects on your host EXE even if you do manage to map it into your memory space and call it.

The requirements for the entry point of a dynamically loadable library and an executable file are very, very different.

You can't run an executable via LoadLibrary. Use CreateProcess (or one of its siblings) instead.

From the LoadLibrary function docs (highlight in bold is mine):

LoadLibrary can also be used to load other executable modules. For example, the function can specify an .exe file to get a handle that can be used in FindResource or LoadResource. However, do not use LoadLibrary to run an .exe file . Instead, use the CreateProcess function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM