Is it possible to connect or make calls to an existing process? - createprocess

In Windows, I currently have two programs working as follow. Program A calls Program B with some parameters, which causes Program B to return some text then exits. Program A calls Program B again with other parameters, which causes Program B to do more things.
When Program B starts, it needs to do some time consuming loading. The method above means Program B has to do the loaded twice. Is it possible to load Program B only once?
e.g. Program A calls Program B with some parameters, which causes Program B to return some text, but the program continues to run. Program A calls the same process again, which causes Program B to do more things.
Is the above possible, perhaps using some sort of Windows messenging? Program A is a Delphi app, uses Windows' CreateProcess method. Program B is a .NET app that does various things according to the parameters passed in. I know we can do this by installing Program B as a Windows service, but I would like to avoid that if possible.

Here comes the technologies for inter process communication. .NET has WCF/webservices etc to do this. The service can be hosted using an executable (even a console app) or as an windows service. You can choose the webserver (IIS) also.
As your program B is in .NET, you can host it as a service and consume it in Delphi application. I don't know for what purpose, you wish to avoid it a a service. But this will positively give you an upper hand and the implementation is fairly simple.
Even in case you are using C/C++ etc, you can think of serving the program B as an webservice using wrappers like gSOAP.

We ended up creating Program B that has an invisible form so it runs continously. Program A sends a Windows message to Program B whenever it needs work to be done. When Program A quits, it sends a 'Quit' message to Program B, Program B sees the message and quits as well.

Related

PyO3 - prevent user submitted code from looping and blocking server thread

I'm writing a game in Rust where each player can submit some python scripts to the server in order to automate various tasks in the game. I plan on using pyo3 to run the python from rust.
However, I can see an issue arising if a player submits a script like this:
def on_event(e):
while True:
pass
Now when the server calls the function (using something like PyAny::call1()) the thread will hang as it reaches the infinite loop.
My first thought was to have pyo3 execute the python one statement at a time, therefore being able to exit if the script been running for over a certain threshold, but I don't think pyo3 supports this.
My next idea was to give each player their own thread to run their own scripts on, that way if one of their scripts got stuck it only affected their gameplay. However, I still have the issue of not being able to kill a thread when it gets stuck in an infinite loop - if a lot of players submitted scripts that just looped, lots of threads would start using a lot of CPU time.
All I need is way to execute python scripts in a way such that if one of them does loop, it does not affect the server's performance at all.
Thanks :)
One solution is to restrict the time that you give each user script to run.
You can do it via PyThreadState_SetAsyncExc, see here for some code. It uses C calls of the interpreter, which you probably can access in Rust (with PyO3 FFI magic).
Another way would be to do it on the OS level: if you spawn a process for the user script, and then kill it when it runs for too long. This might be more secure if you limit what a process can access (with some OS calls), but requires some boilerplate to communicate between the host.

Creating a new "internal" process?

I'm writing a DLL (in Visual C++) and I recently decided that I need to move stuff that currently happens in threads into their own process. This is because I want to support multiple instances of the DLL being loaded and running. However, they all need to access the same group of resources (i/o buffers to a COM port) that needs to be autonomously monitored as long as there is at least one instance of the DLL running.
It seems I need to use CreateProcess(), but I'm unclear on how I should use the lpApplicationName argument. In the examples I've seen, the name of an existing program gets passed, but that isn't what I imagine I need to do. I expected to be able to start a process by specifying a function, much like with CreateThread(). The process doesn't need to be compiled and output as its own executable, does it? It definitely shouldn't be used by anything other than my DLL. Thanks.
EDIT: Okay, so if all CreateProcess() can do is start a pre-existing program, how can I get this to work? If the following happens:
Process loads the DLL
DLL starts port monitoring threads
Second process loads the DLL
Second DLL establishes some IPC to access the same data as the first DLL
First DLL is about to exit, and terminates the monitoring threads
Second DLL starts its own monitoring threads and continues
Doing 5 and 6 seems (especially with my implementation) like a clunky way of doing things, rather than just have behavior that I never have to terminate and restart.
EDIT: The more I think about this, the more I like the idea of making a separate executable, but if anyone think of a more "elegant" method, I'd still like to know.
You can't do that. On *nix you could fork can then call whatever function you want, but CreateProcess doesn't work that way. The only thing CreateProcess can do is launch a new process with execution starting at the entry point of an on-disk executable.

catch exceptions from another process

I would like to know the possibility of catching exceptions and their relevant stack traces that might occur in any process or thread within a process. Is it even possible (say in Linux)? Please have a look at an example of what I am trying to do:
OS : Linux
P1 : Java Process (Running some app inside it)
P2 : Python Process
P3 : C++ process
P4 : My custom monitoring code (listening to exceptions that might happen in P1, P2, P3)
Lets say P1 generates an exception that happens in it code (say NullPointerException) which is caught correctly in the Java code and relevant exception handling procedure runs. Similarly, exceptions might happen in P2 and P3 later in time.
Can P4 (another process only listening for exceptions in P1, P2 and P3) capture all handled/unhandled exceptions in P1, P2, P3? Is it even possible in Linux? If yes, what would be required to make this happen?
In Linux OS you can use the interface which is primary build in for debugging. Look for system call ptrace.
from man ptrace
The ptrace() system call provides a means by which one process (the "tracer") may observe and control the execution of another process (the "tracee"), and examine
and change the tracee's memory and registers. It is primarily used to implement breakpoint debugging and system call tracing.
If you want to know how to get internal information's from the processes you are tracing, you should start with the gdb sources. Maybe have a look in gdb/linux-nat.c as a start point.
But this gives you the OS view on the things you are running. A python script itself is not running and traceable. Instead the python interpreter can be traced. But the information you can get from your os will not provide any useful information from the python language itself. Debugging a python script or a java byte code program is very special. You have to connect to the debug interface of the interpreter if available. For python you can get a debugger pdb. Maybe you can start to inspect this sources how to debug a python program.
Good luck :-)
A practical, straight-forward way of achieving this:
Log all errors from P1-3 into logfiles.
Watch these logfiles with P4.

Qt GUI non responsive on Windows XP

I have a Qt application which gets serial data and displays is in a dashboard type GUI. The basic structure of the program is as follows:
EDIT
SerialPort (Inherits from QIODevice) object get created and have their readyRead signals connected to a slot.
When new data comes in, it's interpreted and sent through the program via a message handler. Eventually the data makes its way to a GUI layer, where it is displayed to the user.
The program runs fine in windows 7, however when I run it on a Panasonic toughbook, running windows XP, the program starts off fine, but after a few moments the GUI stops updating. What I mean by this is that when new data comes in, the gui won't redraw until a user clicks a button or resizes. I'm wondering what are some possible reasons for this type of behavior. I thought it could be that the Main thread was getting overwhelmed by all the serial data coming in, but I think that the GUI runs in a separate thread anyways. Am I wrong? Does anyone have any ideas as to what could be happening?
If SerialPortIO isn't in its own thread, then it might be blocking when it is waiting for new data. I haven't used that particular Serial class, but in general a stream of data probably should be in its own thread.
You can force the program to update the GUI more, by calling qApp->processEvents() periodically, and the GUI should update.
Also, put some qDebug statements in your code, especially at the top of your functions that you suspect that are getting called too frequently or not enough.
Use the following line, and it makes it really easy to follow what is happening in a multithreaded application:
qDebug() << Q_FUNC_INFO;
Hope that helps.

Why the window of my vb6 application stalls when calling a function written in C?

I'm using 3.9.7 cURL library to download files from the internet, so I created a dynamic bibioteca of viculo. dll written in C using VC + + 6.0 the problem is that when either I call my function from within my vb6 application window locks and unlocks only after you have downloaded the file how do I solve this problem?
The problem is that when you call the function from your DLL, it "blocks" your app's execution until it gets finished. Basically, execution goes from the piece of code that makes the function call, to the code inside of the function call, and then only comes back to the next line after the function call after the code inside of the function has finished running. In fact, that's how all function calls work. You can see this for yourself by single-stepping through your code in the VB 6 development environment.
You don't normally notice this because the code inside of a function being called doesn't take very long to execute before control is returned to the caller. But in this case, since the function you're calling from the DLL is doing a lot of processing, it takes a while to execute, so it "blocks" the execution of your application's code for quite a while.
This is a good general explanation for the reason why your application window appears to be frozen. A bit more technically, it's because the message pump that is responsible for processing user interaction with on-screen elements is not running (it's part of your code that has been temporarily suspended until the function that you called finishes processing). This is a bit more difficult for a VB programmer to appreciate, since none of this nitty-gritty stuff is exposed in the world of VB. It's all happening behind the scenes, just like it is in a C program, but you don't normally have to deal with any of it. Occasionally, though, the abstraction leaks, and the nitty-gritty rears its ugly head. This is one of those cases.
The correct solution to this general problem, as others have hinted at, is to run lengthy operations on a background thread. This leaves your main thread (right now, the only one you have, the one your application is running on) free to continue processing user input, while the other thread can process the data and return that processed data to the main thread when it is finished. Of course, computers can't actually do more than one thing at a time, but the magic of the operating system rapidly switching between one task and another means that you can simulate this. The mechanism for doing so involves threads.
The catch comes in the fact that the VB 6 environment does not have any type of support for creating multiple threads. You only get one thread, and that's the main thread that your application runs on. If you freeze execution of that one, even temporarily, your application freezes—as you've already found out.
However, if you're already writing a C++ DLL, there's no reason you can't create multiple threads in a VB 6 app. You just have to handle everything yourself as if you were using another lower-level language like C++. Run the C++ code on a background thread, and only return its results to the main thread when it is completely finished. In the mean time, your main thread is free.
This is still quite a bit of work, though, especially if you're inexperienced when it comes to Win32 programming and the issues surrounding multiple threads. It might be easier to find a different library that supports asynchronous function calls out-of-the-box. Antagony suggests using VB's AsyncRead method. That is probably a good option; as Karl Peterson says in the linked article, it keeps everything in pure VB 6 code, which can be a real time saver as well as a boon to future maintenance programmers. The only problem is that you'll still have to process the data somehow once you obtain it. And if that's slow, you're right back where you started from…
Check out this article, which demonstrates how to asynchronously transfer large files using a little-known method in user controls.

Resources