COM Interop VB6 ActiveX EXE - c#

In my VS2012 C# project, I am wiring a code that is using VC6 composed ActiveX EXE through its auto generated assembly equivalent that, I suppose, some plumbing is done with TLBIMP.exe when it is referenced in my C# project.
When the code is executed I see the ActiveX EXE being launched in a separate process and is terminated upon the termination of the C# code that it is referenced from.
It seems working as I expected, but I have hard time looking for documentation from which I can rest assure that it is the correct usage of ActiveX EXE in C# project.
Can anyone give some advice?

It is a good sign that you can see the process running. A very easy way to prove success and gain 100% confidence would be to add logging to your VC6 exe and examine the log to ensure it is behaving as you expect. For example, if your VC6 exe receives command line arguments, you could log what they are to ensure you are correctly executing it from your C# program. If you cannot change the VC6 exe, you are left with black box testing: With specific inputs, do you get the expected outputs? I suppose you could use fancier and more time consuming methods, but you might not need to if my suggestions are sufficient.

Related

Looking to launch a local exe on a remote machine without having the resurces on the remote machine

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.

GhostScript PS to PDF with images only work during debug mode in Visual Studio

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.

Baking an external .exe into a C# project [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Embedded a *.exe into a dll
I have a C# project - a class library - that produces a DLL file.
From within itself, this project runs an external .exe file. (Using the standard Process stuff, and it works fine.)
However, my question is: How can I bake the external .exe file into the project, such that the end-consumer will only receive the final DLL file, without ever seeing the .exe file itself?
This will make my client happier, as he will continue receiving one DLL file (as before, before I needed the .exe).
If you are going to launch it as an external process, it needs to exist on disk. It could be embedded in the .dll as a resource and extracted on-demand.
Your best bet would be to make the exe an embedded resource.
If your client needs the .exe at runtime, you'd darn well better make sure he has a copy of the .exe. IMHO...
PS:
You can "trick" the client by embedding your .dll as a resource (as a "Trojan horse"), and then extracting it at runtime. Which, IMHO, would be stupid. Expensive. And risky.
If you need the .exe, ship the .exe. And be explicit about it.
If the client expressly refuses to have an .exe - then your implemention violates the requirements, and you need to go back to the drawing board.
The "exe-as-resource" workaround ... is a lie and a cheat. And it isn't even a very efficient or safe cheat.
ALSO:
Embedded a *.exe into a dll
On a side note, remember that when you pull a file from your resources
to disk and then execute code on it, you may trigger Windows Data
Execution Prevention - basically, Windows tries to automatically
detect if something is supposed to be code or data, and if it looks
like data (which a resource would), then it will prevent that data
from being executed as code.
This becomes a particularly sticky issue if your .NET assembly is
going to be used over a network instead of from a local drive - there
are all sorts of .NET security configurations that might prevent this
from working correctly.

There is a way to delete an file that is being executed without kill it?

I have a question that I believe that is complex. I have an application that I execute under my Windows and it takes a long time to finish. I want to keep it running (normally), however I want to kill the file on disk - but obviously it's not possible because it's locked / in-use. I need a way to disassociate it from the running process to kill it and at the same time keep the file running. Any example of code or tool is very welcome.
Well, workarounds are welcome, for example, if there is a way to spawn it from a process, key the master and migrate the child to kill the app, or any other idea that works is welcome - even the ugly ones. :)
Thanks.
A couple of suggestions (completely stolen) from this questions answers:
You could use the MoveFileEx api function to mark the file for deletion upon next reboot.
You can inject a dll to close the handle yourself:
The typical method is as follows. You've said you want to do this in C# so here goes...
If you don't know which process has the file locked, you'll need to examine each process's handle list, and query each handle to determine if it identifies the locked file. Doing this in C# will likely require P/Invoke or an intermediary C++/CLI to call the native APIs you'll need.
Once you've figured out which process(es) have the file locked, you'll need to safely inject a small native DLL into the process (you can also inject a managed DLL, but this is messier, as you then have to start or attach to the .NET runtime).
That bootstrap DLL then closes the handle using CloseHandle etc.
Essentially: the way to unlock a "locked" file is to inject a DLL into the offending process's address space and close it yourself. You can do this using native or managed code. No matter what, you're going to need a small amount of native code or at least P/Invoke into the same.
Helpful links:
http://www.codeproject.com/KB/threads/winspy.aspx
http://damianblog.com/2008/07/02/net-code-injection/
That is a matter the application you want to kill has to handle. It shouldn't keep files open during a long running process. If the application doesn't close the file, killing it will lead to exception in that application.
Not sure if this will work on every Windows version, but here it is:
Rename process executable "foo.exe" to "foo.old"
Put new "foo.exe" to correct place
Send message to process, so it will execute new "foo.exe" image and terminate himself.
On start, remove "foo.old" file in program directory.
Update: oops, looks like you do not want to put new image, just remove old one. Then MoveFileEx is only "legal" option.

Run.Cs or.Vb files without calling the in asp.net

By security reason I ask this... Can .cs or .vb files to run in any way without calling those in asp.net?
C# and VB script requires a host to interpret the code. Windows Explorer wont interpret C# or VB script. However, there are applications which can be launched which will interpret the script. For example, cscript.exe can interpret *.vbs files and execute the code within it.
There are also other more malevolent techniques to trick applications into executing script. This is particularly used in getting Microsoft office to open exploits on computers via scripting.
they can be compiled using csc.exe or vbc.exe and then run as .exe, but this can be done only if malevolent user has logged in at your server.
And if so he can do mostly anything his windows account permits.

Categories