I have an application that prints the report automatically. I am using CPrintDialog to get the Printer DC.
void CMyClass::PrintReport()
{
CDC dc;
CPrintDialog printDlg(FALSE);
printDlg.GetDefaults ();
::DeleteDC( printDlg.m_pd.hDC );
LPDEVMODE pDevMode = printDlg.GetDevMode();
if(pDevMode)
{
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
::GlobalUnlock(pDevMode);
}
HDC hDC;
if( (hDC = printDlg.CreatePrinterDC()) == NULL )
{
::GlobalFree( printDlg.m_pd.hDevMode );
::GlobalFree( printDlg.m_pd.hDevNames );
return;
}
::GlobalFree( printDlg.m_pd.hDevMode );
::GlobalFree( printDlg.m_pd.hDevNames );
dc.Attach(hDC); // Attach a printer DC
dc.m_bPrinting = TRUE;
dc.SetMapMode(MM_LOENGLISH);
/*
Printing Logic using dc
*/
}
This works fine when I run my application in the Debug mode which comes a a Console application.
But, the CPrintDialog creation is failing when I run the application as a Windows Service.
Am I doing anything wrong? :( Please help me.
Note: The Application is designed in a way to run as a Service in the Installation.
the CPrintDialog creation is failing when I run the application as a Windows Service.
You cannot display dialogs (or any type of user interface) in a Windows Service. So CPrintDialog is never going to work.
But you don't need to create a dialog to get a printer device context, assuming that you already know which printer you want to print to. And since you're running as a non-interactive service, you must already know this, because there's no way that the user can choose a printer.
To do so, just call CreateDC directly, specifying "WINSPOOL" as the device and the name of the printer. You can obtain the name of the desired printer by enumerating the installed printers using the EnumPrinters function. This is all conveniently documented in a how-to article: Retrieve a Printer Device Context.
Related
Hi i'm relativly new to kernel programming (i've got a lot of c++ development experience though) and have a goal that i want to achieve:
Detecting and conditionally blocking attempts from userland programs to write or read to specific memory addresses located in my own userland process. This has to be done from a driver.
I've setup a development enviorment (virtual machine running the latest windows 10 + virtualkd + windbg) and already successfully deployed a small kmdf test driver via the visual studio integration (over lan).
So my question is now:
How do i detect/intercept Read/WriteProcessMemory calls to my ring3 application? Simply blocking handles isn't enough here.
It would be nice if some one could point me into the right direction either by linking (a non outdated) example or just by telling me how to do this.
Update:
Read a lot about filter drivers and hooking Windows Apis from kernel mode, but i really dont want to mess with Patchguard and dont really know how to filter RPM calls from userland. Its not important to protect my program from drivers, only from ring3 applications.
Thank you :)
This code from here should do the trick.
OB_PREOP_CALLBACK_STATUS PreCallback(PVOID RegistrationContext,
POB_PRE_OPERATION_INFORMATION OperationInformation)
{
UNREFERENCED_PARAMETER(RegistrationContext);
PEPROCESS OpenedProcess = (PEPROCESS)OperationInformation->Object,
CurrentProcess = PsGetCurrentProcess();
PsLookupProcessByProcessId(ProtectedProcess, &ProtectedProcessProcess); // Getting the PEPROCESS using the PID
PsLookupProcessByProcessId(Lsass, &LsassProcess); // Getting the PEPROCESS using the PID
PsLookupProcessByProcessId(Csrss1, &Csrss1Process); // Getting the PEPROCESS using the PID
PsLookupProcessByProcessId(Csrss2, &Csrss2Process); // Getting the PEPROCESS using the PID
if (OpenedProcess == Csrss1Process) // Making sure to not strip csrss's Handle, will cause BSOD
return OB_PREOP_SUCCESS;
if (OpenedProcess == Csrss2Process) // Making sure to not strip csrss's Handle, will cause BSOD
return OB_PREOP_SUCCESS;
if (OpenedProcess == CurrentProcess) // make sure the driver isnt getting stripped ( even though we have a second check )
return OB_PREOP_SUCCESS;
if (OpenedProcess == ProtectedProcess) // Making sure that the game can open a process handle to itself
return OB_PREOP_SUCCESS;
if (OperationInformation->KernelHandle) // allow drivers to get a handle
return OB_PREOP_SUCCESS;
// PsGetProcessId((PEPROCESS)OperationInformation->Object) equals to the created handle's PID, so if the created Handle equals to the protected process's PID, strip
if (PsGetProcessId((PEPROCESS)OperationInformation->Object) == ProtectedProcess)
{
if (OperationInformation->Operation == OB_OPERATION_HANDLE_CREATE) // striping handle
{
OperationInformation->Parameters->CreateHandleInformation.DesiredAccess = (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
}
else
{
OperationInformation->Parameters->DuplicateHandleInformation.DesiredAccess = (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
}
return OB_PREOP_SUCCESS;
}
}
This code, once registered with ObRegisterCallback, will detect when a new handle is created to your protected process and will kill it if it's not coming from Lsass, Csrss, or itself. This is to prevent blue screens from critical process being denied a handle to
your application.
I have a service written in C#. Running the following code:
public static bool PrintPDF(string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = $#"-dPrinted -dNoCancel=true -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies={numberOfCopies} -sDEVICE=mswinpr2 -sOutputFile=""\\spool\{printerName}"" ""{pdfFileName}""";
startInfo.FileName = Path.Combine(ghostScriptPath, "gswin64c.exe");
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
Process process = Process.Start(startInfo);
Console.WriteLine(process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd());
process.WaitForExit(30000);
if (process.HasExited == false) process.Kill();
return process.ExitCode == 0;
}
Outside of Windows Service, it's working without any problem.
Inside the service, when running as Local System, GhostScript started running but timed out without any output.
After some fiddling around, I finally switched the service to run as Network Service and also set Network Service as owner of the folder from which the service exe and GhostScript exe where placed (Before I did that, I got Access Denied error) - And now the service is running fine.
My questions is - How come Network Service can work where Local System can't? I thought Local System has more privileges. And also, how can I get more info regarding the actual issue? I've found a workaround but it was simply a lucky shot in the dark. I have no idea what the real problem is.
Some more info:
Running Windows 10 64 bit, and using GhostScript v9.29
You need to run the service under a local user account that is dedicated to it.
You also need to login with that user at list one time in order the printer list to be populated!
Created a webextension for firefox (currently using Nightly 52), that uses native messaging to launch a java program on Linux (Ubuntu 14, 32x).
The webextension loads, reads the .json file and reads the path which points to a script that starts the java program. The JSON and the path are correct as when I use:
var native = browser.runtime.connectNative("passwordmanager");
console.log("native.name" + native.name); //outputs passwordmanager.
native.onDisconnect.addListener(function(m) { console.log("Disconnected"); });
The above code prints the name of the native port and also prints "Disconnected". So I m guessing the native app is terminating for some reason.
The application is only skeleton right now, that just does sysout and reads sysin and works correctly if Launch it directly through the shell script.
While debugging the webextension, I am not able to step into the call to connectNative, as it just steps-over that call instead of doing step-in. So kind of out of options whats' going wrong.
Please let me know if anyone is able to create a native messaging app based on FF webextension and any pointers on what I might be doing wrong.
Thanks
This solution here shows you how to detect onConnect and onFail. It should help you out to figure out your real problem.
So I don't think you can do proper error handling with connectNative from the JS side alone. You can do somewhat error handling if you get the exe side involved, but you can't get a string for "error reason" when an error occurs. The error is only logged to console.
First make sure to set your deeloper prefs, so messages show in your browser console. You can use this addon - https://addons.mozilla.org/en-US/firefox/addon/devprefs/ - or read that addon description it gives you the MDN page with the prefs to set.
Then this is how you can do some sort of error handling (without error reason) (pseudo-code - i might need a .bind in the callbcks):
function connectNative(aAppName, onConnect, onFail) {
var listener = function(payload) {
if (!connected) {
connected = true;
port.onDisconnect.removeListener(failedConnect);
onConnect();
} else {
// process messages
}
}
var failedConnect = function() {
onFail('failed for unattainable reason - however see browser console as it got logged there');
}
var connected = false;
var port = chrome.runtime.connectNative(aAppName);
port.onMessage.addListener(listener);
port.onDisconnect.addListener(failedConnect);
return port;
}
Now in your exe, as soon as it starts up, make it write to stdout something. That will trigger the onConnect.
I have this line of code in my Delphi app:
sh := CoShellWindows.Create;
When run through a Citrix session, this raises an exception "Not enough storage is available to complete this operation."
Can someone confirm my suspicion that I can't access this through Citrix? I'm running in Seamless mode if that makes any difference. Maybe there's something I need to change on the published icon to make it work?
I am guessing that there is no "Shell" in Citrix to create.
Thanks
EDIT
The CoShellWindows is simply a class which creates an object which implements the IShellWindows interface. This interface is then used to iterate through it's items looking for an instance of Internet Explorer (or more specifically, an item which implements the IWebBrowser2 interface).
There are a few other use case scenarios using the CoShellWindows, but all ultimately are used to interact with the IWebBrowser2 interface (Internet Explorer 8). My requirement is to obtain this IWebBrowser2 object.
The call, behind the scenes is calling the Windows API CoCreateInstance with the following parameters:
rclsid = {9BA05972-F6A8-11CF-A442-00A0C90A8F39} (CLSID of
IShellWindows)
pUnkOuter = null (nil)
dwClsContext = CLSCTX_ALL (I've tried various combinations of these
flags)
riid = {85CB6900-4D95-11CF-960C-0080C7F4EE85} (IID of IShellWindows)
ppv = a variable declared as type IShellWindows
eg:CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_ALL, IID_IShellWindows, sh)
Your exception "Not enough storage is available to complete this operation." should really read "Shell does not exist so no instance can be created"
Basically you are correct in your assumption that there is no shell to create in Citrix.
What are you using the shell for? as if you provide more information we may well be able to offer a full work around.
I have a C++ App than can optionally run as Windows Service on XP and interacts with the Desktop (yes, I know it's bad practice but it's been around for a long time!)
Retrofitting html help to it I've discovered HtmlHelp() doesn't work from a service. I've tried running hh.exe using CreateProcess() and ShellExecute() with no success. On the other hand, running Write using CreateProcess works fine, so there must be something different about hh.exe. No amount of googling has shed any light. How can I fire up a chm file from a service?
PROCESS_INFORMATION ProcInfo;
STARTUPINFO si;
memset(&si, '\0', sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
//si.lpDesktop = "winsta0\\default"; // <-- doesn't make any difference
char *helpcmd = "hh.exe c:\\help\myhelpfile.chm";
BOOL bSuccess = ::CreateProcess(NULL, helpcmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &ProcInfo);
Finally found an answer to this:
Under the registry key:
HKLM\SOFTWARE\Microsoft\HTMLHelp\1.x\HHRestrictions
Make a new DWORD value key:
EnableNonInteractiveUser
And set the value to 1.
This will allow an interactive service process on XP to show HTML Help.
If your service process is already running you may need to restart it before this will work (the need to do this or not depends on how your program run-time system starts the HTML Help viewer - cached results mean that it might not work until restart.)
At this stage its unknown to me if the same change will work for Vista, Win7 or Win8.