Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does CLR match C++ file access constants with C# enums?

Tags:

c#

marshalling

I would like to know how does CLR match GENERIC_READ with FileAccess.Read, for instance. I had marshalled this parameter as UnmanagedType.U4

#define GENERIC_READ (0x80000000L)

but

FileAccess.Read = 1

How does marshaler know what is required?

like image 227
Oksana Avatar asked Oct 25 '25 08:10

Oksana


1 Answers

CreateFile's dwDesiredAccessMode argument is too out of whack to cleanly map to an enum. So FileAccess is mapped in code to an int. From the Reference Source's FileStream.cs source code file, FileStream.Init() method:

    int fAccess;
    ...
    fAccess = access == FileAccess.Read? GENERIC_READ:
    access == FileAccess.Write? GENERIC_WRITE:
    GENERIC_READ | GENERIC_WRITE;
    ...
    _handle = Win32Native.SafeCreateFile(tempPath, fAccess, ...etc)

Original indenting reproduced, odd as it looks.

like image 187
Hans Passant Avatar answered Oct 27 '25 23:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!