To get this to work in .NET 4, you have to change the
DLLImport
signature.
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
ref SECURITY_ATTRIBUTES lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr template);
Then change the usage to be this:
IntPtr deviceHandle = CreateFile(
fileName,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ref securityAttributes,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);