How to call a C function with pointer in C# ?

  • Thread starter Thread starter james ou
  • Start date Start date
J

james ou

hi, everybody,

I want to call a C function in the C#.

I add the following code in the C# application.

[ StructLayout(LayoutKind.Sequential) ]
public struct sockaddr
{
public System.Int16 sa_family;
public char[] sa_data;
};

[ StructLayout(LayoutKind.Sequential) ]
public unsafe struct pcap_addr
{
public pcap_addr *next;
public sockaddr *addr;
public sockaddr *netmask;
public sockaddr *broadaddr;
public sockaddr *dstaddr;
};

[ StructLayout(LayoutKind.Sequential) ]
public unsafe struct pcap_if
{
public pcap_if * next;
public char *name;
public char *description;
public pcap_addr *addresses;
public System.Int32 flags;
};

[ DllImport("wpcap.dll") ]
public extern static int pcap_findalldevs(pcap_if ** dev,
[MarshalAs(UnmanagedType.LPTStr)] String err);

private void button1_Click(object sender, System.EventArgs
e)
{
pcap_if **alldevs = new pcap_if() ;
String errbuf = "vv";

/* Retrieve the device list */
if (pcap_findalldevs(alldevs, errbuf) == -1)
{
label1.Text = errbuf;
}
}

When I build the application. The complier say :

D:\My Documents\Visual Studio Projects\WindowsApplication2
\Form1.cs(189): Cannot implicitly convert
type 'WindowsApplication2.pcap_if'
to 'WindowsApplication2.pcap_if**'

How can I call it?

Another question is how can I declare "char sa_data[14]"
in the C# ?

Thanks
 
* "james ou said:
I want to call a C function in the C#.

I add the following code in the C# application.

No relation to .NET Windows Forms programming. You will more likely get
an answer if you post to the C# group:

<
 
Back
Top