2 u011514551 u011514551 于 2013.11.15 16:50 提问

C# 引用C++的dll,在C# 如何获取结构体的句柄 作为传入参数,求高手帮忙。
c#

C++ 结构体如下
typedef struct tagHIP
{
USHORT dhcp_enable; //DHCP使能
USHORT reserve; //保留
CHAR ip[HIP_BUF_LEN_32];// 设备IP
CHAR netmask[HIP_BUF_LEN_32];
CHAR gateway[HIP_BUF_LEN_32];
CHAR mac[HIP_MAC_LEN_20];
CHAR multi_ip[HIP_BUF_LEN_32];//多播地址
CHAR dns_addr[HIP_BUF_LEN_32];//DNS服务器
CHAR dns_addr_ext[HIP_BUF_LEN_32];//备用DNS服务器
LONG lReserve; //保留
}HIP_NETWORKE, *LPHIP_NETWORK_t;
函数接口如下
int HIP_I_GetDeviceConfig(LONG lLoginID, DWORD dwCommand,LONG lChannel, LPVOID lpOutBuffer, DWORD dwOutBufferSize, LPDWORD lpBytesReturned, int waittime);

使用如下:
DWORD dwReturn=0;
tagHIP_NETWORKETHPARAM para_network;

int i1=HIP_I_GetDeviceConfig(theApp.loginId, 3 ,0,&para_network,sizeof(tagHIP_NETWORKETHPARAM),&dwReturn,0);//获取参数

结构体及接口转成C#代码如下
public struct tagHIP
{
public ushort dhcp_enable; //DHCP使能
public ushort cmd_port;
public ushort data_port; // 数据端口
public ushort web_port; //web 端口
public ushort broad_port;
public ushort multi_port; //多播端口
public ushort rtsp_port; //RTSP端口
public ushort reserve; //保留

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] ip;// 设备IP

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] netmask;

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] gateway;

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] mac;

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] multi_ip;//多播地址

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] dns_addr;//DNS服务器

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
    public byte[] dns_addr_ext;//备用DNS服务器

    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I1)]
    public byte[] bReserve;                    //保留

    public void Init()
    {
        ip = new byte[32];
        netmask = new byte[32];
        gateway = new byte[32];
        mac = new byte[32];
        multi_ip = new byte[32];
        dns_addr = new byte[32];
        dns_addr_ext = new byte[32];
        bReserve = new byte[4];
    }
}

接口
[DllImport("HIPI_Sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int HIP_I_GetDeviceConfig(int lLoginID, uint dwCommand, int lChannel, IntPtr lpOutBuffer,
int dwOutBufferSize, IntPtr lpBytesReturned, int waittime);

C# 代码中如何使用HIP_I_GetDeviceConfig();这个函数,第四个参数如何才能把结构体转成句柄???
求高手解决,在下感激不尽。

1个回答

gamefinity
gamefinity   Rxr 2015.06.17 07:41

可以直接使用结构,例如

[DllImport("HIPI_Sdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int HIP_I_GetDeviceConfig(int lLoginID, uint dwCommand, int lChannel, ref HIP hip,
int dwOutBufferSize, IntPtr lpBytesReturned, int waittime);
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!