110,538
社区成员
发帖
与我相关
我的任务
分享
typedef struct _IP_ADAPTER_ADDRESSES {
....头部结构声明....
struct _IP_ADAPTER_ADDRESSES *Next;//指向下一个IP_ADAPTER_ADDRESSES
PCHAR AdapterName;
PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress;
PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress;
PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress;
PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress;
....密密麻麻密密麻麻....
};
struct IP_ADAPTER_ADDRESSES
{
....头部结构声明....
public IntPtr Next;
[MarshalAs(UnmanagedType.LPStr)]
public string AdapterName;
public Pointer<IP_ADAPTER_UNICAST_ADDRESS> FirstUnicastAddress;
public Pointer<IP_ADAPTER_ADDRESS_LIST_NODE> FirstAnycastAddress;
public Pointer<IP_ADAPTER_ADDRESS_LIST_NODE> FirstMulticastAddress;
public Pointer<IP_ADAPTER_ADDRESS_LIST_NODE> FirstDnsServerAddress;
....密密麻麻密密麻麻....
}
unsafe struct A{
public A* pa;
public int value;
}
A a = new A();
...
..
public struct IP_ADAPTER_ADDRESSES
{
public Pointer<IP_ADAPTER_ADDRESSES> Next;
}
public struct Pointer<T>
where T : struct
{}
public struct IP_ADAPTER_ADDRESSES
{
public IP_ADAPTER_ADDRESSES Next;
}
internal IntPtr next;
public Pointer<IP_ADAPTER_ADDRESSES> Next
{
get { return new Pointer<IP_ADAPTER_ADDRESSES>(next); }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ProjectRabbit.InteropUtils
{
public struct Pointer<T>
where T:struct
{
IntPtr pointer;
//T类型的长度
static readonly int sizeofT;
static Pointer(){
sizeofT = Marshal.SizeOf(typeof(T));
}
public Pointer(IntPtr ptr)
{
pointer = ptr;
}
public T Value
{
get
{
return Get();
}
set
{
Set(value);
}
}
/// <summary>
/// 模拟c指针的数组用法
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
if (index == 0)
return Get();
return (this + index).Get();
}
set
{
if (index == 0)
Set(value);
(this + index).Set(value);
}
}
/// <summary>
/// 指针解引用,c#没法重载*,只能另找一个了
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static T operator ~(Pointer<T> p)
{
return p.Get();
}
public static Pointer<T> operator ++(Pointer<T> ptr)
{
return new Pointer<T>(ptr.pointer + sizeofT);
}
public static Pointer<T> operator --(Pointer<T> ptr)
{
return new Pointer<T>(ptr.pointer - sizeofT);
}
/// <summary>
/// 指针-
/// </summary>
/// <param name="ptr"></param>
/// <param name="val"></param>
/// <returns></returns>
public static Pointer<T> operator +(Pointer<T> ptr,int val)
{
return new Pointer<T>(ptr.pointer + sizeofT);
}
/// <summary>
/// 指针+
/// </summary>
/// <param name="ptr"></param>
/// <returns></returns>
public static Pointer<T> operator -(Pointer<T> ptr)
{
return new Pointer<T>(ptr.pointer - sizeofT);
}
/// <summary>
/// 直接赋值指针
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static implicit operator Pointer<T> (IntPtr p){
return new Pointer<T>(p);
}
/// <summary>
/// 直接获取指针
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static implicit operator IntPtr(Pointer<T> p)
{
return p.pointer;
}
/// <summary>
/// 获取指针
/// </summary>
public IntPtr Ptr{
get { return pointer; }
set { pointer = value; }
}
/// <summary>
/// 获取指针是否为空
/// </summary>
public bool IsNULL
{
get { return pointer == IntPtr.Zero; }
}
/// <summary>
/// 调用Marshal取出结构体
/// </summary>
/// <returns></returns>
public T Get()
{
return (T)Marshal.PtrToStructure(pointer, typeof(T));
}
/// <summary>
/// 调用Marshal更新指针内存
/// </summary>
/// <param name="val">结构体值</param>
public void Set(T val)
{
Marshal.StructureToPtr(val, pointer, true);
}
}
}