所以我將這些東西從C++移植到c#中。而它的一部分看起來像這樣:CreateFile的端口INVALID_HANDLE_VALUE
m_hParstat = CreateFile(_T("\\\\.\\LPTSTAT1"), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(m_hParstat == INVALID_HANDLE_VALUE)
{
// do some stuff
}
所以在我的C#代碼我有
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
public const int FILE_ATTRIBUTE_NORMAL = 0x00000080;
public const uint GENERIC_READ = 0x80000000;
public const uint OPEN_EXISTING = 3;
public const UInt32 INVALID_HANDLE_VALUE = 0xffffffff;
然後
m_hParstat = CreateFile("\\\\.\\LPTSTAT1", GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
if (m_hParstat.ToInt32() == INVALID_HANDLE_VALUE)
{
}
但是VS是說,比較是無用的,因爲該常數在int範圍之外。如何檢查我的句柄以獲取有效值?
我認爲這可行,但SwDevMan81有一個更簡單的解決方案。 – 2011-04-28 15:29:15