在 WinPcap 中工作,pcap_open 并不总是返回指针

发布于 2024-12-02 21:32:45 字数 937 浏览 3 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

悍妇囚夫 2024-12-09 21:32:45

如果pcap_open返回0,则表示有错误,如果不为空,则将其写入errbuf中。

为其分配一些内存(至少PCAP_ERRBUF_SIZE,即256字节),例如,使用那个答案然后显示错误字符串或尝试类似的操作(使用 string/StringBuilder):

[DllImport("wpcap.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr pcap_open(string devicename, int size, int mode, int timeout, IntPtr auth, StringBuilder errbuf);

string devicename = "\\Device\\NPF_{EADB4C21-B0AF-4EF2-86AB-80A37F399D1C}";
try
{
   StringBuilder errbuf=new StringBuilder(256);
   iface = pcap_open(devicename, 65536, 1, 1000, IntPtr.Zero, errbuf);
   Console.WriteLine(errbuf.ToString());
}
catch (Exception er) { return; }

If pcap_open returns 0, it means there was an error, and it would be written in errbuf 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):

[DllImport("wpcap.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr pcap_open(string devicename, int size, int mode, int timeout, IntPtr auth, StringBuilder errbuf);

string devicename = "\\Device\\NPF_{EADB4C21-B0AF-4EF2-86AB-80A37F399D1C}";
try
{
   StringBuilder errbuf=new StringBuilder(256);
   iface = pcap_open(devicename, 65536, 1, 1000, IntPtr.Zero, errbuf);
   Console.WriteLine(errbuf.ToString());
}
catch (Exception er) { return; }
强辩 2024-12-09 21:32:45

可能存在某种合法错误。您是否尝试过将 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.

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