File IO between C/C++ libs and .NET Framework

Discussion in 'C++' started by Eat_My_Shortz, Feb 28, 2005.

  1. I'm trying to interoperate between file IO operations, on an open file,
    between unmanaged C++ code and Managed C++, using the .NET framework.

    Basically, at present, I have a C-style FILE* object, with an open
    file. I would like to do some operations on this file, then while it's
    still open, convert it into a .NET FileStream and do some processing
    there.

    I've noted that .NET's FileStream has a constructor:
    public FileStream(IntPtr handle, System.IO.FileAccess access)
    I assume the IntPtr argument is designed to interop with unmanaged
    code. I've tried passing the FILE* in there, but get the error "The
    handle is invalid" (Note this is a perfectly valid open file, opened
    with fopen and can be read/written to using fread/fwrite/fgetc/fprintf,
    etc).

    So, next I go looking for the C++ library's file operations. I try some
    sample code with the std::eek:fstream object:
    std::eek:fstream outData;

    outData.open("test.txt", ios::app);

    outData << "Hello World!" << endl;

    // Now add .NET Framework FileStream
    IntPtr odNet = IntPtr(&outData);

    FileStream* fs = new FileStream(odNet, FileAccess::Write);

    Still the same: The Handle is invalid.

    I've googled everywhere, and the most information anyone can provide on
    this overload is that indeed, it does take an IntPtr, which is a Handle
    to a file. Thanks a lot.

    So, can anyone answer:
    1. How do you get a Handle to a file, if this isnt the right way?
    2. Can FileStream actually interop with C's FILE*?
    3. Can FileStream actually interop with C++'s std::eek:fstream?
    4. How do I "plug" it in?

    Note: I'd really rather interop with C's FILE* if that's at all
    possible, since I have a LOT of code using it.

    If not, I assume I just have to store the file position, close the
    file, then reopen it in .NET, and seek to that position again, right?
    Annoying...
    Thanks to anyone who replies.
    Eat_My_Shortz, Feb 28, 2005
    #1
    1. Advertising

  2. Eat_My_Shortz wrote:

    > I'm trying to interoperate between file IO operations, on an open file,
    > between unmanaged C++ code and Managed C++, using the .NET framework.
    >
    > Basically, at present, I have a C-style FILE* object, with an open
    > file. I would like to do some operations on this file, then while it's
    > still open, convert it into a .NET FileStream and do some processing
    > there.
    >
    > I've noted that .NET's FileStream has a constructor:
    > public FileStream(IntPtr handle, System.IO.FileAccess access)
    > I assume the IntPtr argument is designed to interop with unmanaged
    > code. I've tried passing the FILE* in there, but get the error "The
    > handle is invalid" (Note this is a perfectly valid open file, opened
    > with fopen and can be read/written to using fread/fwrite/fgetc/fprintf,
    > etc).
    >
    > So, next I go looking for the C++ library's file operations. I try some
    > sample code with the std::eek:fstream object:
    > std::eek:fstream outData;
    >
    > outData.open("test.txt", ios::app);
    >
    > outData << "Hello World!" << endl;
    >
    > // Now add .NET Framework FileStream
    > IntPtr odNet = IntPtr(&outData);
    >
    > FileStream* fs = new FileStream(odNet, FileAccess::Write);
    >
    > Still the same: The Handle is invalid.
    >
    > I've googled everywhere, and the most information anyone can provide on
    > this overload is that indeed, it does take an IntPtr, which is a Handle
    > to a file. Thanks a lot.
    >
    > So, can anyone answer:
    > 1. How do you get a Handle to a file, if this isnt the right way?
    > 2. Can FileStream actually interop with C's FILE*?
    > 3. Can FileStream actually interop with C++'s std::eek:fstream?
    > 4. How do I "plug" it in?
    >
    > Note: I'd really rather interop with C's FILE* if that's at all
    > possible, since I have a LOT of code using it.
    >
    > If not, I assume I just have to store the file position, close the
    > file, then reopen it in .NET, and seek to that position again, right?
    > Annoying...
    > Thanks to anyone who replies.



    This is off topic in clc++, you should ask in
    microsoft.public.dotnet.languages.vc.


    If it doesn't appear in your news server use the public MS news server:

    msnews.microsoft.com


    I think however that the cleanest way is the one you mentioned in the end.


    Or use one method only.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Feb 28, 2005
    #2
    1. Advertising

  3. Sorry about that. This usenet thing is hard to navigate!
    Thanks, I think I'll just close and reopen the file... hrm. Thx for ur
    help.
    Eat_My_Shortz, Mar 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph
    Replies:
    2
    Views:
    528
    Richard Bos
    Sep 17, 2003
  2. Raman
    Replies:
    5
    Views:
    1,038
    Raman
    May 9, 2008
  3. Freyr
    Replies:
    1
    Views:
    234
    bobicanprogram
    Jul 16, 2009
  4. Michael

    osx framework <> linux libs

    Michael, Oct 7, 2011, in forum: C++
    Replies:
    5
    Views:
    1,060
    Alain Ketterlin
    Oct 7, 2011
  5. Greg Hauptmann
    Replies:
    4
    Views:
    199
    Stefano Crocco
    Feb 7, 2009
Loading...

Share This Page