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

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
 
H

Herfried K. Wagner [MVP]

* "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:

<
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top