CreateFile 未返回正确的句柄 - Delphi 中的设备

发布于 2024-09-27 20:24:29 字数 426 浏览 14 评论 0原文

希望找到有 Windows 服务经验的人。

我正在尝试在 Delphi 中使用以太网适配器的 NdisProt 驱动程序

my_Handle := CreateFile(PChar('\\.\NdisProt'),
    GENERIC_WRITE or GENERIC_READ, 0, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

(也尝试过 \\.\\NdisProt

执行后 my_Handle <强>总是具有十进制值“4008”,并且GetLastError总是返回0

如果我尝试读取或写入文件,我会遇到访问冲突,任何人都知道为什么我会遇到这种不需要的情况行为?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半枫 2024-10-04 20:24:29

如果 CreateFile 未返回 Invalid_Handle_Value,则它为您提供了有效的句柄,否则该设备的驱动程序存在严重错误。假设是前者。

访问冲突与您的句柄值无关。这意味着您正在访问不属于您的进程的内存(例如通过取消引用空指针、未初始化的指针、非指针或已释放的指针)。您的问题出在其他地方,也许是在阅读或编写您忽略在此处显示的代码中。

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.

陌路终见情 2024-10-04 20:24:29

您问题中的代码不是赋值语句。这是一个比较表达式。您应该已经从编译器收到警告,指出变量的值未定义。如果执行该代码后它始终具有值 4008,那么您应该在执行该代码之前检查它是否也具有该值。可能只是 CreateFile 返回有效的句柄值,但您没有使用它返回的值。

如果 4008 不是 CreateFile 返回的值,则 4008 可能不是有效的句柄值。如果操作系统将句柄视为指针(或者如果它对句柄执行某种转换以生成指针),那么与该“句柄”对应的指针可能不是进程中的有效地址;这可以解释访问冲突。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文