我正在使用Win32 CreateProcess函数来执行对外部可执行文件的调用.可执行文件返回一个字符串
有没有办法在调用可执行文件后捕获并查询返回的字符串?如果不这样做,我可能必须将字符串写入可执行文件中的文件,并在调用完成后在调用程序中读取该字符串.这看起来很蹩脚.
我希望能够在主进程中使用CreateProcess打开GUI应用程序,并在我在主进程中创建的窗口中显示GUI.有谁知道如何实现这一目标?谢谢!
在我的Win32应用程序中,我嵌入了ffplay.exe进行视频预览.它工作得很好但每次我开始预览时光标变得"忙",即箭头+沙漏.我想避免这种情况.
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
LPCWSTR procName =(LPCWSTR)"D:\\test dir 1\\Calc.exe";
LPWSTR procArg =(LPWSTR)"blacknull";
if(CreateProcess(procName,procArg,0,0,0,CREATE_DEFAULT_ERROR_MODE,0,0,&si,&pi))
{
//do some work
}
printf( "CreateProcess failed (%d).\n", GetLastError());
system("Pause");
Run Code Online (Sandbox Code Playgroud)
它不断抛出错误(2) - > The System cannot find the file specified.
我不知道出了什么问题.我也尝试在同一个Dir中使用"Calc.exe".但它不起作用.
我正在尝试使用devcon.exe来检查各种硬件的状态.在示例中,我正在尝试检查我的SATA HBA状态,但是devcon正在抱怨它.这是代码:
int main(int argc, char** argv) {
std::string cmdLine("\"C:\\Users\\afalanga\\Documents\\Visual Studio 2010\\Projects\\PlayGround\\Debug\\devcon.exe\" status PCI\\VEN_8086^&DEV_3A22^&SUBSYS_75201462^&REV_00");
char* pCmdLine(new char[cmdLine.length() + 10]);
memset(pCmdLine, 0, cmdLine.length() + 10);
for(int i(0); i < cmdLine.length(); i++)
pCmdLine[i] = cmdLine.at(i);
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};
if(!CreateProcess(NULL, pCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
std::cout << "Create child process failed. Error code: "
<< GetLastError() << std::endl;
return 1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当上面执行时,devcon抱怨说"找不到匹配的设备".但是,如果我将该命令行从调试器复制/粘贴到我的命令提示符并按Enter键(或者当然删除调试器围绕它的所有包含引号),该命令将按预期完美地执行.
我在处理字符串时遇到了什么问题?以上是在MSDN上读取CreateProcess()文档的结果(发现不一定需要第一个参数,而cmd args根本不应该去那里).我分配10个额外字节的内存来复制字符串的原因是,在CreateProcess()函数的内部可能会改变"无论什么",而不会踩到其他内存.至少,当我这样做时,这是我的想法.
我正在开发一个应用程序,其中进程 A 的多个实例依赖于进程的单个实例 B。这个想法是进程 A 的一个实例启动进程 B,以便 A 的所有实例都可以用它。A 的实例托管在第 3 方进程中,并且可以在不可预测的时间点被拆除(通过终止进程树)。因此,进程 B 不是进程 A 的任何实例的子进程至关重要。
我尝试使用 PInvoke 调用 CreateProcess,在创建标志中指定 DetachedProcess (0x08),但这不起作用(请参阅下面的代码)。
[DllImport("kernel32.dll")]
private static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref StartupInfo lpStartupInfo, out ProcessInformation lpProcessInformation);
public Process LaunchProcess(Path executablePath, string args)
{
StartupInfo sInfo = new StartupInfo();
const uint creationFlags = (uint)(CreationFlags.CreateNoWindow | CreationFlags.DetachedProcess);
ProcessInformation pInfo;
bool success = CreateProcess(executablePath.ToString(), args, IntPtr.Zero, IntPtr.Zero, false, creationFlags, …Run Code Online (Sandbox Code Playgroud) 我们需要在delphi应用程序的命令窗口中执行ffmpeg.
我们找到了使用"ExtractShortPathName"函数保护路径的解决方案.
但是在某些计算机上我们无法获得8.3路径(HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\NtfsDisable8dot3NameCreation为2),我们想找到另一种逃避空间的方法.
这是代码:
sParameters := '"C:\Users\[...]\input.wav" -r 12.03 -f image2 -i "C:\Users\[...]\delphilm%d.png" -vf "scale=1024:704" -ab 96k -r 24 -b 2000k -pass 1 -vcodec libx264 -fpre "C:\[...]\libx264-normal.ffpreset" "C:\Users\[...]\export.mp4"';
sCommand := 'C:\Program Files\My application\utils\bin\ffmpeg.exe';
Handle := CreateProcess(nil, PChar('cmd.exe /C '+ProtectPath(sCommand)+' '+sParameters),nil, nil, True, 0, nil, nil, SI, PI);
Run Code Online (Sandbox Code Playgroud)
使用ProtectPath功能:
function ProtectPath(sCommand:Widestring):Widestring;
begin
Result := sCommand;
// get the 8.3 path
Result := ExtractShortPathName(sCommand);
// if 8.3 path is not accessible
if(Pos(' ', Result)>0)then begin
//Result := '"'+sCommand+'"'; --> do not work …Run Code Online (Sandbox Code Playgroud) 我有以下c代码,我从这个问题的第一个答案中获取,我在VS2008中使用C89进行编译,所以我对代码进行了一些更改以正常工作,它编译得很好,但它不能成功创建namedpipe后创建进程(CreateProcessA不工作)始终返回错误2并在panic函数中打印消息.
我试图在CreateProcessA中运行的程序可以在这里下载,我通常运行它并按如下方式使用它:
> C:\qhichwa>cmd.exe /c "C:\qhichwa\flookup.exe -bx C:\qhichwa\qhichwa.fst"
wasi <user writes wasi>
wasi <program responds printing wasi>
hola <user writes hola>
+ ? <program responds printing + ?>
<pres ctrl + c to terminate program>
> C:\qhichwa>
Run Code Online (Sandbox Code Playgroud)
两者之间的界限< comments >只是评论.
为了成功创建命名管道需要哪些更正?
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// name of our glorious pipe
#define PIPE_NAME L"\\\\.\\pipe\\whatever" // bloody unicode string
// exit on fatal error
void panic(const char * msg) …Run Code Online (Sandbox Code Playgroud) 我正在使用CreateProcess这种方式:
resultCreate = CreateProcess(Command, CommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
//"Command" contains the executable file to execute
//"CommandLine" contains the parameters to pass to that executable
Run Code Online (Sandbox Code Playgroud)
参数如下:
Param1: "C:\Users\myuser\Desktop\file.dll"
Param2: "file" (module name)
Param3: " " (blank)
Run Code Online (Sandbox Code Playgroud)
所以完整的命令行字符串将是:
"C:\Users\myuser\Desktop\file.dll" file " "
Run Code Online (Sandbox Code Playgroud)
CreateProcess 成功运行可执行文件并应用前两个参数,但是当到达第三个时,它会抛出错误
The specified process could not be found.
Function " " could not be called, due to " " doesn't exist in the DLL "(null)"
Run Code Online (Sandbox Code Playgroud)
如何正确传递所需的参数?
我使用 LogonUser 获取主用户令牌,然后使用 CreateProcessAsUser API 来创建进程。但我收到错误代码 6。不确定是什么问题。下面是代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LogOnUserTestWindows
{
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
class Program
{
// Define the Windows LogonUser and CloseHandle functions.
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool LogonUser(String username, String domain, IntPtr password,
int logonType, int logonProvider, ref IntPtr token);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);
private enum SW
{
SW_HIDE …Run Code Online (Sandbox Code Playgroud) createprocess ×10
winapi ×5
windows ×5
c++ ×4
delphi ×2
process ×2
busy-cursor ×1
c ×1
c# ×1
delphi-5 ×1
named-pipes ×1
null ×1
parameters ×1
string ×1