我正在寻找一种有效的方法来查找给定的应用程序(比如app.exe)是否是单个实例?我想到了以下这些解决方案:
但我不相信上述两种解决方案.在Windows中还有其他有效的方法吗?
请注意,我不会杀死或对该应用程序的已运行(如果有)实例进行任何修改.
我试图弄清楚如何从我用CreateProcessW创建的过程中读取标准输出/错误.我查看了文档,谷歌搜索并搜索了这个列表,但我找不到好的指针/样本:)
这是我到目前为止所提出的(它在Windows上运行正常,它是我的java代码中的相关代码段):
Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
Kernel32.StartupInfo startupInfo = new Kernel32.StartupInfo();
Kernel32.ProcessInfo processInformation = new Kernel32.ProcessInfo();
if (!kernel32.CreateProcessW(null, new WString(command), null, null, false,
DETACHED_PROCESS, null, new WString(dir.getAbsolutePath()), startupInfo,
processInformation)) {
throw new IOException("Could not start process. Errno: " +
kernel32.GetLastError());
}
kernel32.CloseHandle(processInformation.hProcess);
kernel32.CloseHandle(processInformation.hThread);
Run Code Online (Sandbox Code Playgroud)
那么......我如何从该过程中获取输出?任何人都已经这样做并且关心分享样品吗?
谢谢你们提前帮助.
以下代码CreateProcess用于运行带有环境变量的命令.在这里,它试图运行notepad %APPDATA%\test.txt.
如果我notepad %APPDATA%\test.txt直接在其中运行Windows' CMD,%APPDATA%将被扩展.但是,执行时无法扩展环境变量CreateProcess.你能帮忙评论原因和解决方法吗?任何评论将不胜感激!

program TestConsole2;
{$APPTYPE CONSOLE}
uses
Windows, SysUtils;
var
I: Integer;
ProgramName: String;
StartInfo : TStartupInfo;
ProcInfo : TProcessInformation;
CreateOK : Boolean;
begin
try
FillChar(StartInfo, SizeOf(StartInfo), #0);
FillChar(ProcInfo, SizeOf(ProcInfo), #0);
StartInfo.cb := SizeOf(StartInfo);
ProgramName := 'NOTEPAD %APPDATA%\test.txt';
CreateOK := CreateProcess(
nil, PChar(ProgramName), nil, nil, True, 0, nil, nil, StartInfo, ProcInfo);
if CreateOK then WaitForSingleObject(ProcInfo.hProcess, INFINITE);
Readln(ProgramName);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end; …Run Code Online (Sandbox Code Playgroud) 我在使用TerminateProcess. 我调用这个函数,进程仍然存在(在任务管理器中)。这段代码被多次调用,多次启动相同的 program.exe,这些进程在任务管理器中,我认为这并不好。实际上一直在创建两个进程:program.exe和conhost.exe。
我真的很感激任何帮助。
这是代码:
STARTUPINFO childProcStartupInfo;
memset( &childProcStartupInfo, 0, sizeof(childProcStartupInfo));
childProcStartupInfo.cb = sizeof(childProcStartupInfo);
childProcStartupInfo.hStdInput = hFromParent; // stdin
childProcStartupInfo.hStdOutput = hToParent; // stdout
childProcStartupInfo.hStdError = hToParentDup; // stderr
childProcStartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
childProcStartupInfo.wShowWindow = SW_HIDE;
PROCESS_INFORMATION childProcInfo; /* for CreateProcess call */
bOk = CreateProcess(
NULL, // filename
pCmdLine, // full command line for child
NULL, // process security descriptor */
NULL, // thread security descriptor */
TRUE, // inherit handles? Also use if STARTF_USESTDHANDLES */ …Run Code Online (Sandbox Code Playgroud) 我正在寻找Delphi中CreateProcess的快速替代方法来在exe中执行某些计算,包括XML中的几个返回值.目前,我正在使用某些参数调用C#-exe.其中一个电话约需要.0.5秒-这是方式昂贵,因为此exe需要调用几百次(unfortunatley反复调用,即多线程不会加快工作).
我当前的代码看起来像这样(找到解决方案来获取StackOverflow上的exe somwhere的控制台输出).
IsExecutable := CreateProcess(
nil,
PChar(WorkDir + Exe + CommandLine),
nil,
nil,
True,
HIGH_PRIORITY_CLASS,
nil,
nil,
StartupInfo,
ProcessInformation);
CloseHandle(StdOutPipeWrite);
if IsExecutable then
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
Result := Result + Buffer;
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
finally
CloseHandle(ProcessInformation.hThread);
CloseHandle(ProcessInformation.hProcess);
end
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我不是很好的德尔福 - 实际上,我觉得"我不知道我在做什么"的狗咪模因......
我正在尝试使用CreateProcess函数执行dos命令:
LPWSTR cmd=(LPWSTR)QString("C:\\windows\\system32\\cmd.exe subst " + DLetter+" \""+mountPath+"\"").utf16();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if ( CreateProcessW(0, // Application name
cmd, // Application arguments
NULL,
NULL,
TRUE,
0,
NULL,
L"C:\\windows\\system32", // Working directory
&si,
&pi) == TRUE)
{ ...
Run Code Online (Sandbox Code Playgroud)
它给出了最后一个错误3 = ERROR_PATH_NOT_FOUND,当我将应用程序路径"C:\\windows\\system32\\cmd.exe"与命令分开时,它显示控制台而不执行我的subst命令.
任何帮助将不胜感激.
我正在使用 CreateProcess 打开一个窗口,但在理解 SetWinEventHook 时遇到了很多麻烦。
在调用函数中,我有:
HWINEVENTHOOK hook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT );
BOOL result = CreateProcess(0, arguments,
NULL, NULL, FALSE, 0, NULL,
NULL, &StartupInfo, &ProcessInfo)
if (hook) {
UnhookWinEvent(hook);
}
Run Code Online (Sandbox Code Playgroud)
create Process 顺利启动,但没有调用链接到 SetWinEventHook 的 WinEventProc 函数。为了让 WinEventProc 被调用,我尝试过这样的事情:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0);
Run Code Online (Sandbox Code Playgroud)
在 createProcess 调用之后,但我不知道如何结束 while 循环,以便它持续运行。
我已经做了很多阅读,但我不明白如何使用 SetWinEventHook 来捕获由 CreateProcess 启动的进程。任何帮助表示赞赏!
我已经阅读了一些CreateProcess不能从DllMain函数中调用的来源。
创建过程:
不要从 DllMain 函数调用 CreateProcess。这会导致应用程序停止响应。
永远不要从 DllMain 中执行以下任务: 调用 CreateProcess。创建一个进程可以加载另一个 DLL。
这是为什么?它指出它会导致应用程序停止响应,但这只是一种症状。真正的原因是什么?
我问的原因是我尝试从一个DllMain函数创建一个进程,它看起来工作得很好。
我想执行 shell 命令来更新我的处理器 ATMega 2560 的固件,如下所示:
avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"file.cpp.hex\":a
Run Code Online (Sandbox Code Playgroud)
我可以通过 ShellExecute() 函数来做到这一点:
ShellExecute(0, L"open", L"cmd.exe", L"/C avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"file.cpp.hex\":a > log.txt", 0, SW_HIDE);
Run Code Online (Sandbox Code Playgroud)
但我想重定向输出缓冲区,所以我想我应该使用 CreateProcess() 函数。我尝试过这个,但没有成功。
CreateProcess(NULL, L"cmd /C avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"file.cpp.hex\":a", NULL, NULL, 0, 0, NULL, NULL, NULL, NULL);
Run Code Online (Sandbox Code Playgroud) 我有一个简单的(Windows)应用程序,使用CreateProcess函数启动另一个应用程序.然后通过使用EnumWindows和新创建的进程的进程ID获取正确的hwnd.
获得hwnd之后,我的应用程序的"主循环"开始了.此循环继续,直到CreateProcess启动的应用程序不再运行.
一切都运行得很好,直到我尝试使用PeekMessage来查看发送到我发起的应用程序的消息 - 似乎我的应用程序没有识别任何消息,但启动的程序(通过CreateProcess)正在运行像往常一样,做它应该做的一切......
我想要实现的,是删除某些消息被发送到应用程序,主要是各种F键(F1,F2..F12键),但如果可能的话,我想更改显示的菜单应用程序(我不知道我的意思是菜单的技术名称,它是你在右上角看到你点击应用程序图标的那个) - 我想添加一个小的'about'选项.
如果有人能够在我的代码中指出我做错了什么,或者为了阻止某些按键到达启动的应用程序更好的替代方案,我将非常感激.
提前谢谢了.:)
这是我目前的代码:
VOID PerformLaunch(LPWSTR lpAppName, LPTSTR lpCmdLine) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD dwLoopExitCode = NULL;
BOOL cpBool = FALSE;
BOOL finishedLoop = FALSE;
MSG message = { 0 };
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
cpBool = CreateProcess(lpAppName, lpCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!cpBool) {
MessageBox(Game_HWND, L"Could not start process!", L"Error:", MB_OK | MB_ICONERROR);
}
//-- Enumerate windows until Game_HWND && Game_Hook are not …Run Code Online (Sandbox Code Playgroud) createprocess ×10
c++ ×5
winapi ×4
windows ×3
cmd ×2
delphi ×2
dllmain ×1
exe ×1
java ×1
jna ×1
pascal ×1
peekmessage ×1
performance ×1
process ×1