[Python.NET] Getting an open Python file handle into CLR

Ron Harding rharding64 at yahoo.com
Tue Jul 21 06:19:56 CEST 2015

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 at gmail.com> wrote:

I posted this on StackOverflow but thought it might be a better question 
for this mailing list.


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

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)

Dan Lenski

Python.NET mailing list - PythonDotNet at python.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20150721/f29cdcd5/attachment.html>

More information about the PythonDotNet mailing list