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这个程序也有这个问题,还望各位 赐教

解决方案 »

  1.   

    chDIMOF是什么东东,跟踪看一下szPath中是不是正确的
      

  2.   

    是你的路径问题The first parameter, lpApplicationName, can be NULL, in which case the executable name must be in the white space-delimited string pointed to by lpCommandLine. If the executable or path name has a space in it, there is a risk that a different executable could be run because of the way the function parses spaces. The following example is dangerous because the function will attempt to run "Program.exe", if it exists, instead of "MyApp.exe".CreateProcess(NULL, "C:\\Program Files\\MyApp", ...)
    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", ...)
      

  3.   


    /******************************************************************************
    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();强制启动程序调试(你认为
    是出错)。
      

  4.   

    我用断点调试到createProcess以后,就会谈出对话框10 Optex.exe遇到问题需要关闭,我们对此引起的不便抱歉。我想问一下yxz_lp,这个对话框是DebugBreak()引发的吗?
      

  5.   

    另外szPath中的路径应该是对的,就算我把路径szPath改成“c:\1.txt”(该文件是存在的),好像createProcess也不能打开记事本,希望大家都能赐教
      

  6.   

    有没有人运行过10-optex这个程序阿?是不是编译选项设置的有问题?
      

  7.   

    这个对话框是DebugBreak()引发的吗?//不是
    就算我把路径szPath改成“c:\1.txt”(该文件是存在的)//应该是 "CMD.exe C:\\1.txt"
      

  8.   

    更正:
    就算我把路径szPath改成“c:\1.txt”(该文件是存在的)//应该是 "notepad.exe C:\\1.txt"
      

  9.   

    非常感谢yxz_lp ,你举例"notepad.exe C:\\1.txt"是对的,但是我还有两个问题,希望你能不吝赐教
    1 用createprocess函数第二个参数命令行打开程序有什么语法或者句式(例如你举例"notepad.exe C:\\1.txt")?似乎msdn上说的不是很详细。
    2 10 Optex.exe这个程序你运行的结果有没有遇到我那种情况?为什么我运行会出错?我相信作者的程序应该是正确的,这个也是我一直想问的问题。
    希望你能够给我解答,谢谢。