Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Note:- All command are working fine with OSCALL function in compiler VC12

with complier VC14

exe name is RemDicomNodes.exe

CMD command prompt:-

RemDicomNodes 5 6 1 2 3 10 8 9

RemDicomNodes.exe 1 A

RemDicomNodes 2 B B 0 localhost 1 "BE" 104 3 1 7 7 60 0 "" 2 0 0 0 0 "" 3 0 0 0 0 "" 0 6 1 2 3 10 8 9 0 0 0 0 NoConversion 0 0 0 0 1 0 0 1 BE 0 0 0 IPv4 0 0 10 0 1 0

Above all commands are working fine and showing proper output on CMD command prompt

With OSCAll function:-

RemDicomNodes 5 6 1 2 3 10 8 9

#(Working fine i am getting output in variable "outputbuffer" which i am using inside readfile API)

RemDicomNodes.exe 1 A

#(Working fine i am getting output in variable "outputbuffer" which i am using inside readfile API)

RemDicomNodes.exe 2 B B 0 localhost 1 """BE"""" " 104 3 1 7 7 60 0 " """""" " 2 0 0 0 0 " """""" " 3 0 0 0 0 " """"""" 0 6 1 2 3 10 8 9 0 0 0 0 NoConversion 0 0 0 0 1 0 0 0 AE 0 0 0 IPv4 0 0 10 0 1 0"

#Problem(that any arguments we are send but it is not working, i am not getting output in variable "outputbuffer" which i am using inside readfile API it is not printing anything )

This is the function which i am using to run my exe


What I have tried:

<pre>BOOL MagicWatchComProc::OSCall(CString i_cmd, CString& a_output, DWORD& a_exitCode, long i_timeOut)

{
    STARTUPINFO      aStartupInfo;
    PROCESS_INFORMATION aProcessInfo;
    HANDLE               hReadHandle    = NULL;
    HANDLE               hWriteHandle   = NULL;
    HANDLE               hErrorHandle   = NULL;
    DWORD                dwBytesRead    = 0;
    SECURITY_ATTRIBUTES sa             = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
    //time_t l_timeOut = 300000; //5 min.
    
    //reset output string
    a_output = "";
    
    DEBUG_TRACE(_T("OSCall: launching ") + i_cmd);

    if (!i_cmd)
    {
        DEBUG_TRACE(_T("OSCall: Error: empty command\n"));
        return false;
    }
    
    //reset errors
    SetLastError(0);
    
    // Initialize process startup structure
    FillMemory(&aStartupInfo, sizeof(aStartupInfo), 0);
    //GetStartupInfo(&aStartupInfo);
    aStartupInfo.cb       = sizeof(aStartupInfo);
    aStartupInfo.dwFlags      = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    aStartupInfo.wShowWindow = SW_HIDE;
    
    // Create pipe that will transfer the process output to our buffer
    if(!CreatePipe(&hReadHandle, &hWriteHandle, &sa, 0))
    {
        DEBUG_TRACE(_T("OSCall: Error: Pipe creation\n"));
        return false;
    }
    // Set process' stdout to our pipe
    aStartupInfo.hStdOutput = hWriteHandle;
    
    // We are going to duplicate our pipe's write handle
    // and pass it as stderr to create process.  The idea
    // is that some processes have been known to close
    // stderr which would also close stdout if we passed
    // the same handle.  Therefore we make a copy of stdout's
    // pipe handle.
    
    if (!DuplicateHandle( GetCurrentProcess(), hWriteHandle, GetCurrentProcess(), &hErrorHandle, 0, TRUE, DUPLICATE_SAME_ACCESS ))
    {
        CloseHandle(hReadHandle);
        CloseHandle(hWriteHandle);
        DEBUG_TRACE(_T("OSCall: Error: duplicate handle\n"));
        return false;
    }
    aStartupInfo.hStdError = hErrorHandle;
    
    // Check input parameter
    TCHAR l_inputCommand[2048];
    
    _tcscpy(l_inputCommand, i_cmd);
    
    // Create process of service program
    if(!CreateProcess( NULL, l_inputCommand, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &aStartupInfo, &aProcessInfo ))
    {
        CloseHandle(hReadHandle);
        CloseHandle(hWriteHandle);
        CloseHandle(hErrorHandle);
        DEBUG_TRACE(_T("OSCall: Error: could not create process\n"));
        return false;
    }
    
    // The process is alive now and has inherited the environment, now
    // we can release the critical section
    
    // Close the write end of our pipe (both copies)
    // so it will die when the child process terminates
    CloseHandle(hWriteHandle);
    CloseHandle(hErrorHandle);
    
    // We close the handle of the process in order to prevent memory leaks when
    // the process terminates.
    //CloseHandle(aProcessInfo.hThread);
    
    // Allocate memory for output buffer
    DWORD   dwAvailableOutput = 16;
    CHAR    outputbuffer[18];
    
    //CString strPipeName;
    //DWORD dwSize = 0;
    
    //GetNamedPipeHandleState( hReadHandle, NULL, NULL, NULL, NULL, strPipeName.GetBuffer(1), dwSize );
    //WaitNamedPipe( strPipeName, 100 );
    
    // -> Read output from CGI program
    time_t l_time;
    time_t l_startTime;
    time(&l_startTime);
    do
    {
        time(&l_time);
    
        if( (l_time - l_startTime) > i_timeOut)
            {
                SetLastError(WAIT_TIMEOUT);
                break;
            }
        
        if( ReadFile( hReadHandle, outputbuffer, dwAvailableOutput, &dwBytesRead, NULL ) )
        {
            outputbuffer[dwBytesRead] = 0;  // null terminate
            a_output += outputbuffer;
        }
    }
    //We are done
    while ( GetLastError() != ERROR_BROKEN_PIPE );
    
    //reset ERROR_BROKEN_PIPE error
    if(GetLastError() == ERROR_BROKEN_PIPE)
        SetLastError(0);
    
    //DEBUG_TRACE(_T("OSCall: command output: ") + a_output);
    
    //CloseHandle(hErrorHandle);
    CloseHandle(hReadHandle);
    
    if( !GetLastError() )
    {
        return true;
    }
    else
    {
        DEBUG_TRACE(_T("OSCall: Error: cmd command failed. Error code: ")+ i2cs(GetLastError()) +_T("; ExitCode: ")+ i2cs(a_exitCode) +_T("."));
        return false;
    }
}

int main()
{
    DWORD   dwExitCode = 0;
    long l_timeOut = 60000; //ms = 10 min
    BOOL    bRet    = FALSE;
    CString strInstallLmutil(_T("cmd.exe /C "));
    CString strLmutilInstallFile(_T("remdicomnodes.exe"));
    CString strPathLmutilInstall;
    strPathLmutilInstall.Format(_T("%s\\bin\\%s"),strtemp,strLmutilInstallFile);    
    strInstallOption = _T("");
    strInstallOption = _T(" 2 B B 0 localhost 1 \"""BE""\"" " 104 3 1 7 7 60 0 " "\"""\"" " 2 0 0 0 0 " "\"""\"" " 3 0 0 0 0 " "\"""\"""  0 6 1 2 3 10 8 9  0 0 0 0 NoConversion 0 0  0  0  1 0 0 0 AE 0 0 0 IPv4 0 0 10 0 1 0");
    bRet = OSCall ( cmd, l_commandOutput, dwExitCode, l_timeOut);
    SAM_TRACE1("OSCall: bRet value: :%d ",bRet);
    if( bRet == FALSE )
    {
        SAM_TRACE0("remdicomnodes.exe execution failed \n");
    }
    return 0;   
}
Posted
Comments
Richard MacCutchan 20-Nov-20 7:04am    
"but it is not working"
That statement tells us nothing I am afraid. Please explain exactly what you mean, what output you get and what is wrong with it. Also indicate where in the code the problem occurs.
Member 14500061 20-Nov-20 7:50am    
@Richard MacCutchan
as i explain with these below parameter's i can see the output on CMD Cammand prompt but when i use this function OSCall, readfile API Buffer is empty,console output is not there


RemDicomNodes.exe 2 B B 0 localhost 1 """BE"""" " 104 3 1 7 7 60 0 " """""" " 2 0 0 0 0 " """""" " 3 0 0 0 0 " """"""" 0 6 1 2 3 10 8 9 0 0 0 0 NoConversion 0 0 0 0 1 0 0 0 AE 0 0 0 IPv4 0 0 10 0 1 0"


i am suspecting this part of code
variable "outputbuffer" is empty with above command

if( ReadFile( hReadHandle, outputbuffer, dwAvailableOutput, &dwBytesRead, NULL ) )
{
outputbuffer[dwBytesRead] = 0; // null terminate
a_output += outputbuffer;
}
Richard MacCutchan 20-Nov-20 8:15am    
You do not check the result of the call to ReadFile, or what value is returned in dwBytesRead. Do not assume that system calls succeed, always check their status.
Member 14500061 23-Nov-20 21:40pm    
@Richard MacCutchan
i am not getting what improvement is require in this code
are you telling i need to check ReadFile status?
is there any code improvement require here ,if yes then can you plz help me out here?
Richard MacCutchan 24-Nov-20 3:40am    
Yes, you must check the results of any system call. Do not assume that because you ask for 100 characters the system gives you 100 characters. As to any other improvement, it is difficult to figure out what this code is supposed to be doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900