473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

P/Invoke and FILE*

Hy!

I have to invoke a function that has a FILE pointer for its parameter.
e.g.
int myfunct(FILE *f);

Does anyone know how can P/Invoke such function? And would it be possible to
pass StreamReader as a parameter to an invoked function (I know it is not
possible to do so directly)?
Thanks in advance
- Samo
Nov 16 '05 #1
4 1865
Does anyone know how can P/Invoke such function?
Well first you have to aquire a FILE* from some function that uses the
same C runtime library as the DLL containing myfunc.

And would it be possible to
pass StreamReader as a parameter to an invoked function (I know it is not
possible to do so directly)?


Not directly, no.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
> I have to invoke a function that has a FILE pointer for its parameter.
e.g.
int myfunct(FILE *f);
The FILE structure is (AFAIK) an internal of the standard C library, so you
won't be able to recreate it in C#. I think it can even differ between
debug/release or single-threaded/multi-threaded build. Best way to go is
probably building a wrapper in managed C++.
Does anyone know how can P/Invoke such function? And would it be possible to pass StreamReader as a parameter to an invoked function (I know it is not
possible to do so directly)?


I think there are microsoft-specific C functions that enable you to create a
FILE from a win32 HANDLE. Maybe you can create an unnamed pipe, connect one
end to the FILE, and another to your StreamReader.
Of course, this only works if your C-function treats the file as a stream,
too (i.e. doesn't reposition the file pointer)

Niki
Nov 16 '05 #3
Oh my... whole bunch of stuff... :)
I think it would be beteer to dig in and rewrite this one in pure C# since
this wouldn be such on overkill.
;)

But thanks for your help.

"Niki Estner" <ni*********@cube.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have to invoke a function that has a FILE pointer for its parameter.
e.g.
int myfunct(FILE *f);
The FILE structure is (AFAIK) an internal of the standard C library, so

you won't be able to recreate it in C#. I think it can even differ between
debug/release or single-threaded/multi-threaded build. Best way to go is
probably building a wrapper in managed C++.
Does anyone know how can P/Invoke such function? And would it be possible
to
pass StreamReader as a parameter to an invoked function (I know it is
not possible to do so directly)?


I think there are microsoft-specific C functions that enable you to create

a FILE from a win32 HANDLE. Maybe you can create an unnamed pipe, connect one end to the FILE, and another to your StreamReader.
Of course, this only works if your C-function treats the file as a stream,
too (i.e. doesn't reposition the file pointer)

Niki

Nov 16 '05 #4
On Sun, 20 Jun 2004 14:55:21 +0200, "SamoK" <sn***@wetsoftware.com>
wrote:
Hy!

I have to invoke a function that has a FILE pointer for its parameter.
e.g.
int myfunct(FILE *f);

Does anyone know how can P/Invoke such function? And would it be possible to
pass StreamReader as a parameter to an invoked function (I know it is not
possible to do so directly)?
Thanks in advance
- Samo


Well, treat the FILE pointer in C# as an IntPtr. You can then import
such function using C# P/Invoke like so:

[ DllImport("YourLibrary.dll") ]
public static extern int myfunct(IntPtr filehandle);

Of course, you also need some other function the returns an usable
FILE* handle for you (I quickly made this up, dunno if it actually
works):

[ DllImport("msvcrt.dll", CharSet=CharSet.Auto) ]
public static extern IntPtr fopen(string file, string mode);
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Altland | last post by:
Here is my basic problem. I have a form that executes a cpu instensive algorithm occupying the first thread. When the algorithm is executed another form pops up telling the user the progress that...
2
by: Kishore | last post by:
Hi, I have a small console application written in c# that takes two arguments. I am using System.Diagnostics.Process.Start("cmdApp.exe", arguments) to start the application from another .net...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
3
by: Simon King | last post by:
I have created a service which uses the FileSystemWatcher to monitor folders for file changes. I have a app.config file which specifies which folders to watch and what to do when a change occurs. ...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i don´t want to filter the return value of the method, i have to pass a new instance of the...
0
by: RajeevSekar | last post by:
Hi Experts, I am trying to invoke a batch(reside in my local machine) file from asp.net using System.diagnostics.process.start("path\filename") method, it is working fine, but when i try to...
0
by: =?Utf-8?B?RmFsZ3VuaSBWYWhvcmE=?= | last post by:
I have migrated the development environment for MFC single document application from VC 6 to VS 2005. I am not able to invoke the help file from the Menu – help . CMainFrame has the following...
4
balabaster
by: balabaster | last post by:
Does anyone know how to get a file's short pathname in 3.5? (Without p/invoke) I've got a file compare routine that runs a diff on two directories returning the differences of every file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.