调用C++写的dll,其中有一个参数SOCKADDR_IN,请问我该如何转换成C#的方式?如果定义一个一模一样的结构体的话,我该如何转换相应的数据类型,请高手帮忙!
—- 10分
这是WinSock编程里的一个结构体。参见下文:
http://blog.csdn.net/lihengzk/article/details/1415312
http://blog.csdn.net/lihengzk/article/details/1415312
这个结构体只能是你自己参照文章的结构体定义了。
或者直接使用指针定义也是可以的,不过就是分析结构时,赋值或者读取返回值麻烦一些。自己定义结构体则正好相反,定义麻烦,赋值或则读取返回值容易。
—- 10分
http://bbs.csdn.net/topics/390018402
可以参考这个帖子
可以参考这个帖子
—- 20分
http://pinvoke.net/default.aspx/Structures/sockaddr_in.html
—-
楼上各位大侠,我现在要调用下面一个C++的函数,不知道该如何传值:
int _stdcall ConnectNetPort(SOCKET *lpSocket,//传入已分配套接字的指针
SOCKADDR_IN * pPrinterAddr, //打印机的地址
timeval *lpTimeout); //超时
功能说明:
调用该函数将会连接打印机。成功返回1,失败返回值见下:
-1:创建套接字失败。
-2:设|为非阻塞模式失败。
-3:连接超时。
-4:Select错误
int _stdcall ConnectNetPort(SOCKET *lpSocket,//传入已分配套接字的指针
SOCKADDR_IN * pPrinterAddr, //打印机的地址
timeval *lpTimeout); //超时
功能说明:
调用该函数将会连接打印机。成功返回1,失败返回值见下:
-1:创建套接字失败。
-2:设|为非阻塞模式失败。
-3:连接超时。
-4:Select错误
现在主要我不知道如何去传SOCKADDR_IN这个参数,我定义了下面一个类,不知道对不对的:
public class SOCKADDR_IN { private short _sin_family; //指代协议族,在socket编程中只能是AF_INET public short sin_family { get { return _sin_family; } set { _sin_family = value; } } private ushort _sin_port; //存储端口号(使用网络字节顺序) public ushort sin_port { get { return _sin_port; } set { _sin_port = value; } } private IN_ADDR _sin_addr; //存储IP地址,使用in_addr这个数据结构 public IN_ADDR sin_addr { get { return _sin_addr; } set { _sin_addr = value; } } [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] private byte[] _sin_zero; //是为了让sockaddr与sockaddr_in两个数据结构保持大小相同而保留的空字节 public byte[] sin_zero { get { return _sin_zero; } set { _sin_zero = value; } } public SOCKADDR_IN() { _sin_addr = new IN_ADDR(); _sin_zero = new byte[8]; } public SOCKADDR_IN(short sinfamily, ushort sinport, IN_ADDR sinaddr, byte[] sinzero) { _sin_family = sinfamily; _sin_port = sinport; _sin_addr = sinaddr; _sin_zero = sinzero; } }
public class Sunb { private int _s_b1; public int s_b1 { get { return _s_b1; } set { _s_b1 = value; } } private int _s_b2; public int s_b2 { get { return _s_b2; } set { _s_b2 = value; } } private int _s_b3; public int s_b3 { get { return _s_b3; } set { _s_b3 = value; } } private int _s_b4; public int s_b4 { get { return _s_b4; } set { _s_b4 = value; } } public Sunb() { } public Sunb(int sb1, int sb2, int sb3, int sb4) { _s_b1 = sb1; _s_b2 = sb2; _s_b3 = sb3; _s_b4 = sb4; } } public class Sunw { private uint _s_w1; public uint s_w1 { get { return _s_w1; } set { _s_w1 = value; } } private uint _s_w2; public uint s_w2 { get { return _s_w2; } set { _s_w2 = value; } } public Sunw() { } public Sunw(uint sw1, uint sw2) { _s_w1 = sw1; _s_w2 = sw2; } } public class Sun { private Sunb _s_un_b; public Sunb s_un_b { get { return _s_un_b; } set { _s_un_b = value; } } private Sunw _s_un_w; public Sunw s_un_w { get { return _s_un_w; } set { _s_un_w = value; } } private uint _s_addr; public uint s_addr { get { return _s_addr; } set { _s_addr = value; } } public Sun() { _s_un_b = new Sunb(); _s_un_w = new Sunw(); } public Sun(Sunb sunb, Sunw sunw, uint saddr) { _s_un_b = sunb; _s_un_w = sunw; _s_addr = saddr; } } public class IN_ADDR { private Sun s_un; /// <summary> /// can be used for most tcp & ip code /// </summary> public uint s_addr { get { return s_un.s_addr; } set { s_un.s_addr = value; } } /// <summary> /// host on imp /// </summary> public int s_host { get { return s_un.s_un_b.s_b2; } set { s_un.s_un_b.s_b2 = value; } } /// <summary> /// network /// </summary> public int s_net { get { return s_un.s_un_b.s_b1; } set { s_un.s_un_b.s_b1 = value; } } /// <summary> /// imp /// </summary> public uint s_imp { get { return s_un.s_un_w.s_w2; } set { s_un.s_un_w.s_w2 = value; } } /// <summary> /// imp # /// </summary> public int s_impno { get { return s_un.s_un_b.s_b4; } set { s_un.s_un_b.s_b4 = value; } } /// <summary> /// logical host /// </summary> public int s_lh { get { return s_un.s_un_b.s_b3; } set { s_un.s_un_b.s_b3 = value; } } public IN_ADDR() { s_un = new Sun(); } public IN_ADDR(uint saddr, int shost, int snet, uint simp, int simpno, int slh) { s_un = new Sun(); s_addr = saddr; s_host = shost; s_net = snet; s_imp = simp; s_impno = simpno; s_lh = slh; } }
—-
谢谢楼上几位,已解决!
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明调用C++写的dll,其中有一个参数SOCKADDR_IN!