VS2010, C#, Python: Python script file execution on runtime - c#

I wish to create a sample program in C# where it downloaded real time data (ie datalogger) via USB port. C# accept the data (hex or numerical) and then automatically download a python file (ie validatemethodA.ps) which is then executed. The python accept the data from the C# code and then do validation check on data or do complex calculation and return results back to C#, where the data is displayed on window form and appended to the file (by C#).
The Python is a script, where it does not requires complies in advance (during c# runtime) this mean the user is free to modify the python script to optimise pass/fails parameter or calculation results without getting back to VS2010 to complies the whole program.
This mean I can supply release C# product and only user can modify the python file.
How this is achieved?, is there sample/demo, can anyone link me to this solution so I can experiment this proof of concept?

Have you looked at IronPython?
There is a whole bunch of information on Stack Overflow including a number of particularly relevant questions such as IronPython - Load script from string in C# 4.0 application.
And plenty more on the web.

There are a couple of ways to do this...one would be to "shell out" to Python by invoking a system call from your C# program. There, you would be looking to use the .NET runtime Process.Start method to run the Python interpreter as a subprocess from your C# program.
Another option would be to use a tighter integration between C# and Python by using the IronPython interpreter. IronPython (ipy), is the python interpreter implemented in .NET
ipy can run Python code (essentially the same as normal "c" python, but with some differences), but can also import .NET assemblies and interact with the .NET runtime, directly.

Related

Is it possible/better to use PHP to execute C# code from the file or is it necessary to make it an executable?

I am making a PHP webpage that will retrieve data from a database and allow a user to send that data as an object to a database utilized by another application. Because of the API of the application, the object I need to send it as requires .NET.
The C# page takes in the necessary constructors, creates the object, and sends it. This part of my code functions perfectly when run within Visual Studio or when ran as a .exe from the command line. I can, presumably, run exec() on the executable and have it run that way within my PHP page.
However, I am hesitant to develop it this way. It seems like there should be a way to simply have a/some PHP file(s) within the same project and call the C# file directly.
I am very new to both PHP and C#. I have searched for both solutions, and I have only found that it is possible to run an executable that uses C# within PHP and seen examples of C# using PHP, but nothing that addresses using an executable over referencing the file directly, or even anything about just referencing a C# file within a Visual Studio project.
To clarify the question, I am wondering if it is possible to execute code from a C# file directly with PHP, and if so, is it a better practice than using exec() to run an executable version of the C# project.
EDIT: I want to clarify that I know PHP isn't naturally available within Visual Studio.
Yes, it's possible to execute C# code directly from PHP. The title of your question suggest you want to execute C# code that is compiled in .dll form not as an executable application .exe Excute C# Code From File
Using a DLL With PHP for Dummies
Call C-Sharp Using PHP
Make it executable than call it in php by
<?php
exec('file.exe');
?>
You can also read this article: Calling .Net Framework and .Net Assemblies from PHP

Converting c# code to C++ native code or for x86 Processor

How can i make my C# console application able to run without .Net frame work
e.g convert it to native c++ code or to some other method so that it will run without .Net frame work.
Edit:
or how can i compile c# code as a native c++ code
I wouldn't recommend using tools because there will be complications and it will be hard to test. However if you want to try there are a lot of tools which you are able to find with a simple google search action. For example here.

in C#, calling third party API (C++)

Was wondering if anyone would be so kind to point me in the right direction on how to call a third party's C++ DLL in C#. The API is closed source but the header files are available.
I've read up on using P/Invoke but after further diving, it's leading to marshalling very complex data structures across, and that's IF I can do the declarations for the third party classes/functions.
If there's any reading material / tutorials out there you can recommend or just point me in the right direction, I would appreciate it.
Thanks so much in advance
I found http://www.pinvoke.net/ to be very helpful.
Also the http://clrinterop.codeplex.com/releases/view/14120
Additionally, what I did that I found helpful was to do a test solution. Start off with making a C# console app that initially does nothing, then make another Project (File, New, Project, Visual C++, Win32 Console Application)
When the Wizard appears, choose DLL, Export symbols, (ATL and/or MFC if you need that).
When the Project appears, compile it as is and copy the DLL to you output folder of you C# Project
Use the P/Invoke Interop Assistant to generate the C# method based on the header (I'd start with the function that takes no params and returns an int.)
then call that from your C# console app.
Once I'd established how it worked with something really simple it was much easier to expand a bit at a time.
Try writing the C# calls yourself and then use the P/Invoke Interop Assistant to double check. Best wishes.

Running C++ Qt app from C#

Introduction: I have qt-opencv-multithreaded - Qt/OpenCv application writen in C++ (running on Windows). But I also have another application written in C#.
C++ app does analysis of video and identifies robots (position, rotation). And stores it in variable inside ProcessingThread.
C# app receives these values and decides where should robots move.
I decided to use C# project as startup project. And I tried to create C++/cli wrapper for qt-opencv-multithreaded .
Questions: Now I have two questions:
How do I run the Qt app from another project? I tried to add include MainWindow.h to wrapper project and create instance from it. I added also directory of qt-opencv-multithreaded to "Additional Include Directories" but it wants also qt directories and I am worried that it is not a right way to do it. So how should I do it?
I know I need to use wrapper. But how do I sent data stored in list of robots in C++ project in processing thread to C# project? (where robot includes cv::Point2i position and cv::Point2i rotation and some other not important inner class) I tried to "pseudoserialize" values into string, send it to C#, and then "deserialize" it to prepared object. But it seems somehow wrong. Is there simple way to do what I need?
Thank you
If you want to run it as a stand-alone process, C# provides facilities for doing so.
If you want to call individual functions of your C++ code from C#, look into P/Invoke

Emulating PHP page to get response in C#

I browsed internet but haven't found my answer... as I'm running out of ideas, I'd appreciate some help on this issue.
So here it is:
I have some PHP code that takes a path to a file and returns a bunch of information that i need.
I have the source code (it is open source), and i know my C#.
My problem is i want to be able to use this PHP code from a C# application, offline, emulating the PHP code.
Any ideas?
Thanks.
If you really must, you can always launch the PHP interpreter from C# by spawning a process. I'll leave the details to MSDN. Better, though, would be to just translate the PHP to C# by hand.
You could set up a PHP server on the local machine and invoke the file using an HTTP request created with c# (such as with the WebRequest object). This satisfies the requirement of working offline and since you have the PHP code, you can make minor modifications to accommodate whatever parameters you need to pass into it.
However unless this is for an isolated case in a controlled environment, I would not suggest it.
If this is for distribution to user machines it would be a) impractical to deploy and b) a maintenance nightmare. If you are running on a server and rewriting the PHP in c# is not an option, it should get the job done.
It sounds like to either wish to
Translate the PHP code to C#
Call the PHP code from .NET
???
I don't think emulation is the term you're looking for, exactly.
If you can link to the PHP code (if it's OSS, is there a site with more information?) or paste it here, we can probably let you know the best way to handle this.
If you don't want to translate the PHP code into C# then you must invoke the PHP interpreter.
First, look at the license on the PHP code you would be using. When I looked at the code earlier I noticed that all but one PHP file had a GNU GPL license on it. GNU GPL should work for what you want, but I recommend that you review the GNU GPL if you haven't done so already. You should also look into what license is supposed to be applied to the last file before continuing.
Second, rewrite the entry file. The file in question is upload_file.php. This file loads the other files as includes, parses the SC2 replay file, and generates an output for use by a web browser. You just need to rewrite this file to have it output a simplified version of its output to the console. You might have it receive the name of the replay file from command line arguments. The other files that are marked as GNU GPL can just be copied directly into your project. I should also say here that there may already be a console version. Take a minute to look around for one.
Third, write a C# application. Invoke the PHP interpreter telling it the what file to interpret, and the name of the replay file to parse. You will need to redirect the console output from your PHP instance into your C# code to be able to read the output.
Fourth, read the output.
Fifth, parse the output for use in your C# application.
Old thread but figured it could use an answer.
You could use the codeplex Phalanger. http://phalanger.codeplex.com/ It allows you to take existing php files and compile them to .net

Categories