473,395 Members | 1,516 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,395 software developers and data experts.

.NET (C#) and file handles

I have an API DLL with a handful of methods, which I wrapped in
DllImport-attributed methods in C#.

Everything works fine, however, one of the methods takes an open file
handle (as an int) to write its execution log into.

Is there a way in .NET to create such a handle to pass to the method?

TIA!

Nov 17 '05 #1
5 17302
You can create an instance of the FileStream class and use the Handle
property to get the file handle. If you are using .NET 2.0, you should use
the SafeFileHandle property (and then call DangerousGethandle on the
SafeFileHandle returned).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Usenet User" < @ . > wrote in message
news:6g********************************@4ax.com...
I have an API DLL with a handful of methods, which I wrapped in
DllImport-attributed methods in C#.

Everything works fine, however, one of the methods takes an open file
handle (as an int) to write its execution log into.

Is there a way in .NET to create such a handle to pass to the method?

TIA!

Nov 17 '05 #2

"Usenet User" < @ . > wrote in message
news:6g********************************@4ax.com...
I have an API DLL with a handful of methods, which I wrapped in
DllImport-attributed methods in C#.

Everything works fine, however, one of the methods takes an open file
handle (as an int) to write its execution log into.

Is there a way in .NET to create such a handle to pass to the method?


Check out FileStream.Handle.

You would do something like

using (FileStream fs = new FileStream(...))
{
ExternalMethod( . . ., fs.Handle.ToInt32());

}

David
Nov 17 '05 #3

Opague structures (such as file/window handles) can be passed to/from
PInvoke via the System.IntPtr type. You should be able to do this without
the need for unsafe code blocks...

I assume, but dunno off the top of my head, that there is some way to get a
file handle from a .NET stream - if not then you can PInvoke the CreateFile()
& CloseHandle() APIs... In any event you'll probably want to remember to
implement the IDispose pattern to clean up your file handle...

"Usenet User" wrote:
I have an API DLL with a handful of methods, which I wrapped in
DllImport-attributed methods in C#.

Everything works fine, however, one of the methods takes an open file
handle (as an int) to write its execution log into.

Is there a way in .NET to create such a handle to pass to the method?

TIA!

Nov 17 '05 #4
On Tue, 30 Aug 2005 13:17:38 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
You can create an instance of the FileStream class and use the Handle
property to get the file handle. If you are using .NET 2.0, you should use
the SafeFileHandle property (and then call DangerousGethandle on the
SafeFileHandle returned).

Hope this helps.


Thanks! I was looking exactly for that. Other option I was considering
was to use Win32 API to get such a handle.
Nov 17 '05 #5
On Tue, 30 Aug 2005 13:17:38 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
You can create an instance of the FileStream class and use the Handle
property to get the file handle. If you are using .NET 2.0, you should use
the SafeFileHandle property (and then call DangerousGethandle on the
SafeFileHandle returned).

Hope this helps.


Thanks! I was looking exactly for that. Other option I was considering
was to use Win32 API to get such a handle.
Nov 17 '05 #6

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

Similar topics

12
by: Woodster | last post by:
I currently have some code for an application that is running on Win32. I have tried to keep anything not directly gui related as separate as possible for portability reasons, including file...
18
by: Conrad F | last post by:
Hello all, I am waiting for receipt of files in a directory. I use the FileSystemWatcher to detect when files arrive in said folder. I need to read the data from these files ASAP but the files...
10
by: Brian Henry | last post by:
Hi, I am having a problem with an attachment system I made... it works with files up to ~3MB in size then after that if you try to upload a file it just goes to a "Page can not be displayed" page...
10
by: GJP | last post by:
Hello. Ive been asked to make my own notepade for college assignment. All ig going well, but i cant get the save to work. I can get Save a (shows dialog box), i can get it to just save too,...
3
by: Debbie Carter | last post by:
Can XML files be easily encrypted?
1
by: apple | last post by:
i try to print image file in a directory using PrintDocument. It will raise printPage event to draw image to the printer. The file will be deleted after print and the directory will be checked...
2
by: eBob.com | last post by:
I've got the basics of an XML ini file working. I.E. I can stash away and retrieve user preferences. (Code below.) But how do I handle a new preference? Say I have A and B. And then I invent...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
2
by: SharpCoderMP | last post by:
hi, in my app i monitor the filesystem for changes with FileSystemWatchers. When the change is detected the app performs some actions using Shell32 to obtain information from the filesystem. now...
1
by: dwainew | last post by:
Problem: I'd like to archive off the contents of a text file. However, other processes may have the file open, so MoveTo(newfilepath) can't be done, as far as I've found. Start with a 5 MB...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.