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

Ron Harding rharding64 at yahoo.com
Tue Jul 21 21:28:17 CEST 2015

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

 Ron Harding via PythonDotNet <pythondotnet at ...> 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 


Python.NET mailing list - PythonDotNet at python.org

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

More information about the PythonDotNet mailing list