I have a program that has c/c# abilities, and I have python. I want that program to update a text file, almost in milliseconds, and have the python to read that text file in milliseconds as well. How can I go achieve this?
Is it possible for a text file to be updated live by another program and be read live by python? Is there any alternative way to do this instead of relying on text file.
Basically what I want to do is a bunch of computations on live data from that program using python and send back those computations to the program in form of commands.Can a file not be closed and reopened and yet updated in the memory?
If you start the C/C# process from python with subprocess.Popen then your two programs can communicate via the stdin and stdout pipes:
c_program = subprocess.Popen(["ARGS","HERE"],
stdin = subprocess.PIPE, # PIPE is actually just -1
stdout= subprocess.PIPE, # it indicates to create a new pipe
stderr= subprocess.PIPE #not necessary but useful
)
Then you can read the output of the process with:
data = c_program.stdout.read(n) #read n bytes
#or read until newine
line = c_program.stdout.readline()
Note that both of these are blocking methods, although non blocking alternatives exist.
Also note that in python 3 these will return bytes objects, you can convert into a str with the .decode() method.
Then to send input to the process you can simply write to the stdin:
c_program.stdin.write(DATA)
Like the read above, in python 3 this method expects a bytes object. You can use the str.encode method to encode it before writing it to the pipe.
I have extremely limited knowledge of C# but from limited research it seems that you can read data from System.Console.In and write data to System.Console.Out, although if you have written programs in C# that run in a terminal, the same methods used to write data to the screen and read input from the user will work here too. (you can imagine the .stdout as the terminal screen and data python writes to .stdin the user input)
Related
I know the title sounds a little weird, but I'm wondering if there is a good way to pass a string value in python to a string value in c#?
I have a neural-network that I am using in python to detect objects in an image. For each object that is detected, it also gets an identifier of what the neural-network thinks it detected which is stored in a string. I also have a C# application that I am creating, that runs my Python script through a bat file. What I want to do is get the string identifier from my Python script so I can use it as a string in my C# application. I've though about using a txt file where I would output the string from my Python script and have my C# application read the txt back. Is there any better ways to achieve this out there?
You can pass your string as a command line argument to the .net program (e.g. myprog.exe)
Then read it out with Environment.GetCommandLineArguments
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/
if you need to do communication back and forth the easiest way to achieve that is by using a TCP/UDP client/server. The are available in c# and python. There are other mechanisms like named pipes on windows but tcp is the easiest.
This actually has a good sample you could use, but google around;
Send Data from [Python Client] to [C# Server] using Socket
This is called inter process communication btw, which is a great google term.
Also see:
What is the simplest method of inter-process communication between 2 C# processes?
In the sake of this game I'm modding at the moment, which has archives ending in ".rpf" (which is short for 'rage engine package file')
How exactly is it that programmers, can find ways to open these types of files/archives, without having access to the rage engine?
What would one need to know in order to even begin trying to open a foreign file extension? The files can be opened thanks to the OpenIV Team who created the program necessary for opening the files, but how exactly does a developer, figure out or even know where to start when it comes to developing an application that is to fulfill the task of opening another file?
It's called Reverse engineering
You look into file with hex editor, notice some texts, or numbers that look like offsets. You apply your own experience writing similar stuff, make some assumptions and check if it is correct for multiple entries, and so on and so forth.
There many ways to open such files.
First you can use specific programs to open them like OpenIV for RPF archive.
If you would try to Mod or write a cheat for example, most people disassemble the program or open them with a Hex-Editor.
Programs like HexEdit change the binary values of a program into hexadecimal numbers, for example the byte 10100101 into Hex 0xA5h (A5).
Another way is to disassemble the program. Programs like ollyDbg, IDA Pro or other disassemble the binary values into assembly-code. Now you're able to search for some offsets, adresses and texts and you can remove or edit them to let the program do what you want.
Some programs are able to generate a pseudocode to C or C# e.g. (.NET Reflector) which helps you to understand what the program do.
After you read for example some memory adresses and their offsets, you can change them in the disassemble program itself (JNZ (Jump if not Zero) to JMP (Jump) for example to jump directly in every case) and write these code on the executable or you can implement them in a own program which changes them or patch them.
That is the princip you looking up for to understand how the program does work and then you add some features of your own or write a complete new application to fulfill the task of opening any file. Like Vlad said thats simply called reverse engineering.
This question already has answers here:
C# Console receive input with pipe
(8 answers)
Closed 7 years ago.
I'm trying to read in a text file from the command prompt in C#, via
program.exe < textfile.txt
However, I cannot find the correct way to do this.
So far, I've only been able to pass the path and the filename to string [] args and then opening the file with the StreamReader class. While this is an acceptable alternative, I've been told that the method with "<", which I suppose is a redirection of standard input, offers advantages like not requiring file handling.
Can anyone give some insight into this?
edit: Program.exe is my C# application.
You've got the right idea - the '<' symbol means the Console class reads from the file you specify, instead of reading user input from the console. When you do this, you read from it using the Console class.
The advantages of reading from STDIN is that the user can either run the program as program.exe, and manually type the input the program is expecting, or they can run it as program.exe < input.txt. The only time this is a disadvantage is if you know you will always supply a file, and consider the effort of typing the '<' symbol too much...
In a command prompt the < sign is one of several redirection operators. Specifically, the < sign is the input redirection operator. When you type this: program.exe < textfile.txt, you are telling the command prompt to execute program.exe and read the command input from a file, instead of reading input from the keyboard. In this way, the command prompt basically opens textfile.txt and takes its content and "stuffs it" into the keyboard buffer, so, as far as program.exe is concerned, the input is being read from the keyboard and has no idea you are actually "stuffing" the keyboard buffer with contents from a file.
If your program currently is reading from a file, you will need to modify your program. You no longer want to read from a file and instead read from the keyboard, using commands such as Console.ReadLine or Console.Read or Console.ReadKey.
As far as advantages, the advantages are minimal.
I have a particular problem, I have some program that I cannot modify but that provides some functionality I'd like to use inside office. So I am writing a plugin for Office that takes my document, executes the program on the background, puts the document on the stdin. The program writes to the stdout, and I take that back to my program to post process that.
This all works fine except that the program asks for a password which I don't want to put on stdin. The tool has a way to read the password from any other input stream but it needs the number of the file-descriptor it should read from.
So here is my question: how do I (within the .net environment) open a stream on a file descriptor with a number that I can give as parameter to this program? Ideally I want to write something like:
process.start("start-program --password-fd " + x);
stream = new StreamWriter(x);
stream.write("secritpwd");
ect.. (but then magically corrected so it will work ;) )
I hope someone can help me.
Thanks
I'm not sure exactly what this app means by "file descriptor", but you may be able to pass the handle of an inheritable anonymous pipe. See AnonymousPipeServerStream. (This assumes you're on at least .NET 3.5.)
The basic outline would be something like this:
Instantiate an AnonymousPipeServerStream.
Pass the pipe handle (pipeServer.GetClientHandleAsString()) as a command-line parameter to your C executable.
Write to the AnonymousPipeServerStream.
File descriptors aren't part of Windows - they're part of the C runtime library. You would have to write a DLL in C or C++ to do your file I/O, then call it from your C# program. Get the file descriptor number from the C DLL to pass to your other code.
Maby this links will help you to start and to get the logic from:
How to use OpenFileById to open a file
Opening pipe connection to a file descriptor in C#
OpenFileById Function
I work for a company that makes application's in C#.
recently we got a customer asking us to look in to rebuilding an application written in PHP.
This application receives GPS data from car mounted boxes and processes that into workable information.
The manufacturer for the GPS device has a PHP class that parses the received information and extracts coordinates. We were looking in to rewriting the PHP class to a C# class so we can use it and adapt it. And here it comes, on the manufacturers website there is a singel line of text that got my skin krawling:
"The encoding format and contents of the transmitted data are subject to constant changes.
This is caused by implementations of additional features by new module firmware versions which makes it virtually impossible to document it and for you to properly decode it yourself."
So i am now looking for a option to use the "constantly changing" PHP class and access it in C#. Some thing link a shell only exposing some function's i need. Except i have no idea how i can do this. Can any one help me find a solution for this.
I know it's a really hacky solution, but if you need a bit of PHP code that you don't want to have to repeatedly port to C# each time, you could try the following approach, although it means that you would need the php command line tool on the target machine.
First step is to have a php script that continously reads data off stdin, decodes it using this special class from the vendor, and writes the result out to stdout. Really simple example:
<?php
include("VendorDecodingClass.php");
while(true)
{
$input = fgets(STDIN); //read off of the stdin stream
//can't remember if this is valid, but somehow check that there is some data
if($input)
{
//pass it off to the vendor decoding class
$output = VendorDecoding::decode($input);
fwrite(STDOUT, $output); //write the results back out
}
//sleep here so you don't suck up CPU like crazy
//(1 second may be a bit long tho, may want usleep)
//Edit: From Tom Haigh, fgets will block, so the sleep isn't necessary
//sleep(1);
}
?>
Anyway, once you have that in place, in your C# application, right at the start, create a new Process to run that script and then save the Process instance somewhere, so you can reference the STDIN and STDOUT at a later point. Example:
ProcessStartInfo procStartInfo = new ProcessStartInfo("php", "yourscript.php");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process(); //store this variable somewhere
proc.StartInfo = procStartInfo;
proc.Start();
Then, when you want to decode your data, you just write to the stdin of the php process you created, and wait for a response on the stdout. Using the stdin/stdout approach is a lot more efficient than creating a new process each time you want to decode some data, because the overhead of creating that process can be noticeable.
proc.StandardInput.WriteLine(somedata); //somedata is whatever you want to decode
//may need to wait here, or perhaps catch an exception on the next line?
String result = proc.StandardOutput.ReadLine();
//now result should contain the result of the decoding process
Disclaimer here, I haven't tested any of this code, but that is the general gist of how I might do it.
Something else I just thought of, you will want some mechanism for terminating that PHP process. It may be OK to use Process.Kill, but if the decoding does any file IO, or anything critical you may want to send an interrupt signal to the php script somehow.
I assume the php script is on your machine and returns usefull data. The first -not very elegant solution- that pops into my mind is the following:
Make sure your machine has the php commandline installed, so that you are able to run the php script from commandline. To execute a commandlinetool from C# see code for that here. The returned data now probably needs to get processed my your C# program.
I have never tried this and do not know anyone that has, but I remember comming across this sometime ago and thought I would throw it out there as a possible option for you.
Phalanger is a compiler project that compiles PHP code to IL, so you can use that then have a managed assembly that you reference from your code directly.
If the format is a regex you can try to put it in an application setting file (not resources, these are compiled WITH the application, you can't change them without recompiling the app).
Application settings are not changeable by the user but you can do that by editing the XML.
Or you can set the settings to user mode and then you can change the format from inside your application code.
Why don't you just launch the PHP script from C#, have it output its results to a file and then use that file as input for your C# program?
Personally, I would setup a PHP web service with a proper and stable API that the C# project can access, implement the manufacturers supplied PHP class in the web service and let it be.