本文整理汇总了C++中SetStdHandle函数的典型用法代码示例。如果您正苦于以下问题:C++ SetStdHandle函数的具体用法?C++ SetStdHandle怎么用?C++ SetStdHandle使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetStdHandle函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetStdHandle
BOOL CIperfThread::InitInstance()
{
// TODO: スレッドごとの初期化をここで実行します。
HANDLE hOldIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOldOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hOldErr = GetStdHandle(STD_ERROR_HANDLE);
STARTUPINFOA si;
memset(&si,0,sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
m_uniqid = mkhash(m_CmdLine, m_uniqid) ;
m_uniqid += (WORD)m_nThreadID;
m_uniqid += (WORD)m_nThreadID >> 4;
FreeConsole();
AllocConsole();
CreateIperfPipe();
if(CreateProcess(NULL, m_CmdLine, NULL, NULL, TRUE, 0,
NULL, NULL, &si, &m_ProcessInfo) == FALSE ) {
return FALSE;
}
EnumWindows(WindowHidden, (LPARAM)(PROCESS_INFORMATION *)&m_ProcessInfo);
WaitForInputIdle(m_ProcessInfo.hProcess, INFINITE);
SetStdHandle(STD_OUTPUT_HANDLE,hOldOut);
SetStdHandle(STD_INPUT_HANDLE,hOldIn);
SetStdHandle(STD_ERROR_HANDLE,hOldErr);
EnumWindows(WindowHidden, (LPARAM)(PROCESS_INFORMATION *)&m_ProcessInfo);
return TRUE;
}
示例2: DosDupHandle
APIRET os2APIENTRY DosDupHandle(os2HFILE hFile,
PHFILE pHfile)
{
int srcIdx = (int)hFile;
FileTable.lock(srcIdx);
if(!FileTable[srcIdx]) {
FileTable.unlock(srcIdx);
return 1; //FixMe
}
int dstIdx;
if(*pHfile==(os2HFILE)-1) {
dstIdx = FileTable.findAndLockFree();
if(dstIdx==-1) {
FileTable.unlock(srcIdx);
return 4; //too many open files
}
} else {
dstIdx = (int)*pHfile;
FileTable.lock(dstIdx);
}
if(srcIdx==dstIdx) {
//no-op
FileTable.unlock(srcIdx);
return 0;
}
if(FileTable[dstIdx]) {
CloseHandle(FileTable[dstIdx]->ntFileHandle);
delete FileTable[dstIdx];
FileTable[dstIdx] = 0;
}
APIRET rc;
HANDLE target;
if(DuplicateHandle(GetCurrentProcess(),
FileTable[srcIdx]->ntFileHandle,
GetCurrentProcess(),
&target,
0,
TRUE,
DUPLICATE_SAME_ACCESS
))
{
rc = 0;
FileTable[dstIdx] = new ntFILE;
FileTable[dstIdx]->ntFileHandle = target;
FileTable[dstIdx]->mode = FileTable[srcIdx]->mode;
if(dstIdx==0)
SetStdHandle(STD_INPUT_HANDLE,target);
else if(dstIdx==1)
SetStdHandle(STD_OUTPUT_HANDLE,target);
else if(dstIdx==2)
SetStdHandle(STD_ERROR_HANDLE,target);
} else
rc = 6; //invalid handle
FileTable.unlock(srcIdx);
FileTable.unlock(dstIdx);
return rc;
}
示例3: WIN32_strerror
/*
* WIN32_strerror with argument for late notification */
const char *
WIN32_strerror(int err)
{
static char xbstrerror_buf[BUFSIZ];
if (err < 0 || err >= sys_nerr)
strncpy(xbstrerror_buf, wsastrerror(err), BUFSIZ);
else
strncpy(xbstrerror_buf, strerror(err), BUFSIZ);
return xbstrerror_buf;
}
int
WIN32_Close_FD_Socket(int fd)
{
int result = 0;
if (closesocket(_get_osfhandle(fd)) == SOCKET_ERROR) {
errno = WSAGetLastError();
result = 1;
}
_free_osfhnd(fd);
_osfile(fd) = 0;
return result;
}
#if defined(__MINGW32__) /* MinGW environment */
int
_free_osfhnd(int filehandle)
{
if (((unsigned) filehandle < SQUID_MAXFD) &&
(_osfile(filehandle) & FOPEN) &&
(_osfhnd(filehandle) != (long) INVALID_HANDLE_VALUE)) {
switch (filehandle) {
case 0:
SetStdHandle(STD_INPUT_HANDLE, NULL);
break;
case 1:
SetStdHandle(STD_OUTPUT_HANDLE, NULL);
break;
case 2:
SetStdHandle(STD_ERROR_HANDLE, NULL);
break;
}
_osfhnd(filehandle) = (long) INVALID_HANDLE_VALUE;
return (0);
} else {
errno = EBADF; /* bad handle */
_doserrno = 0L; /* not an OS error */
return -1;
}
}
示例4: GetCurrentProcess
void CIperfThread::CreateIperfPipe()
{
HANDLE hReadPipeOut;
HANDLE hReadPipeErr;
HANDLE hReadPipeInp;
HANDLE hWritePipeOut;
HANDLE hWritePipeErr;
HANDLE hWritePipeInp;
HANDLE hParent = GetCurrentProcess();
SECURITY_ATTRIBUTES security;
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.lpSecurityDescriptor = NULL;
security.bInheritHandle = TRUE;
if(!CreatePipe(&hReadPipeOut,&hWritePipeOut,&security,0)) ASSERT(0);// ("CreatePipe");
if(!CreatePipe(&hReadPipeErr,&hWritePipeErr,&security,0)) ASSERT(0);// ("CreatePipe");
if(!CreatePipe(&hReadPipeInp,&hWritePipeInp,&security,0)) ASSERT(0);// ("CreatePipe");
SetStdHandle(STD_OUTPUT_HANDLE,hWritePipeOut);
SetStdHandle(STD_ERROR_HANDLE, hWritePipeErr);
SetStdHandle(STD_INPUT_HANDLE, hReadPipeInp);
DuplicateHandle(hParent,hReadPipeOut,hParent,&m_hPipeOut,0,FALSE,DUPLICATE_SAME_ACCESS);
DuplicateHandle(hParent,hReadPipeErr,hParent,&m_hPipeErr,0,FALSE,DUPLICATE_SAME_ACCESS);
DuplicateHandle(hParent,hWritePipeInp,hParent,&m_hPipeIn,0,FALSE,DUPLICATE_SAME_ACCESS);
CloseHandle(hReadPipeOut);
CloseHandle(hReadPipeErr);
CloseHandle(hWritePipeInp);
}
示例5: ZeroMemory
bool ProcInOut::Exec(const std::string & cmd, std::string & out, DWORD timeout) {
bool fSuccess;
ZeroMemory(&saAttr, sizeof(saAttr));
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
return false;
if (! SetStdHandle(STD_OUTPUT_HANDLE, hChildStdoutWr))
return false;
fSuccess = (DuplicateHandle(GetCurrentProcess(), hChildStdoutRd,
GetCurrentProcess(), &hChildStdoutRdDup , 0,
(FALSE ? false : true),
(BOOL)DUPLICATE_SAME_ACCESS) != 0);
if( !fSuccess )
return false;
CloseHandle(hChildStdoutRd);
if (! CreateChildProcess(cmd)) {
CloseHandle(hChildStdoutWr);
CloseHandle(hChildStdoutRdDup);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
return false;
}
if (! SetStdHandle(STD_OUTPUT_HANDLE, hSaveStdout))
return false;
if (!CloseHandle(hChildStdoutWr))
return false;
HANDLE lpHandles[] = { piProcInfo.hProcess, hChildStdoutRdDup, NULL};
DWORD dwWait;
DWORD timerStart = GetTickCount();
DWORD timerEnd;
for (;;) {
dwWait = WaitForMultipleObjects(2, lpHandles, false, timeout);
// process finished
if (dwWait == WAIT_OBJECT_0 || dwWait == WAIT_ABANDONED_0 || dwWait == (WAIT_ABANDONED_0 + 1)) {
break;
}
if (dwWait == (WAIT_OBJECT_0 + 1)) {
timerEnd = GetTickCount();
// timeout
if (timerEnd < timerStart && ((MAXDWORD - timerStart) + timerEnd) >= timeout) // counter has been reinitialized
break ;
if (timerStart < timerEnd && (timerEnd - timerStart) >= timeout)
break ;
ReadFromPipe(out);
}
if (dwWait == WAIT_TIMEOUT) {
break;
}
}
CloseHandle(hChildStdoutRdDup);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
return true;
}
示例6: pipe_setup
/* This function does file handle black magic. Here, we create pipes,
duplicate the ones we're going to pass on to the child process, and
set the current process's stdin and stdout to be those pipes. (###
I'm not sure duplicating them is necessary, but it doesn't hurt...)
inpipes, outpipes, and old_handles are 2 element arrays. */
static void pipe_setup (STARTUPINFO *siStartInfo, int inpipes[],
int outpipes[], HANDLE old_handles[])
{
const int pipe_size = 2000;
HANDLE new_stdin, new_stdout;
HANDLE parent = GetCurrentProcess();
/* Create new file handles--in binary mode.
_pipe sticks the read then the write handle in {in,out}pipes, and
returns 0 on success and -1 on failure */
if (_pipe(inpipes, pipe_size, O_BINARY) != 0
|| _pipe(outpipes, pipe_size, O_BINARY) != 0
/* Duplicate the stdin and stdout handles. False on failure. */
|| !DuplicateHandle(parent, /* source process */
/* next, handle to dup */
(HANDLE) _get_osfhandle(inpipes[0]),
parent, /* Proc to give new handles to */
&new_stdin, /* Where new handle is stored */
0, /* Parameter ignored */
TRUE, /* Make new handle inheritable */
DUPLICATE_SAME_ACCESS)
|| !DuplicateHandle(parent, /* source process */
/* next, handle to dup */
(HANDLE)_get_osfhandle(outpipes[1]),
parent, /* Proc to give new handles to */
&new_stdout, /* Where new handle is stored */
0, /* Parameter ignored */
TRUE, /* Make new handle inheritable */
DUPLICATE_SAME_ACCESS)) {
fprintf(stderr, "Failed while doing pipe stuff for fd_exec");
exit(1);
}
/* Save the old stdin and stdout handles to some place we can remember */
old_handles[0] = GetStdHandle(STD_INPUT_HANDLE);
old_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE);
/* Set stdin and stdout to the new handles */
if (!SetStdHandle(STD_INPUT_HANDLE, new_stdin)
|| !SetStdHandle(STD_OUTPUT_HANDLE, new_stdout)) {
fprintf(stderr, "Failed while doing pipe stuff for fd_exec");
exit(1);
}
/* Now tell the StartInfo to use the handles we just created. By
default, child processes don't inherit the stdin and stdout of
their parents. */
siStartInfo->dwFlags = STARTF_USESTDHANDLES;
siStartInfo->hStdInput = new_stdin;
siStartInfo->hStdOutput = new_stdout;
/* nothing funny with stderr, but we still have to initialize
the field anyway */
siStartInfo->hStdError = GetStdHandle(STD_ERROR_HANDLE);
}
示例7: doExec
static long doExec( const char *std_in, const char *std_out, const char *cmd )
{
HANDLE cp;
long st;
HANDLE old_in;
HANDLE new_in;
HANDLE old_out;
HANDLE new_out;
old_in = INVALID_HANDLE_VALUE;
new_in = INVALID_HANDLE_VALUE;
old_out = INVALID_HANDLE_VALUE;
new_out = INVALID_HANDLE_VALUE;
preSpawn();
cp = GetCurrentProcess();
if( std_in != NULL ) {
old_in = GetStdHandle( STD_INPUT_HANDLE );
new_in = CreateFile( std_in, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if( new_in == INVALID_HANDLE_VALUE ) {
return( -1L );
}
SetStdHandle( STD_INPUT_HANDLE, new_in );
}
if( std_out != NULL ) {
old_out = GetStdHandle( STD_INPUT_HANDLE );
new_out = CreateFile( std_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING | CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
if( new_out == INVALID_HANDLE_VALUE ) {
if( std_in != NULL ) {
SetStdHandle( STD_INPUT_HANDLE, old_in );
CloseHandle( new_in );
}
return( -1L );
}
SetStdHandle( STD_OUTPUT_HANDLE, new_out );
}
if( cmd == NULL ) {
st = MySpawn( Comspec );
} else {
SetConsoleActiveScreenBuffer( GetStdHandle( STD_OUTPUT_HANDLE ) );
st = system( cmd );
}
if( std_in != NULL ) {
CloseHandle( new_in );
SetStdHandle( STD_INPUT_HANDLE, old_in );
}
if( std_out != NULL ) {
CloseHandle( new_out );
SetStdHandle( STD_OUTPUT_HANDLE, old_out );
}
postSpawn( st );
return( st );
}
示例8: FlushConsoleInputBuffer
void console_widget_NT_t::restore_console()
{
FlushConsoleInputBuffer(saved_input);
SetConsoleMode(saved_input, saved_mode);
SetStdHandle(STD_INPUT_HANDLE, saved_input);
SetStdHandle(STD_OUTPUT_HANDLE, saved_output);
SetConsoleCursorInfo(saved_output, &saved_cursor);
if (allocated_console) {
FreeConsole();
}
}
示例9: pipe_cleanup
/* Here we undo the hackery in the setup, and close any handles we
know fd_exec doesn't use.
*/
static void pipe_cleanup (int inpipes[], int outpipes[], HANDLE old_handles[])
{
/* Close unnecessary fd's--the ones the child uses */
if (close(inpipes[0]) != 0
|| close(outpipes[1]) != 0
/* close the handles we're pretending are our stdin and stdout */
|| !CloseHandle(GetStdHandle(STD_INPUT_HANDLE))
|| !CloseHandle(GetStdHandle(STD_OUTPUT_HANDLE))
/* now restore the real stdin and stdout */
|| !SetStdHandle(STD_INPUT_HANDLE, old_handles[0])
|| !SetStdHandle(STD_OUTPUT_HANDLE, old_handles[1])) {
lose("Failed while doing pipe cleanup for fd_exec");
}
}
示例10: AllocConsole
CL_ConsoleWindow_Generic::CL_ConsoleWindow_Generic(
const CL_StringRef &title,
int width,
int height)
{
#ifdef WIN32
AllocConsole();
SetConsoleTitle(CL_StringHelp::utf8_to_ucs2(title).c_str());
COORD coord;
coord.X = width;
coord.Y = height;
scrbuf =
CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL);
if(scrbuf == INVALID_HANDLE_VALUE)
throw CL_Exception("Unable to allocate console screen buffer");
SetStdHandle(STD_OUTPUT_HANDLE, scrbuf);
SetConsoleActiveScreenBuffer(scrbuf);
SetConsoleScreenBufferSize(scrbuf, coord);
#endif
}
示例11: SetHandle
static VOID SetHandle(UINT Number, HANDLE Handle)
{
if (Number < 3)
SetStdHandle(STD_INPUT_HANDLE - Number, Handle);
else
ExtraHandles[Number - 3] = Handle;
}
示例12: log
void QConsolePrivate::setupStandardIO (DWORD stdHandleId, int targetFd,
const char* name, const char* devName)
{
log ("Opening %s...\n", devName);
int fd = open (devName, _O_RDWR | _O_BINARY);
if (fd != -1)
{
if (fd != targetFd)
{
log ("Opened %s is not at target file descriptor %d, "
"duplicating...\n", name, targetFd);
if (dup2 (fd, targetFd) == -1)
log ("Failed to duplicate file descriptor: errno=%d.\n", errno);
if (close (fd) == -1)
log ("Failed to close original file descriptor: errno=%d.\n",
errno);
}
else
log ("%s opened and assigned to file descriptor %d.\n", devName, fd);
if (! SetStdHandle (stdHandleId, (HANDLE) _get_osfhandle (targetFd)))
log ("Failed to re-assign %s: error=%08x.\n", name, GetLastError ());
}
else
log ("Failed to open %s: errno=%d.\n", devName, errno);
}
示例13: win_stdin_start_thread
/* Start the reader thread and do all the file handle/descriptor redirection.
Returns nonzero on success, zero on error. */
int win_stdin_start_thread(void) {
int stdin_fd;
int stdin_fmode;
assert(stdin_thread == NULL);
assert(stdin_pipe_r == NULL);
assert(stdin_pipe_w == NULL);
assert(thread_stdin_handle == NULL);
/* Create the pipe that win_stdin_thread_func writes to. We reassign the
read end to be the new stdin that the rest of the program sees. */
if (CreatePipe(&stdin_pipe_r, &stdin_pipe_w, NULL, 0) == 0)
return 0;
/* Make a copy of the stdin handle to be used by win_stdin_thread_func. It
will remain a reference to the the true stdin after we fake stdin to read
from the pipe instead. */
if (DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
GetCurrentProcess(), &thread_stdin_handle,
0, FALSE, DUPLICATE_SAME_ACCESS) == 0) {
CloseHandle(stdin_pipe_r);
CloseHandle(stdin_pipe_w);
return 0;
}
/* Set the stdin handle to read from the pipe. */
if (SetStdHandle(STD_INPUT_HANDLE, stdin_pipe_r) == 0) {
CloseHandle(stdin_pipe_r);
CloseHandle(stdin_pipe_w);
CloseHandle(thread_stdin_handle);
return 0;
}
/* Need to redirect file descriptor 0 also. _open_osfhandle makes a new file
descriptor from an existing handle. */
/* Remember the newline translation mode (_O_TEXT or _O_BINARY), and
restore it in the new file descriptor. */
stdin_fmode = _getmode(STDIN_FILENO);
stdin_fd = _open_osfhandle((intptr_t) GetStdHandle(STD_INPUT_HANDLE), _O_RDONLY | stdin_fmode);
if (stdin_fd == -1) {
CloseHandle(stdin_pipe_r);
CloseHandle(stdin_pipe_w);
CloseHandle(thread_stdin_handle);
return 0;
}
dup2(stdin_fd, STDIN_FILENO);
/* Finally, start up the thread. We don't bother keeping a reference to it
because it runs until program termination. From here on out all reads
from the stdin handle or file descriptor 0 will be reading from the
anonymous pipe that is fed by the thread. */
stdin_thread = CreateThread(NULL, 0, win_stdin_thread_func, NULL, 0, NULL);
if (stdin_thread == NULL) {
CloseHandle(stdin_pipe_r);
CloseHandle(stdin_pipe_w);
CloseHandle(thread_stdin_handle);
return 0;
}
return 1;
}
示例14: m_kind
// set_stdhandle_t
set_stdhandle_t::set_stdhandle_t( DWORD kind, HANDLE handle )
: m_kind( kind ), m_save_handle( GetStdHandle( kind ) )
{
if( m_save_handle==INVALID_HANDLE_VALUE )
throw os_error_t( "set_stdhandle_t::set_stdhandle_t: GetStdHandle() failed" );
if( !SetStdHandle( kind, handle ) )
throw os_error_t( "set_stdhandle_t::set_stdhandle_t: SetStdHandle() failed" );
}
示例15: rb_SetStdHandle
VALUE
rb_SetStdHandle( VALUE self, VALUE fd, VALUE hConsoleOutput )
{
HANDLE handle = ULongToPtr( NUM2ULONG( hConsoleOutput ) );
if (SetStdHandle(NUM2UINT(fd), handle))
return NUM2UINT(1);
return rb_getWin32Error();
}