By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,635 Members | 1,707 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,635 IT Pros & Developers. It's quick & easy.

P/Invoke, CreateFile and SafeFileHandle

P: n/a
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("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.dkwrote 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("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
uint dwCreationDisposition,
uint dwFlagsAndAttributes,
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 discussion thread is closed

Replies have been disabled for this discussion.