views:

68

answers:

2

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?

+8  A: 

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.

Rob Kennedy
+1  A: 

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.

Rob Kennedy
Thank you for the new answer Rob, I forgot to put the semicolon in the question above, so yes It was an assignment.(updated now) I debugged the value of my_Handle before Createfile, the result was 0. Also, I noticed that running the compiler without debugger support from the IDE the value of the handle is always 4000. I have the original source in C# and I'm trying to make it native. In the original code the handle always gets a value between 250 and 300 (at least on my pc) Original code: http://www.codeproject.com/KB/IP/sendrawpacket.aspx
Ed.C