简体   繁体   English

使用Loadlibrary(“ cmd.exe”)但不起作用

[英]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: 众所周知,当我们启动CMD.exe时,它将出现一个控制台窗口,并以如下行开头:

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: 但是当我在VS和我的代码中创建Windows控制台项目时,如下所示:

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) 如我所料,我可以在进程中加载​​此PE(Windows可执行格式)文件,因此不必启动新的cmd.exe并将其stdIO重定向到启动cmd.exe的进程。(我知道使用exe的Loadlibrary文件可以在不创建新进程的情况下在调用进程中启动exe),为什么Loadlibrary不起作用?(在控制台窗口中未显示任何单词)

(i know Loadlibrary with an exe file could start a exe in calling process without creating a new process) (我知道带有exe文件的Loadlibrary可以在调用进程中启动exe,而无需创建新进程)

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. 如果使用LOAD_LIBRARY_AS_DATAFILE标志,则可以将EXE文件的名称传递给LoadLibraryEx ,以访问其资源,但LoadLibrary既不在EXE中运行代码,也不准备运行代码。

The entry point for an EXE is designed for having its own process. EXE的入口点旨在具有自己的进程。 (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). (我说的是真正的入口点,通常由语言支持库提供。它的名称可能像wmainCRT及其地址,而不是用户提供的main()的地址,出现在PE标头中) 。 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. 通常,它通过调用ExitProcess()退出,这会对主机EXE产生灾难性的影响,即使您确实设法将其映射到内存空间并调用它。

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. 您无法通过LoadLibrary运行可执行文件。 Use CreateProcess (or one of its siblings) instead. 请改用CreateProcess(或其同级之一)。

From the LoadLibrary function docs (highlight in bold is mine): LoadLibrary函数文档中(我的黑体字突出显示):

LoadLibrary can also be used to load other executable modules. LoadLibrary也可以用于加载其他可执行模块。 For example, the function can specify an .exe file to get a handle that can be used in FindResource or LoadResource. 例如,该函数可以指定一个.exe文件来获取可在FindResource或LoadResource中使用的句柄。 However, do not use LoadLibrary to run an .exe file . 但是, 请勿使用LoadLibrary运行.exe文件 Instead, use the CreateProcess function. 而是使用CreateProcess函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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