Note: While my sample code is in Powershell, I could easily convert a C# sample into Powershell. I believe that C# developers are more likely to have encountered this issue, even though I am using Powershell to try and do this.
I am trying to debug an issue with server-side Word automation. I know that you're not meant to use it for server applications and we are in the process of changing the application so it doesn't do this but until this happens I need a reliable way to debug issues where it hangs as it does so randomly.
It is not OK for me to just switch .Visible = True on the Word object because it doesn't fail all the time. I just need a way to retrieve it every now and again when it does.
If I start Word and execute this script, it works:
$w = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Word.Application")
$w.Visible = $false;
Start-Sleep -Seconds 5
$w.Visible = $true;
However, if the hanging copy of Word starts as invoked by the COM+ process, the following exception occurs:
Exception calling "GetActiveObject" with "1" argument(s): "Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))"
The Powershell and COM+ process run under the same user, and in Task Manager they both show up as running under the same user.
I have tried running Powershell as all combinations of elevated/non-elevated and x86/x64 modes to no avail.
Therefore I assume the process is in another context or area that I am not able to access from my Powershell session.
So my question is one of:
How can I launch the Powershell process as part of the COM+ process?
How can I switch my context so I can access this object instance?
Is there any other way I can try and retrieve this hidden window and set it to visible?
Unfortunately, most questions that seemed to revolve around this theme end with someone saying "well, don't do that then". Sadly we don't have an option to redevelop this significant part of the system at the moment and I would like to get this documented for those of us who have to deal with these legacy systems from time to time.
Related
So I have built some code, it's quite simple basically it stops all active input from keyboard and mouse until a text file of a certain name appears in the C:\Temp directory. It also has a manifest file to run it as administrator on start up.
So I found something that on the surface looks like it fulfils my needs of being able to do this task however upon running it I found out that the project has been compiled in x86 and does not run on my x64 machine. Here is the reference to the project if anyone would like to look into it, it's a very smartly designed piece of code that does an interesting objective. It also explains clearly enough what I am trying to accomplish.
So after implementing this (and failing) I have setup a couple other avenues to try, one is VBA through excel with the VBA copying itself to and from the machines in a list and running itself, then there is using VBS to write the entire code as a txt file on the target machine change the extension and then execute it remotely. I have just started researching these but I imagine the problems of running as an administrator amongst other things will crop up again to be dealt with. To be honest though I would really prefer to do this in C# only as that is the language I'm trying to go further in so I'm interested in this challenge. If anybody knows of a similar library of code or application I could look into to achieve what I'm trying I would appreciate being pointed in the right direction.
I would try and be more specific about what libraires/API's im trying to implement but the truth is I don't know what libraries I need to even interact with to get what I want. My goal is to have C# executable code on my machine and a tool that can run that executable on another machine.
Thanks
Thanks to the help in comments from #Nick.McDermaid I was able to correctly open and build the project I was trying to download. Unsure what caused the issue previously with me not being able to open and interact with the code but now I have it I shall pursue this avenue further to accomplish my goal.
As an addendum one other avenue I tried for executing code remotely was through VBS where I used
set svcproc=getobject("winmgmts:{impersonationLevel=impersonate}!\\"&MachineName & "\root\cimv2:win32_process")
scmd="""C:\Program Files\Internet Explorer\iexplore.exe"" -framemerging ""https://gifyu.com/images/Boo-Ghost-Gif.gif"""
'scmd="C:\Windows\notepad.exe"
iret=svcproc.create(scmd,null,null,pid)
set svcproc=nothing
to execute something that existed on the remote machine but I ran into a LOT of security policy issues where I could launch the process but I couldn't bring it to the foreground as the Malware tracker on the machine thought it was an attack and quashed it immediately.
I'm trying to convert a ps file (word file with image) to pdf using Ghostscript.
Everything works fine when I'm debugging my code and just stepping thru it, It generates the pdf with the text,images and whatnot. But when I deploy the app using Visual Studio Setup Project, It does not work and gives me this error "An error occured when call to 'gsapi_new_instance' is made: -100."
Here's my command line arguments
var args = string.Format("-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=\"{1}\" -c save pop -f \"{0}\"", inputFile, #"C:\MedirefPrinter\converted\out.pdf");
Any idea why this isn't working? Thanks
Actual Code :
File Changed Handler
ShellCommand
Please excuse my noobness :)
Moved to an answer to allow more text.
There are three possible reasons for the error:
1) The 'instance' pointer is NULL. I can't see how this is ever possible with our executable as its a globally defined variable and the executable passes its address. This is a sanity check for people writing code against the Ghostscript API.
2) The application was unable to allocate sufficient memory for some internal structures. Again this seems unlikely as your system would have to be unreasonably short on memory.
3) The DLL instance count is already 1 or greater. This can happen if the DLL is shared between multiple processes. Unless you build the library with GS_THREADSAFE it isn't thread safe, and so you can't have multiple processes using the same instance of the DLL. I'd guess that this is your problem but obviously you haven't supplied a full set of code, so I don't know. If you are trying to run more than one copy of Ghostscript simultaneously, from the same directory, then you will get this error.
error -100 means 'something really bad happened so early on that I can't even tell you what it is'.
I very much doubt that the presence of images in the PostScript has any real impact, except that possibly it may slow the interpretation down enough to cause you to attempt to launch two processes.
I have console application which is executed in another app. The problem is that the second app cannot catch the exceptions that first one throws (run with Process). So I have to write some class that contains my return code and appropriate message.So that the app which runs my executable can use it and show error message depending on return code. But I don't know how this kind of thing should be done.
This is an old post i stumbled on but why not answer?
I think what you are looking for is the error stream. In windows and most other systems the OS references it as stderr. I'll post some links for reading, but in essence, separate execution environments or applications only return codes that tell the OS weather or not an application exited (finished) successfully (0) or encountered an error (-1). In windows there are two separate streams stdout which we write execution messages to, or stderr which we write errors too. Those steams can then be read by your other application. Look at these for more info:
How to capture a Processes STDOUT and STDERR line by line as they occur, during process operation. (C#)
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx
Depending on your needs there are some other more complex solutions such as the "Interoperability Library" or writing to file.
Interop info:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/
I'm trying to detect if a process I'm launching via my C# program still answers or not.
I've tryed the following :
Process *notepads[];
notepads = Process::GetProcessesByName("Notepad.exe");
// Test to see if the process is responding.
if (notepads[0]->Responding())
notepads[0]->CloseMainWindow();
else
notepads[0]->Kill();
However, I would like to find a Qt equivalent for ANY process (not only MMI processes).
Do you know how I could do that ?
Getting process list is OS-dependent. so you have to do it by yourself on each platform.
on Windows, you can use Windows API EnumProcesses
on Linux, you can search /proc folder.
See QProcess documentation for details.
Qt does not provide any method to obtain information or control overs process that you don't explicitely start from a QProcess instance.
By my application am creating a pdf file & sharing it via Share Charm. In most times i could successfully sharing the document to Sky Drive, Mail etc..
But at rarely when i sharing the same document am getting an error message like "Something went wrong Couldn't share Document with SkyDrive"
& after that i couldn't share that document. I want to know about at whatever conditions this error may occurs?
If its a generic message such as: Something went wrong with Share. Try again later - when you run the app from VS, then this might help (from here- images):
Open Task Manager, end the explorer.exe task (Details tab).
Select Run new task from File in Task Manager and type in explorer in the textbox and click OK.
If it is specific to Sky Drive then it is likely that it has something to do with the Sky Drive application and not your app. I'm unable to replicate the problem you are having.
I would try to isolate the error and see exactly what triggers the error.
Start with debugging the share charm, but keep in mind that if you end the debug session in the datarequest handler the charm might break and stop working, and you will need to apply the dirty fix I wrote above.
Output what happens to a file and run the share until you get the problem, and see if there is anything in the file that can help you figure out the problem.
Also, check the event log on you computer for a hint, I tend to use powershell for those things, open powershell in admin mode and run Get-EventLog Application -newest 10 | Format-List . Otherwise type Event Viewer while in the 'Metro' mode and look under Applications.
From MSDN- on common issues with the Share
Common issues
Unhandled exceptions in a target app cause it to immediately terminate and be replaced with an error message. The target app should gracefully handle any expected errors originating from the user, such as invalid input data, and report them to the user.
If a target app takes too long to respond to an activation event, the system assumes that the app is choosing not to respond and displays an error. Processing data should be moved out of the activation handler whenever possible, typically by storing a ShareOperation object and processing it asynchronously.
Calls to sharing the API can throw exceptions when called too many times or in the wrong order. When you implement a long-running share, be sure to call the share methods in the following order, without calling any single method twice in a row.
ReportStarted
ReportDataRetrieved
ReportSubmittedBackgroundTask
You can call ReportError or ReportCompleted at any time to complete the sharing operation.
To learn more about the share API, see Windows.ApplicationModel.DataTransfer.ShareTarget namespace.
Got a working solution to this problem
See this links
How to share a file that has been created on the fly
Share Contract Closes Immediately