windows核心编程中的10-optex程序,运行到如下代码段的时候就会出错 STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, chDIMOF(szPath));
CreateProcess(NULL, szPath, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi);
按照道理来说szPath是得到该模块的全路径,然后传给createprocess的lpCommandLine参数,不知道为什么会出错。
如果将szPath改成"CMD"的话,是能够打开一个控制台程序的,但是为什么改成其他的应用程序的全路径就打不开呢?
还有该书的4-processinfo这个程序也有这个问题,还望各位 赐教
PROCESS_INFORMATION pi;
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, chDIMOF(szPath));
CreateProcess(NULL, szPath, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi);
按照道理来说szPath是得到该模块的全路径,然后传给createprocess的lpCommandLine参数,不知道为什么会出错。
如果将szPath改成"CMD"的话,是能够打开一个控制台程序的,但是为什么改成其他的应用程序的全路径就打不开呢?
还有该书的4-processinfo这个程序也有这个问题,还望各位 赐教
If a malicious user were to create an application called "Program.exe" on a system, any program that incorrectly calls CreateProcess using the Program Files directory will run this application instead of the intended application.To avoid this problem, do not pass NULL for lpApplicationName. If you do pass NULL for lpApplicationName, use quotation s around the executable path in lpCommandLine, as shown in the example below.CreateProcess(NULL, "\"C:\\Program Files\\MyApp.exe\" -L -S", ...)
/******************************************************************************
Module name: OptexTest.cpp
Written by: Jeffrey Richter
******************************************************************************/
#include "..\CmnHdr.h" /* See Appendix A. */
#include <tchar.h>
#include <process.h>
#include "Optex.h"
///////////////////////////////////////////////////////////////////////////////
DWORD WINAPI SecondFunc(PVOID pvParam) { COptex& optex = * (COptex*) pvParam; // The primary thread should own the optex here, this should fail
chVERIFY(optex.TryEnter() == FALSE); // Wait for the primary thread to give up the optex
optex.Enter(); optex.Enter(); // Test recursive ownership
chMB("Secondary: Entered the optex\n(Dismiss me 2nd)"); // Leave the optex but we still own it once
optex.Leave();
chMB("Secondary: The primary thread should not display a box yet");
optex.Leave(); // The primary thread should be able to run now return(0);
}
///////////////////////////////////////////////////////////////////////////////
VOID FirstFunc(BOOL fLocal, COptex& optex) { optex.Enter(); // Gain ownership of the optex // Since this thread owns the optex, we should be able to get it again
chVERIFY(optex.TryEnter()); HANDLE hOtherThread = NULL;
if (fLocal) {
// Spawn a secondary thread for testing purposes (pass it the optex) DWORD dwThreadId;
hOtherThread = chBEGINTHREADEX(NULL, 0,
SecondFunc, (PVOID) &optex, 0, &dwThreadId); } else {
// Spawn a secondary process for testing purposes
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, chDIMOF(szPath));
CreateProcess(NULL, szPath, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi);
hOtherThread = pi.hProcess;
CloseHandle(pi.hThread);
} // Wait for the second thread to own the optex
chMB("Primary: Hit OK to give optex to secondary"); // Let the second thread gain ownership of the optex
optex.Leave();
optex.Leave(); // Wait for the second thread to own the optex
chMB("Primary: Hit OK to wait for the optex\n(Dismiss me 1st)"); optex.Enter(); // Try to gain ownership back WaitForSingleObject(hOtherThread, INFINITE);
CloseHandle(hOtherThread);
optex.Leave();
}
int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE, PTSTR pszCmdLine, int) { // This event is just used to determine which instance this is.
HANDLE hevt = CreateEvent(NULL, FALSE, FALSE, TEXT("OptexTest")); if (GetLastError() != ERROR_ALREADY_EXISTS) { // This is the first instance of this test application // First, let's test the single-process optex
COptex optexSingle; // Create a single-process optex
FirstFunc(TRUE, optexSingle); // Now, let's test the cross-process optex
COptex optexCross("CrossOptexTest"); // Create a cross-process optex
FirstFunc(FALSE, optexCross); } else { // This is the second instance of this test application
DebugBreak(); // Force debugger connection for tracing // Test the cross-process optex
COptex optexCross("CrossOptexTest"); // Create a cross-process optex
SecondFunc((PVOID) &optexCross);
} CloseHandle(hevt);
return(0);
}哪是什么错误,当调用 COptex optexCross("CrossOptexTest"); // Create a cross-process optex
SecondFunc((PVOID) &optexCross);时,进入 FirstFunc函数的else分支再次调用10 Optex.exe程序,而
这次调用10 Optex.exe程序,就会进入_tWinMain的else分支,进而调用 DebugBreak();强制启动程序调试(你认为
是出错)。
就算我把路径szPath改成“c:\1.txt”(该文件是存在的)//应该是 "CMD.exe C:\\1.txt"
就算我把路径szPath改成“c:\1.txt”(该文件是存在的)//应该是 "notepad.exe C:\\1.txt"
1 用createprocess函数第二个参数命令行打开程序有什么语法或者句式(例如你举例"notepad.exe C:\\1.txt")?似乎msdn上说的不是很详细。
2 10 Optex.exe这个程序你运行的结果有没有遇到我那种情况?为什么我运行会出错?我相信作者的程序应该是正确的,这个也是我一直想问的问题。
希望你能够给我解答,谢谢。