Franck M

New Member
Joined
Apr 10, 2012
Messages
2
I have got an issue with Windows 7 Sp1 (v 6.1.7601).

This code display a console window with "dir" result. It work well on Windows 7 but not with SP1.

BOOL RunCommand(LPCTSTR szCommand)
{
TCHAR szComSpec[MAX_PATH];
TCHAR szCmd[2048];
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = { 0 };
SECURITY_ATTRIBUTES sa;
int err;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = true;

sprintf(szCmd, "/c %s", szCommand);
GetEnvironmentVariable(TEXT("COMSPEC"), szComSpec, MAX_PATH);
if ( CreateProcess(szComSpec, szCmd, &sa, NULL, TRUE, NULL, NULL, NULL, &si, &pi) )
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return TRUE;
}
else
{
err = GetLastError();
}

return FALSE;
}

void main()
{
if (!RunCommand("dir |more"))
Application->MessageBoxA("CreatProcess Error", "Error", MB_OK);
}

On windows 7 SP1, no console is displayed and the process close automatically without any error message.

Is somebody can help me ?
Thank's in advance.
 

Solution
The issue you are encountering with the code snippet you provided seems related to how the command is being executed on Windows 7 SP1. Let's break down the code and potential areas that might be causing this behavior:
  1. CreateProcess Parameters:
    • The CreateProcess function creates a new process and its primary thread. It's important to ensure that the parameters passed to CreateProcess are set correctly.
    • In your code, the command /c is used before szCommand to run the command. This indicates to the cmd.exe that it should run the command and then terminate.
    []Error Handling:
    • You are obtaining the last error using GetLastError() if CreateProcess fails...
The issue you are encountering with the code snippet you provided seems related to how the command is being executed on Windows 7 SP1. Let's break down the code and potential areas that might be causing this behavior:
  1. CreateProcess Parameters:
    • The CreateProcess function creates a new process and its primary thread. It's important to ensure that the parameters passed to CreateProcess are set correctly.
    • In your code, the command /c is used before szCommand to run the command. This indicates to the cmd.exe that it should run the command and then terminate.
    []Error Handling:
    • You are obtaining the last error using GetLastError() if CreateProcess fails, but you're not handling the error or displaying the error message, which might provide insights into why the process is not running as expected.
    [
    ]Environment Variables:
    • You are fetching the COMSPEC environment variable to get the path to cmd.exe. Ensure that this variable is correctly set and points to the right location.
    []Security Attributes:
    • You are using a SECURITY_ATTRIBUTES struct sa, but the way it's being initialized might cause issues.
    • It's possible that the security attributes might be inherited and causing unexpected behavior.
    [
    ]Execution Flow:
    • The code seems to be designed to pop up an error message box if RunCommand fails. Since you're not seeing this message box, it's likely that the issue lies within the RunCommand function or the command execution itself. To troubleshoot this issue:
      • Check the value of szComSpec to ensure it contains the correct path to cmd.exe.
      • Verify that the command dir |more can be executed successfully in the command prompt.
      • Consider adding more detailed error handling to check for any specific error codes returned by CreateProcess.
      By addressing these points and potentially making adjustments to how the command is executed and how errors are handled, you might resolve the issue you're facing with running commands on Windows 7 SP1.
 

Solution
Back
Top