Emre Erisgen
Emre Erisgen

Reputation: 157

WinPcap C# Wrapping pcap_findalldevs() throws PInvokeStackImbalance

Im trying 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; 

Upvotes: 1

Views: 1190

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

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.

Upvotes: 3

Related Questions