这个intptr看的我很纠结啊,delphi和c里都可以直接传入指针,唯独在C#里先要申请分托管内存才可以
今天查到了byte数组转intptr的方法
但是如果是 thread[]或者是list<thread>这样的东东怎么转非托管的intptr呢?
今天查到了byte数组转intptr的方法
但是如果是 thread[]或者是list<thread>这样的东东怎么转非托管的intptr呢?
解决方案 »
- 执行Server.TransferRequest("XX.aspx");时,出错:为 XX.aspx 执行子请求时出错。没找到解决方案,怎么办,求解
- 急急急!在启动debug的时候,提示查找更新失败远程服务器返回错误:404 未找到
- TextBox或者MultiLine怎么不限制它的长度
- 自定义控件的KeyDown事件
- P2P资源共享问题
- 有什么办法来修改已编译的dll文件中的一个string值???急啊!!
- 怎样重启线程??
- 当我按ALT+S时触发我的一个按纽事件,怎么实现.
- 我装VS.NET正式版之前忘记装IIS,出现问题了,如何解决?
- 求解winform界面诡异卡死的原因
- datagridview的常见属性,散分了
- 求一個函數表達式
Marshal.StructureToPtr();
这个是把结构体转intptr的方法么?
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);
例如 thread 或者2L所说的结构体要怎么转呢?
楼主,你这样做当然不对,改成如下:
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);
如:
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!
这样,你提出接个有代表性的C++函数,我看看
例如public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, bool bWaitAll, uint dwMilliseconds);
实际上我不知道WaitForMultipleObjects里的IntPtr[] handles具体接收类型啊
查资料好像可以是进程或者线程句柄,那我直接传入
[In,Out]thread[] th 或者[In,Out]process[] ps可以么?
例如
public static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] handles, bool bWaitAll, uint dwMilliseconds);
他可以接受进程或线程句柄的数组,但是我不知道他内部的对应类型是什么那么我传入
In,Out]thread[] th 或者In,Out]process[] ps对么?
他可以接受进程或者线程句柄的数组,该如何按照相同类型传值呢?([In,Out]thread[] th可以么?
直接传托管类是不行的.
你需要搞到内部封装掉的句柄.也就是那个IntPtr.
建议lz先去恶补一下Win32编程
原文
“唯独在C#里先要申请非托管内存才可以
今天查到了byte数组转intptr的方法
但是如果是 thread[]或者是list<thread>这样的东东怎么转非托管的intptr呢?”
定义如下
DWORD WINAPI WaitForMultipleObjects(
__in DWORD nCount,
__in const HANDLE *lpHandles,
__in BOOL bWaitAll,
__in DWORD dwMilliseconds
);
目前在尝试取得线程id,再用id取线程句柄,然后把句柄转intptr...........累...........