这个intptr看的我很纠结啊,delphi和c里都可以直接传入指针,唯独在C#里先要申请分托管内存才可以
今天查到了byte数组转intptr的方法
但是如果是 thread[]或者是list<thread>这样的东东怎么转非托管的intptr呢?

解决方案 »

  1.   

    另外补充一句
    Marshal.StructureToPtr();
    这个是把结构体转intptr的方法么?
      

  2.   

    我这样写完全不对啊~
            struct teststruct
            {
              public  int i;
              public bool b;
            }            teststruct temp = new teststruct();
                temp.i = 0;
                temp.b = false;
                IntPtr ps = new IntPtr();
                Marshal.StructureToPtr(temp, ps, false);    
      

  3.   

     List<IntPtr> result = new List<IntPtr>();
      

  4.   

    不是这么搞的。手上没编译器。大概思路是先ToArray,然后获取首个元素地址。
      

  5.   

    先谢谢LS,不过我的本意是想详细了解下intptr的使用方法,不是简单的找一个替代方案
    例如 thread 或者2L所说的结构体要怎么转呢?
      

  6.   


    楼主,你这样做当然不对,改成如下:
      teststruct temp = new teststruct();
      temp.i = 0;
      temp.b = false;int size = Marshal.SizeOf(temp );
    IntPtr ps = Marshal.AllocHGlobal(size);
    Marshal.StructureToPtr(structure, ps , false);
      

  7.   

    另外,对应具体的结构体类型,如果是在进行参数封送,建议首先不要考虑IntPtr,而是直接用起对应的类型:
    如:
    C++:
    void Test(thread* th);
    void Test(thread[] th,int size);C#:
    void Test(ref thread th);
    void Test([In,Out]thread[] th,int size);这样是不是很简单、直接,一般只有对于void*,才有首先考虑使用IntPtr!
      

  8.   

    非常感谢Lz,因为我是在调用api,所以没办法做函数封送啊,它里面的类型大部分都是intptr的
      

  9.   

    敲错了,是LS,所以我只能想把发把object转换成intptr然后传入api函数,郁闷........
      

  10.   

    不就是参数吗?难道你大部分都是void*指针吗?看来你还没理解我上面说的。
    这样,你提出接个有代表性的C++函数,我看看
      

  11.   

    上面的我理解了
    例如public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, bool bWaitAll, uint dwMilliseconds);
    实际上我不知道WaitForMultipleObjects里的IntPtr[] handles具体接收类型啊
    查资料好像可以是进程或者线程句柄,那我直接传入
    [In,Out]thread[] th 或者[In,Out]process[] ps可以么?
      

  12.   

    奇怪了,发言会被屯啊,唉
    例如
    public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, bool bWaitAll, uint dwMilliseconds);
    他可以接受进程或线程句柄的数组,但是我不知道他内部的对应类型是什么那么我传入
    In,Out]thread[] th 或者In,Out]process[] ps对么?
      

  13.   

    例如public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, bool bWaitAll, uint dwMilliseconds);里的IntPtr[] handles
    他可以接受进程或者线程句柄的数组,该如何按照相同类型传值呢?([In,Out]thread[] th可以么?
      

  14.   

    你这明明是C#的啊:public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, bool bWaitAll, uint dwMilliseconds);我问的是C++的函数定义!
      

  15.   

    Thread和Process是.net库为你封装的托管类.
    直接传托管类是不行的.
    你需要搞到内部封装掉的句柄.也就是那个IntPtr.
    建议lz先去恶补一下Win32编程
      

  16.   

    WaitForMultipleObjects是windows的api啊,我哪里有头文件啊,囧...........
      

  17.   

    WaitForMultipleObjects,MSDN里有 
      

  18.   

    我知道这两个封装的托管类是不行的,所以我一开始才问申请非托管内存传递intptr的方法,不过C#只提供byte数组结构体以及一些变量的intptr转换,不要看帖只看一半啊
    原文
    “唯独在C#里先要申请非托管内存才可以
    今天查到了byte数组转intptr的方法
    但是如果是 thread[]或者是list<thread>这样的东东怎么转非托管的intptr呢?”
      

  19.   

    忘了msdn这个活宝了
    定义如下
    DWORD WINAPI WaitForMultipleObjects(
      __in  DWORD nCount,
      __in  const HANDLE *lpHandles,
      __in  BOOL bWaitAll,
      __in  DWORD dwMilliseconds
    );
      

  20.   

    public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, int bWaitAll, uint dwMilliseconds);
      

  21.   

    ??我不是不知道函数的生命,主要是不知道如何取thread的句柄,唉,C#把类都封死了
    目前在尝试取得线程id,再用id取线程句柄,然后把句柄转intptr...........累...........