在 WinPcap 中工作,pcap_open 并不总是返回指针
我正在使用 Winpcap 在 C Sharp
中开发数据包嗅探器,以下是确切的代码:
[DllImport("wpcap.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr pcap_open(char[] devicename, int size, int mode, int timeout, ref IntPtr auth, ref IntPtr errbuf);
string devicename = "\\Device\\NPF_{EADB4C21-B0AF-4EF2-86AB-80A37F399D1C}";
IntPtr errbuf = IntPtr.Zero, auth = IntPtr.Zero, iface;
try
{
iface = pcap_open(devicename.ToCharArray(), 65536, 1, 1000, ref auth, ref errbuf);
}
catch (Exception er) { return; }
pcap_open 并不总是返回指向我的网络接口的有效指针。有时它会返回NULL
(0)。它曾经显示“PInvoke 检测到堆栈不平衡...”,我通过更改调用约定纠正了该问题。我什至确保 devicename
中使用的 char
是 1 字节 (charset ansi)
。还是有些问题。
只是一个观察:每当我调试它时,它总是返回一个有效的指针,但是当我不这样做时,它会在 40% 的情况下返回 NULL。
我已经检查了 & 中的所有内容出来,谷歌搜索了很多,但无法弄清楚任何东西。可能缺少什么? 最糟糕的是我什至无法捕获异常来正确处理它。有人有答案吗?
I'm working on a packet sniffer in C Sharp
using Winpcap, Here is the exact code:
[DllImport("wpcap.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr pcap_open(char[] devicename, int size, int mode, int timeout, ref IntPtr auth, ref IntPtr errbuf);
string devicename = "\\Device\\NPF_{EADB4C21-B0AF-4EF2-86AB-80A37F399D1C}";
IntPtr errbuf = IntPtr.Zero, auth = IntPtr.Zero, iface;
try
{
iface = pcap_open(devicename.ToCharArray(), 65536, 1, 1000, ref auth, ref errbuf);
}
catch (Exception er) { return; }
pcap_open does not always return a valid pointer to my network interface. Sometimes it returns NULL
(0). It used to show "PInvoke detected a Stack unbalance...", I corrected that by changing the calling convention. I even made sure that the char
used in devicename
is of 1 byte (charset ansi)
. Still something is going wrong.
Just an observation: Whenever I debug it, it always returns a valid pointer, but when I don't, it will return NULL
40% of the times.
I've checked everything in & out, googled a lot but could not figure out anything. What could be missing?
The worst part is that I can't even catch the exception to handle it properly. Does anybody have an answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
pcap_open
返回0,则表示有错误,如果不为空,则将其写入errbuf
中。为其分配一些内存(至少PCAP_ERRBUF_SIZE,即256字节),例如,使用那个答案然后显示错误字符串或尝试类似的操作(使用 string/StringBuilder):
If
pcap_open
returns 0, it means there was an error, and it would be written inerrbuf
if it wasn't null.Allocate some memory for it (at least PCAP_ERRBUF_SIZE which is 256 bytes), for example, with the method given in that answer then display the error string or try something like that (with string/StringBuilder):
可能存在某种合法错误。您是否尝试过将 iface 设置为 IntPtr.Zero,然后在针对 IntPtr.Zero 调用 pcap_open 后进行检查?如果将其设置为 null,您也应该能够检查 null 。
当它为空时,您似乎需要将 errorBuf 转换为字符串以查看错误消息是什么。
It may be that there's some sort of legit error. Have you tried setting iface to IntPtr.Zero and then checking ater you call pcap_open against IntPtr.Zero? If it's setting it to null, you should also be able to check for null as well.
WHen it's null, it looks like you will want to convert errorBuf to a string to see what the error message is.