Click here to Skip to main content
16,005,121 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
Maxwell Chen24-Aug-07 8:39
Maxwell Chen24-Aug-07 8:39 
GeneralRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
John Oliviers24-Aug-07 8:48
John Oliviers24-Aug-07 8:48 
GeneralRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
Maxwell Chen24-Aug-07 9:07
Maxwell Chen24-Aug-07 9:07 
GeneralRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
John Oliviers24-Aug-07 9:16
John Oliviers24-Aug-07 9:16 
GeneralRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
Maxwell Chen24-Aug-07 9:51
Maxwell Chen24-Aug-07 9:51 
QuestionRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
John Oliviers29-Aug-07 9:26
John Oliviers29-Aug-07 9:26 
GeneralRe: stackwalk64 compilation error on 64 bit windows - Cannot convert parameter from 'int' to 'HANDLE' Pin
Maxwell Chen29-Aug-07 18:36
Maxwell Chen29-Aug-07 18:36 
QuestionHow to pass data to child process using anonymous pipes Pin
Visweswara Koduri24-Aug-07 5:05
Visweswara Koduri24-Aug-07 5:05 
Hi,
I have a win 32 console application which will spawn a telnet window (Telnet.exe -l username) by using CreateProcess() API call and then pass the password (read from a file by the parent process) to the child process (Telnet window) using anonymous pipes. I have tried with the following code which i got from MSDN and partially modified , but could not get the result.
Please help me how can i pass data to telnet window so that it will logon automatically.

#include <stdio.h>
#include <windows.h>

#define BUFSIZE 4096

HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup,
hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup,
hInputFile, hStdout, hStdInput,hProcess;

PROCESS_INFORMATION piProcInfo;
BOOL CreateChildProcess(VOID);
VOID WriteToPipe(VOID);
VOID ReadFromPipe(VOID);
VOID ErrorExit(LPTSTR);
VOID ErrMsg(LPTSTR, BOOL);

DWORD main(int argc, char *argv[])
{
SECURITY_ATTRIBUTES saAttr;
BOOL fSuccess;

// Set the bInheritHandle flag so pipe handles are inherited.

saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;

// Get the handle to the current STDOUT.

hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
hStdInput = GetStdHandle(STD_INPUT_HANDLE);

// Create a pipe for the child process's STDOUT.

if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
ErrorExit("Stdout pipe creation failed\n");

// Create noninheritable read handle and close the inheritable read
// handle.

fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
GetCurrentProcess(), &hChildStdoutRdDup , DUPLICATE_SAME_ACCESS,
FALSE,
DUPLICATE_SAME_ACCESS);
if( !fSuccess )
ErrorExit("DuplicateHandle failed");
CloseHandle(hChildStdoutRd);

// Create a pipe for the child process's STDIN.

if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
ErrorExit("Stdin pipe creation failed\n");

// Duplicate the write handle to the pipe so it is not inherited.

fSuccess = DuplicateHandle(GetCurrentProcess(), hChildStdinWr,
GetCurrentProcess(), &hChildStdinWrDup, DUPLICATE_SAME_ACCESS,
FALSE, // not inherited
DUPLICATE_SAME_ACCESS);
if (! fSuccess)
ErrorExit("DuplicateHandle failed");

CloseHandle(hChildStdinWr);

// Now create the child process.

fSuccess = CreateChildProcess();
if (! fSuccess)
ErrorExit("Create process failed");

// Get a handle to the parent's input file.

if (argc == 1)
ErrorExit("Please specify an input file");

hInputFile = CreateFile(argv[1], GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);

if (hInputFile == INVALID_HANDLE_VALUE)
ErrorExit("CreateFile failed\n");

// Write to pipe that is the standard input for a child process.

WriteToPipe();

// Read from pipe that is the standard output for child process.

// ReadFromPipe();
hProcess = piProcInfo.hProcess;
WaitForSingleObject(hProcess,INFINITE);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
return 0;
}

BOOL CreateChildProcess()
{

STARTUPINFO siStartInfo;
BOOL bFuncRetn = FALSE;
const char* lpAppName ="C:\\Windows\\System32\\Telnet.exe";
char* lpCmd = "open 10.41.37.239 -l guest";

// Set up members of the PROCESS_INFORMATION structure.

ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );

// Set up members of the STARTUPINFO structure.

ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = hChildStdoutRdDup;
siStartInfo.hStdOutput = hChildStdoutRdDup;
siStartInfo.hStdInput = hChildStdinWrDup;


// Create the child process.

bFuncRetn = CreateProcess(lpAppName,
lpCmd, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
CREATE_NEW_CONSOLE, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION

if (bFuncRetn == 0)
{
ErrorExit("CreateProcess failed");

}
else
{
//CloseHandle(piProcInfo.hProcess);
//CloseHandle(piProcInfo.hThread);
return bFuncRetn;
}
return 0;
}

VOID WriteToPipe(VOID)
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
memset(chBuf,0,BUFSIZE);
// Read from a file and write its contents to a pipe.

ReadFile(hInputFile, chBuf, BUFSIZE, &dwRead, NULL);

WriteFile(hStdInput, chBuf, dwRead, &dwWritten, NULL);



// Close the pipe handle so the child process stops reading.
if (! CloseHandle(hChildStdinWrDup))
ErrorExit("Close pipe failed");

}

VOID ReadFromPipe(VOID)
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
memset(chBuf,0,BUFSIZE);

// Close the write end of the pipe before reading from the
// read end of the pipe.

if (!CloseHandle(hChildStdoutWr))
ErrorExit("CloseHandle failed");

// Read output from the child process, and write to parent's STDOUT.
ReadFile( hChildStdoutRdDup, chBuf, BUFSIZE, &dwRead,
NULL);
WriteFile(hStdout, chBuf, dwRead, &dwWritten, NULL);

}

VOID ErrorExit (LPTSTR lpszMessage)
{
fprintf(stderr, "%s\n", lpszMessage);
ExitProcess(0);
}

Thanks in advance.

Best Regards
Viswa




Visweswara Koduri
QuestionFolder Security in VS Setup Project Pin
Federico Milano24-Aug-07 4:28
Federico Milano24-Aug-07 4:28 
QuestioneVC4 and VC++6 Pin
elegantcharm24-Aug-07 3:56
elegantcharm24-Aug-07 3:56 
AnswerRe: eVC4 and VC++6 Pin
Matthew Faithfull24-Aug-07 4:53
Matthew Faithfull24-Aug-07 4:53 
AnswerRe: eVC4 and VC++6 Pin
elegantcharm24-Aug-07 9:25
elegantcharm24-Aug-07 9:25 
AnswerRe: eVC4 and VC++6 Pin
ghle24-Aug-07 15:18
ghle24-Aug-07 15:18 
QuestionImages in ListCtrl disappear after selecting Pin
Atul2324-Aug-07 2:48
Atul2324-Aug-07 2:48 
QuestionRe: Images in ListCtrl disappear after selecting Pin
Hamid_RT24-Aug-07 3:06
Hamid_RT24-Aug-07 3:06 
QuestionBinding an application in VC++ Pin
Andy Rama24-Aug-07 1:45
Andy Rama24-Aug-07 1:45 
AnswerRe: Binding an application in VC++ Pin
toxcct24-Aug-07 1:49
toxcct24-Aug-07 1:49 
AnswerRe: Binding an application in VC++ Pin
Matthew Faithfull24-Aug-07 2:10
Matthew Faithfull24-Aug-07 2:10 
QuestionRe: Binding an application in VC++ Pin
Hamid_RT24-Aug-07 2:16
Hamid_RT24-Aug-07 2:16 
AnswerRe: Binding an application in VC++ Pin
Andy Rama26-Aug-07 23:17
Andy Rama26-Aug-07 23:17 
QuestionRe: Binding an application in VC++ Pin
David Crow24-Aug-07 6:00
David Crow24-Aug-07 6:00 
AnswerRe: Binding an application in VC++ Pin
Andy Rama26-Aug-07 23:19
Andy Rama26-Aug-07 23:19 
GeneralRe: Binding an application in VC++ Pin
David Crow27-Aug-07 3:00
David Crow27-Aug-07 3:00 
QuestionVisual c++ 6.0 Pin
plural24-Aug-07 1:18
plural24-Aug-07 1:18 
AnswerRe: Visual c++ 6.0 Pin
toxcct24-Aug-07 1:46
toxcct24-Aug-07 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.