问题
Hoping to find somebody that has experience with services in windows.
I am trying to use the NdisProt
driver for ethernet adapters in Delphi
my_Handle := CreateFile(PChar('\\.\NdisProt'), GENERIC_WRITE or GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
(have tried with \\.\\NdisProt
too)
After execution my_Handle
always has the value '4008' decimal and GetLastError always returns 0
If I try to read or write to the file I get acces violation, anybody knows why I'm getting this unwanted behavior?
回答1:
If CreateFile
doesn't return Invalid_Handle_Value
, then it has given you a valid handle, or else the driver for that device is severely buggy. Assume the former.
An access violation has nothing to do with your handle value. It means you're accessing memory that doesn't belong to your process (such as by dereferencing a null pointer, an uninitialized pointer, a non-pointer, or an already freed pointer). Your problem lies elsewhere, perhaps in the reading or writing code that you neglected to show here.
回答2:
The code in your question is not an assignment statement. It's a comparison expression. You should have gotten a warning from the compiler that the variable's value is undefined. If it always has the value 4008 after executing that code, then you should check whether it also had that value before executing that code. It could simply be that CreateFile
is returning a valid handle value, but you're not using the value it returns.
If 4008 isn't the value CreateFile
returns, then it's probable that 4008 isn't a valid handle value. If the OS treats handles as pointers (or if it performs some kind of transform on handles to generate pointers), then it could be that the pointer corresponding to that "handle" is not a valid address in your process; that would explain the access violation.
来源:https://stackoverflow.com/questions/3972775/createfile-not-returning-a-proper-handle-devices-in-delphi