redmon.c
来自「虚拟打印端口」· C语言 代码 · 共 2,160 行 · 第 1/5 页
C
2,160 行
/* If we return an error from WritePort, the spooler crashes.
* To avoid this mess, don't return an error from WritePort.
* Instead carry on as if the error didn't occur.
* The only evidence of an error will be the log file.
*/
*pcbWritten = cbBuf; /* say we wrote it all */
if (prd->config.dwLogFileDebug
&& (prd->hLogFile != INVALID_HANDLE_VALUE)) {
wsprintf(buf,
TEXT("\r\nREDMON WritePort: Process not running. \
Returning TRUE.\r\n Ignoring %d bytes\r\n"), cbBuf);
write_string_to_log(prd, buf);
}
/* Cancel the print job */
redmon_cancel_job(prd);
return TRUE; /* say we wrote it all */
}
if (prd->config.dwLogFileDebug && (prd->hLogFile != INVALID_HANDLE_VALUE)) {
wsprintf(buf,
TEXT("\r\nREDMON WritePort: about to write %d bytes to port.\r\n"),
cbBuf);
write_string_to_log(prd, buf);
}
/* write to stdin pipe */
prd->write_buffer_length = cbBuf;
prd->write_buffer = pBuffer;
prd->write_flag = TRUE;
prd->write_written = 0;
SetEvent(prd->write_event);
if (prd->write && prd->write_buffer_length) {
flush_stdout(prd);
check_process(prd);
Sleep(0);
}
sleep_count = 0;
while (!prd->error && prd->write && prd->write_buffer_length) {
/* wait for it to be written, while flushing stdout */
if (flush_stdout(prd)) {
/* We succeeded in reading something from one of the
* pipes and the pipes are now empty. Give up the
* remainder of our time slice to allow the
* other process to write something to the pipes
* or to read from stdin.
*/
sleep_count = 0;
Sleep(0);
}
else if (prd->write_buffer_length) {
/* The pipes were empty, and the other process
* hasn't finished reading stdin.
* Pause a little until something is available or
* until stdin has been read.
* If the process is very slow and doesn't read stdin
* within a reasonable time, sleep for 100ms to avoid
* wasting CPU.
*/
if (sleep_count < 10)
Sleep(sleep_count * 5);
else
Sleep(100);
sleep_count++;
}
/* Make sure process is still running */
check_process(prd);
}
*pcbWritten = prd->write_written;
if (prd->error)
*pcbWritten = cbBuf;
if (prd->config.dwLogFileDebug && (prd->hLogFile != INVALID_HANDLE_VALUE)) {
DWORD cbWritten;
request_mutex(prd);
WriteFile(prd->hLogFile, pBuffer, cbBuf, &cbWritten, NULL);
FlushFileBuffers(prd->hLogFile);
release_mutex(prd);
wsprintf(buf,
TEXT("\r\nREDMON WritePort: %s count=%d written=%d\r\n"),
(prd->write_flag ? TEXT("OK") : TEXT("Failed")),
cbBuf, *pcbWritten);
write_string_to_log(prd, buf);
}
flush_stdout(prd);
if (prd->error) {
if (prd->config.dwLogFileDebug
&& (prd->hLogFile != INVALID_HANDLE_VALUE)) {
wsprintf(buf,
TEXT("\r\nREDMON WritePort: Process not running. \
Returning TRUE.\r\n Ignoring %d bytes\r\n"), cbBuf);
write_string_to_log(prd, buf);
}
/* Cancel the print job */
redmon_cancel_job(prd);
}
return TRUE; /* returning FALSE crashes Win95 spooler */
}
/* ReadPort can be called within a Start/EndDocPort pair,
* and also outside this pair.
*/
BOOL redmon_read_port(REDATA *prd, LPBYTE pBuffer,
DWORD cbBuffer, LPDWORD pcbRead)
{
/* we don't support reading */
*pcbRead = 0;
if (prd == (REDATA *)NULL) {
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
Sleep(1000); /* Pause a little */
if (prd->config.dwLogFileDebug) {
TCHAR buf[MAXSTR];
wsprintf(buf, TEXT("REDMON ReadPort: returning FALSE. Process %s\r\n"),
prd->error ? TEXT("has an ERROR.") : TEXT("is OK."));
write_string_to_log(prd, buf);
wsprintf(buf, TEXT("REDMON ReadPort: You must disable bi-directional printer support for this printer\r\n"));
write_string_to_log(prd, buf);
}
/* We don't support read port */
return FALSE;
}
BOOL redmon_end_doc_port(REDATA *prd)
{
TCHAR buf[MAXSTR];
DWORD exit_status;
HANDLE hPrinter;
unsigned int i;
if (prd == (REDATA *)NULL) {
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
#ifdef DEBUG_REDMON
syslog(TEXT("redmon_end_doc_port\r\n"));
#endif
if (prd->config.dwLogFileDebug)
write_string_to_log(prd,
TEXT("REDMON EndDocPort: starting\r\n"));
/* tell write thread to shut down */
prd->write_buffer_length = 0;
prd->write_buffer = NULL;
prd->write = FALSE;
SetEvent(prd->write_event);
Sleep(0); /* let write thread terminate */
for (i=0; i<20; i++) {
if (GetExitCodeThread(prd->write_hthread, &exit_status)) {
if (exit_status != STILL_ACTIVE)
break;
}
else
break;
Sleep(50);
}
CloseHandle(prd->write_hthread);
/* Close stdin to signal EOF */
if (prd->hChildStdinWr != INVALID_HANDLE_VALUE)
CloseHandle(prd->hChildStdinWr);
flush_stdout(prd);
/* wait here for up to 'delay' seconds until process ends */
/* so that process has time to write stdout/err */
exit_status = 0;
for (i=0; i<prd->config.dwDelay; i++) {
if (prd->piProcInfo.hProcess == INVALID_HANDLE_VALUE)
break;
if (GetExitCodeProcess(prd->piProcInfo.hProcess, &exit_status)) {
if (exit_status != STILL_ACTIVE)
break;
}
else {
/* process doesn't exist */
break;
}
flush_stdout(prd);
Sleep(1000);
}
flush_stdout(prd);
if (prd->config.dwLogFileDebug) {
wsprintf(buf,
TEXT("REDMON EndDocPort: process %s after %d second%s\r\n"),
(exit_status == STILL_ACTIVE) ?
TEXT("still running") : TEXT("finished"),
i, (i != 1) ? TEXT("s") : TEXT(""));
write_string_to_log(prd, buf);
}
/* if process still running, we might want to kill it
* with TerminateProcess(), but since this can have bad
* side effects, don't do it yet.
*/
/* When the process terminates, it will close its copy of
* of the stdout & stderr pipe write handles.
* The pipes remain open because we still have a handle to
* to the write end.
* Close our copy of the write end of the stdout & stderr
* pipes to flush them.
*/
CloseHandle(prd->hChildStdoutWr);
CloseHandle(prd->hChildStderrWr);
if (prd->hPipeWr != INVALID_HANDLE_VALUE);
CloseHandle(prd->hPipeWr);
/* copy anything on stdout/err to log file */
flush_stdout(prd);
/* Close the read end of the stdio pipes */
CloseHandle(prd->hChildStderrRd);
CloseHandle(prd->hChildStdoutRd);
CloseHandle(prd->hChildStdinRd);
if (prd->hPipeWr != INVALID_HANDLE_VALUE);
CloseHandle(prd->hPipeRd);
/* NT documentation says *we* should cancel the print job. */
/* 95 documentation says nothing about this. */
if (OpenPrinter(prd->pPrinterName, &hPrinter, NULL)) {
SetJob(hPrinter, prd->JobId, 0, NULL, JOB_CONTROL_CANCEL);
ClosePrinter(hPrinter);
}
#ifdef UNUSED
/* elsewhere, the NT documentation says we should use */
/* JOB_CONTROL_SENT_TO_PRINTER */
if (OpenPrinter(prd->pPrinterName, &hPrinter, NULL)) {
SetJob(hPrinter, prd->JobId, 0, NULL, JOB_CONTROL_SENT_TO_PRINTER);
ClosePrinter(hPrinter);
}
#endif
/* If we output to a temporary file, print it now */
if (prd->config.dwOutput == OUTPUT_FILE) {
if (prd->config.dwLogFileDebug) {
wsprintf(buf,
TEXT("\r\nREDMON EndDocPort: Copying %s to %s"),
prd->tempname, prd->config.szPrinter);
write_string_to_log(prd, buf);
}
redmon_printfile(prd, prd->tempname);
if (prd->config.dwLogFileDebug)
write_string_to_log(prd,
TEXT("\r\nREDMON EndDocPort: Deleting temporary file"));
DeleteFile(prd->tempname);
}
else if ( (prd->config.dwOutput == OUTPUT_STDOUT) ||
(prd->config.dwOutput == OUTPUT_HANDLE) ){
redmon_close_printer(prd);
if (prd->config.dwLogFileDebug)
write_string_to_log(prd,
TEXT("\r\nREDMON EndDocPort: Closing printer for stdout or pipe"));
}
if (prd->config.dwLogFileDebug) {
TCHAR buf[256];
wsprintf(buf,
TEXT("\r\nREDMON EndDocPort: %d bytes written to printer\r\n"),
prd->printer_bytes);
write_string_to_log(prd, buf);
}
if (prd->environment) {
/* Are we responsible for this, or does the process delete
* its own environment? */
GlobalUnlock(prd->environment);
GlobalFree(prd->environment);
prd->environment = NULL;
}
if (prd->config.dwRunUser)
CloseHandle(prd->primary_token);
if (prd->config.dwLogFileDebug)
write_string_to_log(prd,
TEXT("REDMON EndDocPort: ending\r\n"));
if (prd->hLogFile != INVALID_HANDLE_VALUE)
CloseHandle(prd->hLogFile);
if (prd->error && prd->config.dwPrintError &&
((prd->config.dwOutput == OUTPUT_STDOUT) || (prd->config.dwOutput == OUTPUT_FILE)
|| (prd->config.dwOutput == OUTPUT_HANDLE)) )
redmon_print_error(prd);
if ((prd->hmutex != NULL) && (prd->hmutex != INVALID_HANDLE_VALUE))
CloseHandle(prd->hmutex);
if (prd->piProcInfo.hProcess != INVALID_HANDLE_VALUE)
CloseHandle(prd->piProcInfo.hProcess);
if (prd->piProcInfo.hThread != INVALID_HANDLE_VALUE)
CloseHandle(prd->piProcInfo.hThread);
reset_redata(prd);
return TRUE;
}
BOOL
redmon_set_port_timeouts(REDATA *prd, LPCOMMTIMEOUTS lpCTO, DWORD reserved)
{
/* Do nothing */
if (prd == (REDATA *)NULL) {
SetLastError(ERROR_INVALID_HANDLE);
return FALSE;
}
#ifdef DEBUG
{
TCHAR buf[MAXSTR];
if (prd->config.dwLogFileDebug) {
wsprintf(buf, TEXT("REDMON SetPortTimeOuts: returning TRUE\r\n\
values = %d %d %d %d %d\r\n"),
lpCTO->ReadIntervalTimeout,
lpCTO->ReadTotalTimeoutMultiplier,
lpCTO->ReadTotalTimeoutConstant,
lpCTO->WriteTotalTimeoutMultiplier,
lpCTO->WriteTotalTimeoutConstant);
write_string_to_log(prd, buf);
}
}
#endif
return TRUE;
}
UINT APIENTRY
GetSaveHookProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_INITDIALOG) {
SetForegroundWindow(hDlg);
BringWindowToTop(hDlg);
}
return 0; // default handler
}
BOOL
browse(HWND hwnd, UINT control, UINT filter, BOOL save)
{
OPENFILENAME ofn;
TCHAR szFilename[MAXSTR]; /* filename for OFN */
TCHAR szFilter[MAXSTR];
TCHAR szDir[MAXSTR];
TCHAR cReplace;
int i;
HANDLE hToken = NULL;
BOOL flag;
/* setup OPENFILENAME struct */
FillMemory((PVOID)&ofn, sizeof(ofn), 0);
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFilename;
ofn.nMaxFile = sizeof(szFilename);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
GetDlgItemText(hwnd, control, szFilename,
sizeof(szFilename)/sizeof(TCHAR) - 1);
if ((lstrlen(szFilename) == 2) && (szFilename[1] == ':'))
lstrcat(szFilename, BACKSLASH);
if (lstrlen(szFilename)) {
lstrcpy(szDir, szFilename);
for (i=lstrlen(szDir)-1; i; i--) {
if (szDir[i] == '\\') {
lstrcpy(szFilename, szDir+i+1);
szDir[i+1] = '\0';
ofn.lpstrInitialDir = szDir;
break;
}
}
}
if (LoadString(hdll, filter, szFilter,
sizeof(szFilter)/sizeof(TCHAR) -1 )) {
cReplace = szFilter[lstrlen(szFilter)-1];
for (i=0; szFilter[i] != '\0'; i++)
if (szFilter[i] == cReplace)
szFilter[i] = '\0';
ofn.lpstrFilter = szFilter;
ofn.nFilterIndex = 0;
}
/* call the common dialog box */
/* The following call may be needed to make the dialog
appear on the correct session.
Need to call this in spoolss.dll, so we need to load this
DLL dynamically.
hToken = RevertToPrinterSelf();
*/
if (save)
flag = GetSaveFileName(&ofn);
else
flag = GetOpenFileName(&ofn);
/*
if (hToken)
ImpersonatePrinterClient(hToken);
*/
if (flag)
SetDlgItemText(hwnd, control, szFilename);
return flag;
}
int
lstrcmpn(LPTSTR s1, LPTSTR s2, i
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?