Passing bytes as parameter to c#? - c#

I am currently stuck while trying to call a c# methods from python. I am using python 3.2 and not IronPython. I used pip to install the latest version of python.net
Problem occurs (as often discussed) while using ref or out parameters.
Here is my code so far:
import clr
path = clr.FindAssembly("USB_Adapter_Driver")
clr.AddReference(path)
from USB_Adapter_Driver import USB_Adapter
gpio = USB_Adapter()
version2 = ''
status, version = gpio.version(version2)
print ('status: ' + str(status))
print ('Version: ' + str(version))
readMask = bytearray([1])
writeData = bytearray([0])
print (readMask)
print (writeData)
status, readData = gpio.gpioReadWrite(b'\x01',b'\x00',b'\x00')
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],b'\x00')
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],)
I have had some major issues getting clr. running at all. But in this exact config it seems to work (I need to save the path to a variable, otherwise it wont work, I also cant type the path the dll in clr.AddReference(path) because this wont work as well)
The c# version method looks like this:
public USB_Adapter_Driver.USB_Adapter.Status version(ref string ver)
My status variable gets a value which works perfectly with the status enum for the c# class.
Problem is: after the call my variable "version" is empty. Why? According to: How to use a .NET method which modifies in place in Python? this should be a legal way to do things. I also tried to use the explicit version but my namespace clr does not contain clr.Reference().
The next (and more severe) problem is pio.gpioReadWrite().Here the info about this one:
public USB_Adapter_Driver.USB_Adapter.Status gpioReadWrite(byte readMask, byte writeData, ref byte readData)
Here I get the error message:
TypeError: No method matches given arguments
It doesn't matter which of the calls I use from above. All of them fail.
Here is the full output of a debugging run:
d:\[project path]\tests.py(6)<module>()
status: 6
Version:
bytearray(b'\x01')
bytearray(b'\x00')
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
d:\[project path]\tests.py(28)<module>()
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],)
(Pdb) Traceback (most recent call last):
File "D:\WinPython-64bit-3.4.4.2Qt5\python-3.4.4.amd64\lib\pdb.py", line 1661, in main
pdb._runscript(mainpyfile)
File "D:\WinPython-64bit-3.4.4.2Qt5\python-3.4.4.amd64\lib\pdb.py", line 1542, in _runscript
self.run(statement)
File "D:\WinPython-64bit-3.4.4.2Qt5\python-3.4.4.amd64\lib\bdb.py", line 431, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "d:\[project path]\tests.py", line 28, in <module>
status, readData = gpio.gpioReadWrite(readMask[0],writeData[0],)
TypeError: No method matches given arguments
Hope one of you has an idea on how to fix this.
Thanks,
Kevin

Python.Net doesn't handle ref/out parameters like IronPython.
status, readData = gpio.gpioReadWrite(b'\x01',b'\x00',b'\x00') call is not quite correct since Python.Net will not return an updated readData as second result.
You can handle ref parameters using reflection. Check out my answer to similar question here
there is a rough code template for your case:
import clr
clr.AddReference("USB_Adapter_Driver")
import System
import USB_Adapter_Driver
myClassType = System.Type.GetType("USB_Adapter_Driver.USB_Adapter, USB_Adapter_Driver")
method = myClassType.GetMethod("gpioReadWrite")
parameters = System.Array[System.Object]([System.Byte(1),System.Byte(0),System.Byte(0)])
gpio = USB_Adapter_Driver.USB_Adapter()
status = method.Invoke(gpio,parameters)
readData = parameters[2]

Related

C# - Python interop using Python.Net [No module named '_ctypes']

I am trying to use Python.NET to perform interop between C# and Python on a Windows machine.
Specifically I have the following code.
PythonEngine.PythonHome = #"C:\Python\3_5_4";
PythonEngine.PythonPath = #"C:\Python\3_5_4\Lib;C:\Python\3_5_4\Lib\site-packages";
using (Python.Runtime.Py.GIL())
{
dynamic np = Py.Import("numpy");
dynamic sin = np.sin;
Console.WriteLine(sin);
}
While I can successfully execute a general python statement as such:
var res = PythonEngine.Eval("1 + 1");
Console.WriteLine(res);
//res = 2
Which indicates that the python engine itself is working successfully, I can also invoke something like this:
var html = Py.Import("html");
Console.WriteLine(html);
//html = <module 'html' from 'C:\\Python\\3_5_4\\Lib\\html\\__init__.py'>
Which further indicates that the module loading functionality is also working correctly.
However whenever I attempt to invoke the line:
dynamic np = Py.Import("numpy");
I receive the following error:
{"ImportError : No module named '_ctypes'"}.
[' File "C:\\Python\\3_5_4\\Lib\\site-packages\\numpy\\__init__.py", line 140, in <module>\n from . import _distributor_init\n', ' File "C:\\Python\\3_5_4\\Lib\\site-packages\\numpy\\_distributor_init.py", line 9, in <module>\n from ctypes import WinDLL\n', ' File "C:\\Python\\3_5_4\\Lib\\ctypes\\__init__.py", line 8, in <module>\n from _ctypes import Union, Structure, Array\n']
The file referenced is located # 'C:\Python\3_5_4\Lib\ctypes\__init__.py'.
I have verified that all the expected paths are set correctly and that the Ctypes folder exists in my python path.
I have tried everything from uninstalling and reinstalling Python to modifying the ctypes/__init__.py file to try and import Ctypes directly to no effect.
Having researched this topic online throughly I have found a number of suggestions which point to running yum install libffi-devel as detailed here. However this does not appear to be something that I can perform on windows given that yum appears to be a Linux only application.
Can anyone provide any guidance?

Executing python script with zmq from C# using IronPython not working

I'm using ZeroMQ for inter-process communication between C# managed application and python script. When calling script from C#, using IronPython I get following error :
An unhandled exception of type
IronPython.Runtime.Exceptions.ImportException occurred in
Microsoft.Dynamic.dll
Additional information: cannot import constants from
zmq.backend.cython
Python code (test.py file):
import sys, zmq, time
def execute(input):
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.bind("tcp://*:18800")
time.sleep(1)
publisher.send(input) #just echo input parameter
And this is how I execute .py code from my C# app :
static void Main(string[] args)
{
var options = new Dictionary<string, object>();
options["Frames"] = true;
options["FullFrames"] = true;
ScriptEngine _engine = Python.CreateEngine(options);
ScriptRuntime _runtime = _engine.Runtime;
ICollection<string> _searchPaths = _engine.GetSearchPaths();
_searchPaths.Add(#"C:\Python27\");
_searchPaths.Add(#"C:\Python27\Lib\");
_searchPaths.Add(#"C:\Python27\Scripts\");
_searchPaths.Add(#"C:\Python27\libs\");
_searchPaths.Add(#"C:\Python27\DLLs\");
_searchPaths.Add(#"C:\Python27\include\");
_searchPaths.Add(#"C:\Python27\lib\site-packages");
_engine.SetSearchPaths(_searchPaths);
dynamic wrapperObj = _runtime.UseFile("test.py");
wrapperObj.execute("test");
}
Note that some code is omitted for brevity, and not actually relevant for this scenario. When manually invoking .py script through the command line, everything works fine, I'm able to pass data around and my C# app is receiving messages.
Anyone knows what is this error and how can be solved?
EDIT : While I was desperate and almost gave up, I tried to write quick named-pipes support, and guess what - similar error:
An unhandled exception of type IronPython.Runtime.Exceptions.ImportException occurred in
Microsoft.Dynamic.dll
Additional information: No module named win32file
This time, my test.py looks like this:
import win32file
def execute(input):
handle = win32file.CreateFile(r"\\.\pipe\test_pipe",
win32file.GENERIC_WRITE,
0, None,
win32file.OPEN_EXISTING,
0, None)
if handle:
win32file.WriteFile(self.handle, input, None)
win32file.FlushFileBuffers(self.handle)
win32file.SetFilePointer(self.handle, 0, win32file.FILE_BEGIN)
I'm not really sure if I have more options for this kind of interoperability, but I'm starting to think that IronPython can just cover basic usage scenarios (when calling pure python code from C#), not to mention matplotlib, numpy or astropy that I will certanly need later on.
Thanks in advance!
Regards,
Civa

C#: ArgumentException when calling System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()

I am in a bit of a strange situation. I have been given a fairly large suite of PowerShell modules and functions, and it is my job to tie these together into an executable. The requirements state that this must be a single, standalone executable with no installer and .net 3.5 may be the only dependency. The Windows Management Framework is not an exception and cannot be assumed to exist on the machine. To get around this, I have added System.Management.Automation as a reference and made it an embedded resource, along with all of the PowerShell module files, and load them from reflection at runtime. This seems to work OK, but I have some errors that I cannot seem to figure out and think it might have something to do with this system.
So Here is the issue: When I start to initialize things to run the PowerShell command, I get a strange error that I can't seem to control.
Here is the code:
public static void RunCommand(object objcommand)
{
//create a script block for toolbox once, get the embeded resource, convert from byte array to string, make scriptblock from string
ScriptBlock toolbox = System.Management.Automation.ScriptBlock.Create(System.Text.Encoding.Default.GetString(Properties.Resources.toolbox));
string command = (string)objcommand;
//get the module name
string modname = options.Commands[command]["module"];
//get the module from the embeded resources, convert to string, convert to scriptblock
var module = System.Management.Automation.ScriptBlock.Create(new System.IO.StreamReader(myasm.GetManifestResourceStream("piramids.Resources." + modname + ".psm1")).ReadToEnd());
using (var powerShell = PowerShell.Create())
{
System.Management.Automation.Runspaces.Runspace rs = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); //i think this line triggers the exception
rs.Open();
powerShell.Runspace = rs;
//make the necesary powershell modules of the command availible
powerShell.AddCommand("new-module").AddParameter("ScriptBlock", toolbox).Invoke();
powerShell.AddCommand("new-module").AddParameter("ScriptBlock", module).Invoke();
//if inethistory, make dlls availible
if (modname.Equals("inethistory"))
{
powerShell.AddCommand("add-type").AddParameter("Path", sqldll).Invoke();
powerShell.AddCommand("add-type").AddParameter("Path", esentdll).Invoke();
}
ICollection<PSObject> output = new List<PSObject>(0);
try {
output = powerShell.AddCommand("get-" + command).AddCommand(format).AddCommand("out-string").Invoke();//pipeline.Invoke();
} catch (System.Management.Automation.RuntimeException e)
{
Console.Error.WriteLine("An Error occured while executing '" + command + "'");
Console.Error.WriteLine(e.Message);
}
//do stuff with the results
and here is the stack trace:
Unhandled Exception: System.ArgumentException: The path is not of a legal form.
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.Path.GetFullPath(String path)
at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
at System.Management.Automation.PSVersionInfo.GetPSVersionTable()
at System.Management.Automation.PSVersionInfo.get_PSVersion()
at Microsoft.PowerShell.DefaultHost..ctor(CultureInfo currentCulture, CultureInfo currentUICulture)
at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
at piramids.Program.RunCommand(Object objcommand)
at piramids.Program.Main(String[] args)
I believe this line is where the exception occurs:
System.Management.Automation.Runspaces.Runspace rs = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();
The CreateRunspace method is not documented to throw any exceptions, and this exception comes from so many levels down that I have no idea what kind of path this thing is checking, as I never called a function that asked for a path.
I am stumped. Does anyone have any idea what may be causing this?
EDIT: After some digging, here is what I found. PSVersionTable is a static field of VersionInfo, so the static constructor is called the first time get called for this field. The static constructor calls an internal method called GetBuildVersion, which tries to get the assembly location of PSVersionInfo. According to This documentation page:
If the assembly is loaded from a byte array, such as when using the Load(Byte[]) method overload, the value returned is an empty string ("").
I am loading from a byte array, so this will be an empty string. But then GetBuildVersion uses this location to do FileVersionInfo.GetVersionInfo which verifies the path with Path.GetFullPath. According to This documentation page:
ArgumentException:
the path is a zero-length string
So there is the problem. Now the question is, How do I assign a location to an assembly loaded from a byte array? May God have mercy on me.
I'm not at all convinced this is even remotely reasonable to expect PowerShell code to work without installing WMF. If I were approached with that request I would respond that all code must be rebuilt in another .NET language (that is, C#).
Still, perhaps you can see if it's this static method. You'll have to de-PowerShell the code I'm afraid. The PowerShell accelerator is just a simple way for me to get at the System.Management.Automation assembly. The class is not public and the method on the class is not public either.
$verInfo = [PowerShell].Assembly.GetTypes() | Where-Object Name -eq 'PSVersionInfo'
$verInfo.GetMethod('get_PSVersion', [System.Reflection.BindingFlags]'NonPublic,Static').Invoke($null, [System.Reflection.BindingFlags]'NonPublic,Static', $null, #(), $null)
Chris

Build visual studio solution using msbuild from c# code

I want to build my solution file from other c# code using msbuid I have tried
var msbuild_path = #"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
var solution_path = #"D:\Sumit\WorkingCopy\Final\Final.sln";
Process.Start(msbuild_path + " " + solution_path);
but this one throws an error Please help me out!!
According to https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx , the Process.Start method takes two arguments:
public static Process Start(string fileName, string arguments)
So you should change your code to
Process.Start(msbuild_path, solution_path);
What you were doing before was actually trying to run a file named "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe(space)D:\Sumit\WorkingCopy\Final\Final.sln", but no such file exists with that name. The msbuild.exe may exist, but "msbuild.exe D:\Sumit...\Final.sln" is not the filename you meant to pass as the command filename. Also, the argument string was empty, so the system assumed you did not want to pass any arguments to "msbuild.exe D:\Sumit...\Final.sln". But the error message was because the two filenames were mashed into one filename.
Windows allows filenames to contain embedded spaces, which frequently causes problems in dealing with command-line arguments.

Rapi.dll / OpenNETCF Invoke example

I'm failing to use the Invoke method of the OpenNETCF Rapi.dll.
Can somebody please post an example? I can't seem to find one anywhere on the web.
Other function calls worked fine, but I couldn't figure out Invoke.
(dllPath was OK)
MyDll is in the root of the CE device.
It has a FindAndKill method which needs one string argument
I've tried this:
var rapi = new CODMrapi.CODMrapi(dllPath);
var encoding = new System.Text.UTF8Encoding();
rapi.Connect();
byte[] inputData = encoding.GetBytes(fileName);
byte[] outputData;
rapi.Connect();
rapi.Invoke("\\MyDll.dll", "FindAndKill", inputData, out outputData);
rapi.Disconnect();
You need to debug to determine what's happening. Add in ::MessageBox calls to the DllMain and the method to see if the DLL is even getting loaded and if the method is getting called. If they are not, make sure that your method is publicly exported from the DLL and the name wasn't mangled (dumpbin is a good tool for this)

Categories

Resources