开发者

WinPcap C# Wrapping pcap_findalldevs() throws PInvokeStackImbalance

开发者 https://www.devze.com 2023-03-20 09:27 出处:网络
Im tr开发者_运维百科ying to write a C# wrapper for winpcap. It gives the warning PInvokeStackImbalance when im trying to debug, but pcap_findalldevs does its job. But I think this will cause a memory

Im tr开发者_运维百科ying to write a C# wrapper for winpcap. It gives the warning PInvokeStackImbalance when im trying to debug, but pcap_findalldevs does its job. But I think this will cause a memory leak in program. Btw this code is from networkminer i didnt write this just trying to understand winpcap and wrapping.

This is the method in WinPcap

int pcap_findalldevs( pcap_if_t **  alldevsp, char * errbuf )

This is what i wrote in my program

[DllImport("wpcap.dll", CharSet = CharSet.Ansi)]
internal static extern int pcap_findalldevs(ref IntPtr alldevsp, StringBuilder errbuf);

i = IntPtr.Zero;
        StringBuilder stringBuilder;
        stringBuilder = new StringBuilder(256);

if (pcap_findalldevs(ref i, stringBuilder) == -1)
            return null; 


You are missing the cdecl calling convention:

[DllImport("wpcap.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]

The default calling convention for DllImport is stdcall but I'd bet that the WinPcap library is exported as cdecl.

0

精彩评论

暂无评论...
验证码 换一张
取 消