Getting an open Python file handle into CLR
Hi, I posted this on StackOverflow but thought it might be a better question for this mailing list. http://stackoverflow.com/questions/31530059/how-can-i-open-a-net- filestream-object-from-a-python-file-handle I'm trying to figure out how to transform an open Python file handle into a System.IO.FileStream object so that I can pass it to C# code. Here's what I've got so far. This produces the good ol' win32 (HANDLE*) object, I believe: import clr, msvcrt from Microsoft.Win32.SafeHandles import SafeFileHandle from System.IO import FileStream, FileAccess from System import IntPtr, Int32, Int64 pyf=open("c:/temp/testing123.txt","w") fileno=pyf.fileno() print fileno # 6 handle = msvcrt.get_osfhandle(fileno) print handle # 1832L Unfortunately, I can't figure out how to convince the clr module to cast this handle into something that I can use as an IntPtr, as required by the constructors of FileStream or SafeFileHandle. I've tried various versions of the following, but they all give me TypeError ("value cannot be converted to System.IntPtr"): FileStream(IntPtr(handle), True) FileStream(IntPtr(Int32(handle), True) SafeFileHandle(IntPtr(handle), True) Thanks, Dan Lenski
perhaps re-directed command line calls for stdin, stdout, stderr in c# interacting with python i/o command line calls(although in python it is common practice to daisy chain commands). i have used this many times to get out of scrapes for 3rd party process calls in c#. good luck! Ron “The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind. ”Nikola Tesla“Radio Power Will Revolutionize the World” (Modern Mechanix & Inventions, July, 1934) On Monday, July 20, 2015 9:05 PM, Dan Lenski <dlenski@gmail.com> wrote: Hi, I posted this on StackOverflow but thought it might be a better question for this mailing list. http://stackoverflow.com/questions/31530059/how-can-i-open-a-net- filestream-object-from-a-python-file-handle I'm trying to figure out how to transform an open Python file handle into a System.IO.FileStream object so that I can pass it to C# code. Here's what I've got so far. This produces the good ol' win32 (HANDLE*) object, I believe: import clr, msvcrt from Microsoft.Win32.SafeHandles import SafeFileHandle from System.IO import FileStream, FileAccess from System import IntPtr, Int32, Int64 pyf=open("c:/temp/testing123.txt","w") fileno=pyf.fileno() print fileno # 6 handle = msvcrt.get_osfhandle(fileno) print handle # 1832L Unfortunately, I can't figure out how to convince the clr module to cast this handle into something that I can use as an IntPtr, as required by the constructors of FileStream or SafeFileHandle. I've tried various versions of the following, but they all give me TypeError ("value cannot be converted to System.IntPtr"): FileStream(IntPtr(handle), True) FileStream(IntPtr(Int32(handle), True) SafeFileHandle(IntPtr(handle), True) Thanks, Dan Lenski _________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
The problem is the conversion from a Python long to a C# number, not anything to do with the file handle itself. This worked for me: (after your "print handle") cs_handle = IntPtr.Overloads[Int64](Int64(handle)) fs = FileStream(cs_handle, True) Jeff On Mon, Jul 20, 2015 at 9:19 PM, Ron Harding via PythonDotNet < pythondotnet@python.org> wrote:
perhaps re-directed command line calls for stdin, stdout, stderr in c# interacting with python i/o command line calls(although in python it is common practice to daisy chain commands).
i have used this many times to get out of scrapes for 3rd party process calls in c#.
good luck!
Ron
“The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind.
”Nikola Tesla“Radio Power Will Revolutionize the World” (Modern Mechanix & Inventions, July, 1934)
On Monday, July 20, 2015 9:05 PM, Dan Lenski <dlenski@gmail.com> wrote:
Hi, I posted this on StackOverflow but thought it might be a better question for this mailing list.
http://stackoverflow.com/questions/31530059/how-can-i-open-a-net- filestream-object-from-a-python-file-handle
I'm trying to figure out how to transform an open Python file handle into a System.IO.FileStream object so that I can pass it to C# code.
Here's what I've got so far. This produces the good ol' win32 (HANDLE*) object, I believe:
import clr, msvcrt from Microsoft.Win32.SafeHandles import SafeFileHandle from System.IO import FileStream, FileAccess from System import IntPtr, Int32, Int64
pyf=open("c:/temp/testing123.txt","w") fileno=pyf.fileno() print fileno # 6 handle = msvcrt.get_osfhandle(fileno) print handle # 1832L
Unfortunately, I can't figure out how to convince the clr module to cast this handle into something that I can use as an IntPtr, as required by the constructors of FileStream or SafeFileHandle.
I've tried various versions of the following, but they all give me TypeError ("value cannot be converted to System.IntPtr"):
FileStream(IntPtr(handle), True) FileStream(IntPtr(Int32(handle), True) SafeFileHandle(IntPtr(handle), True)
Thanks, Dan Lenski
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
Jeffrey Bush <jeff@...> writes:
The problem is the conversion from a Python long to a C# number, not
anything to do with the file handle itself.
This worked for me: (after your "print handle")
cs_handle = IntPtr.Overloads[Int64](Int64(handle)) fs = FileStream(cs_handle, True)
Aha, thanks you very much! Looks like "Overloads" is the missing ingredient for C-style casting. I see now that that is in the docs, but I had overlooked it. Dan
Ron Harding via PythonDotNet <pythondotnet@...> writes:
perhaps re-directed command line calls for stdin, stdout, stderr in c# interacting with python i/o command line calls(although in python it is common practice to daisy chain commands).
Unfortunately, I cannot redirect the output of the C# library to a convenient filehandle like stdout/stderr; I need to be specify a FileStream object. It pretty much comes down to this: how do I cast a win32 file handle to an IntPtr? Thanks, Dan
how do I cast a win32 file handle to an IntPtr?
i ran into this in the past as well; specifically when i started using FTDI USB devices on DLPdesign development kits. the library is written in c++ MFC code. After providing data marshal/platform invoke i applied the intptr in c#. The USB device that I am accessing, passes a file handle to indicate specific pass / fail condition. i.e. the first command opens a connection to the device, the expected handle is 'OK'. if not one of many different possible error messages is passed in the file handle. SummaryOpen the device and return a handle which will be used for subsequent accesses.DefinitionFT_STATUS FT_Open (int iDevice, FT_HANDLE *ftHandle)ParametersiDevice Index of the device to open. Indices are 0 based.ftHandle Pointer to a variable of type FT_HANDLE where the handle will bestored. This handle must be used to access the device.Return ValueFT_OK if successful, otherwise the return value is an FT error code. public class Wrapper{private static IntPtr m_USBhandler; [DllImport("FTD2XX.dll", EntryPoint="FT_Open")]public static extern uint FT_Open(int iDevice, ref IntPtr ftHandle); [DllImport("FTD2XX.dll", EntryPoint="FT_Close")]public static extern uint FT_Close(IntPtr ftHandle); [DllImport("FTD2XX.dll", EntryPoint="FT_Read")]public static extern uint FT_Read(IntPtr ftHandle, byte[] lpBuffer,uint dwBytesToRead, ref uintlpdwBytesReturned); [DllImport("FTD2XX.dll", EntryPoint="FT_Write")]public static extern uint FT_Write(IntPtr ftHandle, byte[] lpBuffer,uint dwBytesToWrite, ref uintlpdwBytesWritten); [DllImport("FTD2XX.dll", EntryPoint="FT_Purge")]public static extern uint FT_Purge(IntPtr ftHandle, uint dwMask); public static bool OpenDevice(){if (((FT_Status)Wrapper.FT_Open(1, ref m_USBhandler)) ==FT_Status.Ok){return true;}else{return false;}} i hope this helps you by seeing one way of applying file handles in c#. Ron “The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind.”Nikola Tesla“Radio Power Will Revolutionize the World” (Modern Mechanix & Inventions, July, 1934) On Tuesday, July 21, 2015 11:12 AM, Dan Lenski <dlenski@gmail.com> wrote: Ron Harding via PythonDotNet <pythondotnet@...> writes:
perhaps re-directed command line calls for stdin, stdout, stderr in c# interacting with python i/o command line calls(although in python it is common practice to daisy chain commands).
Unfortunately, I cannot redirect the output of the C# library to a convenient filehandle like stdout/stderr; I need to be specify a FileStream object. It pretty much comes down to this: how do I cast a win32 file handle to an IntPtr? Thanks, Dan _________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
As I said in my message, you can using IntPtr.Overloads[Int64](handle). Jeff On Tue, Jul 21, 2015 at 11:12 AM, Dan Lenski <dlenski@gmail.com> wrote:
Ron Harding via PythonDotNet <pythondotnet@...> writes:
perhaps re-directed command line calls for stdin, stdout, stderr in c# interacting with python i/o command line calls(although in python it is common practice to daisy chain commands).
Unfortunately, I cannot redirect the output of the C# library to a convenient filehandle like stdout/stderr; I need to be specify a FileStream object.
It pretty much comes down to this: how do I cast a win32 file handle to an IntPtr?
Thanks, Dan
_________________________________________________ Python.NET mailing list - PythonDotNet@python.org https://mail.python.org/mailman/listinfo/pythondotnet
participants (3)
-
Dan Lenski -
Jeffrey Bush -
Ron Harding