473,811 Members | 3,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

P/Invoke, CreateFile and SafeFileHandle

Hi,

Can anyone explain how P/Invoke is able to cast the 32-bit HANDLE value
returned from CreateFile (for example) to a SafeFileHandle object?

Have a look at this code snippet from the .NET Framework Reference:

-----------------------------------------------------------
[DllImport("kern el32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
string lpFileName,
uint dwDesiredAccess ,
uint dwShareMode,
IntPtr lpSecurityAttri butes,
uint dwCreationDispo sition,
uint dwFlagsAndAttri butes,
IntPtr hTemplateFile);
private SafeFileHandle handleValue = null;
handleValue = CreateFile(
Path,
GENERIC_WRITE,
0,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
-----------------------------------------------------------

Is there some kind of behind-the-scenes stuff going on with P/Invoke to
generate the extra code that puts the returned HANDLE into the handle
member of the SafeFileHandle. .?
-Bob
Dec 1 '06 #1
2 11750
Bob Gray wrote:
Hi,

Can anyone explain how P/Invoke is able to cast the 32-bit HANDLE value
returned from CreateFile (for example) to a SafeFileHandle object?

Have a look at this code snippet from the .NET Framework Reference:

<snipped for brevity>

Is there some kind of behind-the-scenes stuff going on with P/Invoke to
generate the extra code that puts the returned HANDLE into the handle
member of the SafeFileHandle. .?
Yes, the CLR's platform invoke marshaling layer stores the HANDLE returned
by CreateFile into the SafeFileHandle object.
--
Tom Porterfield

Dec 1 '06 #2
Hi Bob,

That's not the only conversion done by the marshaling service:

"Default Marshaling Behavior"
http://msdn2.microsoft.com/en-us/lib...75(VS.80).aspx

--
Dave Sexton

"Bob Gray" <bo******@e03.d kwrote in message
news:Xn******** *************** ***********@216 .196.109.144...
Hi,

Can anyone explain how P/Invoke is able to cast the 32-bit HANDLE value
returned from CreateFile (for example) to a SafeFileHandle object?

Have a look at this code snippet from the .NET Framework Reference:

-----------------------------------------------------------
[DllImport("kern el32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
string lpFileName,
uint dwDesiredAccess ,
uint dwShareMode,
IntPtr lpSecurityAttri butes,
uint dwCreationDispo sition,
uint dwFlagsAndAttri butes,
IntPtr hTemplateFile);
private SafeFileHandle handleValue = null;
handleValue = CreateFile(
Path,
GENERIC_WRITE,
0,
IntPtr.Zero,
OPEN_EXISTING,
0,
IntPtr.Zero);
-----------------------------------------------------------

Is there some kind of behind-the-scenes stuff going on with P/Invoke to
generate the extra code that puts the returned HANDLE into the handle
member of the SafeFileHandle. .?
-Bob

Dec 2 '06 #3

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

Similar topics

1
16717
by: Chuck Rittersdorf | last post by:
Hi There I am having a problem using the win32 API from VB6. I am trying to send a command string to a printer(zebra TLP 2742) on LPT1 using the folowing API functions CreateFile and WriteFile I have written C code which works fine, just the VB translation/port
0
1697
by: Nitin Narang | last post by:
I have a piece of code which is moved to .NET VC7 recently from VC6 and there has been an apparent change in behavior using CreateFile/Writefile functionality. I am creating a file on a shared drive using the win32 Api and using the handle returned, i call writefile giving number of bytes to write as 1MB. Shared directory on the machine maps to F: drive ( "shared" is the directory name maps to " F:") In VC6 on executing...
3
7477
by: Antoine | last post by:
Hello, I'm writing a program to send requests to my wlan pocket pc device (UIO1: driver) in C#. Here how I import CreateFile functions from coredll.dll with DllImport: public static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess,
4
25860
by: JLW | last post by:
I'm having alot of difficulty with this one. I have seen no less than 20 different ways to use this function with a named pipe. I'm trying to use \\.\TAPE0 (TapeDrive). Here's my decleration: internal static extern IntPtr CreateFile( String filename, UInt32 desiredAccess, UInt32 shareMode,
5
6330
by: Andrew Clark | last post by:
Hello, Thanks for all replies on this subject. I still cannot get CreateFile to retun a good value though. I went to PInvoke.net and saw the VB.NET declaration of this function: <DllImport("kernel32.dll", SetLastError:=True)> Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As EFileAccess, ByVal dwShareMode As EFileShare, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As...
5
3083
by: Fla | last post by:
Hy! I'm a newbie to VB 2005 and I have to connect my program to a driver previously developed for a custom ISA card. With my old VB 6 code I used the routine CreateFileA exported from kernel32 with this defination: Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal NULLSecurityAttributes As Long, ByVal dwCreationDisposition As Long,...
8
2116
by: news.microsoft.com | last post by:
Hello, i'm having problems to call a api dll in vb dot net. I absolutely want to use this api call and none of the frame calls. This is my declaration: ***************** Public Structure SECURITY_ATTRIBUTES Public nLength As UInt32 Public lpSecurityDescriptor As IntPtr
3
2874
by: Scott Bell | last post by:
Hello all, What is the equivalent in the .NET Framework for accessing a device at the block/sector level such as one would when using CreateFile on a device like "\\.\D:"? Attempting to create a BinaryReader on a FileStream opened (read-only) on "D:" yields an Access Denied exception. Thanks,
0
1408
by: NvrBst | last post by:
Just for clarification, the Microsoft.Win32.SafeHandles.SafeFileHandle can be used with anything that uses the "kernel32's CloseHandle" method for clean up? For Example, "kernal32's OpenProcess"? It seems to work, but I just wanted to be 100% sure. NB
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10413
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9214
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5700
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.