编译程序将BOOL CloseHandle(HANDLE hObject)理解为int __cdecl CloseHandle(void *),怎么回事?

lwjwj1314 2005-10-31 02:02:22
我的程序如下:
#include <windows.h>
#include <process.h>
#include <stdio.h>

unsigned __stdcall ThreadProc(void *p)
{
printf("Thread execute!");
return 0;
}

int main()
{
unsigned tid;
unsigned long thd;
thd=_beginthreadex(NULL,0,ThreadProc,NULL,0,&tid);
if(NULL!=thd)
{
CloseHandle(thd);
}
WaitForSingleObject(thd,0);
return 0;
}

编译出错:

\multi_thread\main.cpp(18) : error C2664: 'CloseHandle' : cannot convert parameter 1 from 'unsigned long' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
\multi_thread\main.cpp(20) : error C2664: 'WaitForSingleObject' : cannot convert parameter 1 from 'unsigned long' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

CloseHandle的原型本来就是BOOL CloseHandle(HANDLE hObject);
编译程序却将它理解成:int __cdecl CloseHandle(void *)
这是怎么回事啊?
...全文
188 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
loverx 2005-11-03
  • 打赏
  • 举报
回复
1。说明:
CloseHandle(thd);
WaitForSingleObject(thd,0);为WIN32 API

而beginthreadex为c runtime,数据类型需要转换

2。解决方法:
正如楼上所说
HANDLE thd=(HANDLE)_beginthreadex(NULL,0,ThreadProc,NULL,0,&tid);

如果假设你使用CreateThread也不会出现上面的问题,因为其是WIN32标准的API
但不建议使用,推荐老侯的《WIN32多线程》
everandforever 2005-10-31
  • 打赏
  • 举报
回复
HANDLE thd=(HANDLE)_beginthreadex(NULL,0,ThreadProc,NULL,0,&tid);

__cdecl 是默认的调用方式。
lwjwj1314 2005-10-31
  • 打赏
  • 举报
回复
那需要强制转换?是吗:
if(NULL==thd)
{
CloseHandle((void*)thd);
}
WaitForSingleObject((void*)thd,INFINITE);
bobob 2005-10-31
  • 打赏
  • 举报
回复
BOOL 是被定义成int
bool才是关键字
lwjwj1314 2005-10-31
  • 打赏
  • 举报
回复
那返回值呢?
goodboyws 2005-10-31
  • 打赏
  • 举报
回复
typedef void* HANDLE;

15,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧