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?
Related
I'm using SSH.NET to create my terminal application for UWP.
For now, I've been able to send/receive data with the library, but I would like to do something like the PuTTY application, that shows the text with different colors, or even being able to edit files with the Linux vi editor.
Is there a way to get color / position information with this library?
When implementing a terminal emulation, you primarily have to process ANSI escape codes sent by the server.
There's no support for that in SSH.NET or .NET Framework.
Implementing it on your own is a huge task. PuTTY implementation of the terminal emulation, terminal.c, has almost 8000 lines of code. And that's only a processing part, a drawing is separate.
Quick google search for "c# terminal emulation" results in:
https://github.com/munificent/malison-dotnet
(though I have no experience with this library)
The only part of this on SSH.NET side, is to request terminal emulation by using an overload of SshClient.CreateShell that takes terminalName argument (and its companions).
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)
On one of my VBScripts, I have to call a C# console application.
AFTER the C# application finishes executing (synchronous), I need to get the output of the C# application, which is a string, into the VBScript.
System.Environment.ExitCode in C# only allows an Integer to be returned.
How can I have get the string that is output by the C# application into the VBScript?
I realize that it is possible to save the output of the C# application to a file and read that using VBScript. However, I was looking for something less messy.
Any solutions/suggestions are welcome.
For example: Is there some way to create a DLL and use this to enable a return string value to be passed to VBScript? I am not sure. This is just a thought. Is there a way to create something similar to Shell.Application BrowseForFolder?
Thanks for all your help!
You can use a WScript.Shell object to execute the C# process, and then read from it's StdOut filestream.
For details, see this tek-tip on the subject.
We have a program (A.exe) with GUI and a toolbar who does NLP stuff with some text.
In that toolbar we have function A which transform the text by adding some xml tags. Someone here (the boss) would like that I create a Web Service calling function A. It told me about Dynamic Data Exchange (he used it few years ago), I saw something like SendMessage.
The Web Service will be used by someone over the Internet : sending a text and getting the result as xml. The GUI program could not be started when someone calls the WS because it is too slow, so someone sugggests to launch this A.exe once for all and the WS will ask this A.exe by sending a DDE call. I don't know how A.exe will react in case of concurrent calls.
The Web Service will:
save the text file in a directory
call the A.exe
the A.exe will compute the text file and create the xml file
the WS will loop until the xml file exist
the WS will get the xml and send it as stream to the original caller
I would like to note that:
DDE is old and seems to need a DDE server capable program.
SendMessage is a little bit obscur as I am a Java developer.
I did not try named or anonymous pipes to make that call as suggested.
Thank you.
PS: It is an heresy to build a WS calling a server-side program with UI, isn't?
The answer to this question really depends on what you are trying to accomplish. Does your function alter data coming from an external data source such as a database or are you invoking a function within an application?
I would start with the following questions.
1) What does the function of the button do? Can it's functionality be moved into a service and shared between multiple applications?
2) Can you put the functionality inside a DLL and share between two applications?
3) Do the two apps really need to communicate with one another?
If the two processes MUST communicate with one another and you merely want application A to behave in response to a message from application B, consider using named pipes for TCP/IP communication. This is simple enough in .NET and provides quick communication between processes.
UPDATE: Here is a link with info on using named pipes: http://msdn.microsoft.com/en-us/library/bb546085.aspx.
UPDATE 2: This is an update after you updated your question. Do you have access to A.exe's source code? If you do, you have two options: 1) move that logic into your service or 2) modify A.exe to accept command-line parameters so that your service can invoke the process and get back results. If you don't have source code and you can't invoke A.exe from the command-line, then there isn't much you can do other than write the process yourself in a form your web service can call.
P.S. You don't have to worry about concurrent calls because each server request will execute a separate process.
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.