CreateThread 将 long 传递给 lpParameter

发布于 12-15 04:57 字数 619 浏览 5 评论 0原文

编译器在第 3 行说“非法间接”。IDE 说“表达式必须是指向完整对象类型的指针”

001 DWORD WINAPI MyCallbackFunction( LPVOID lpvParam )
002 {
003    long value = (long) *lpvParam;
004    ...
005    return 0;
006 }
007
008 BOOL StartMyThread( long value )
009 {
010    DWORD dwThreadId;
011    BOOL result = FALSE;
012    HANDLE hThread = NULL;
013    hThread = CreateThread(NULL, 0, MyCallbackFunction, &value, NULL, &dwThreadId );
014    result = (NULL == hThread);
015    CloseHandle( hThread );
016    return result;
017 }

如果我将第 3 行更改为此,它可以编译,但会崩溃...

long value = (long) lpvParam;

Compiler is saying "illegal indirection" on line 3. The IDE says "expression must be a pointer to a complete object type"

001 DWORD WINAPI MyCallbackFunction( LPVOID lpvParam )
002 {
003    long value = (long) *lpvParam;
004    ...
005    return 0;
006 }
007
008 BOOL StartMyThread( long value )
009 {
010    DWORD dwThreadId;
011    BOOL result = FALSE;
012    HANDLE hThread = NULL;
013    hThread = CreateThread(NULL, 0, MyCallbackFunction, &value, NULL, &dwThreadId );
014    result = (NULL == hThread);
015    CloseHandle( hThread );
016    return result;
017 }

If I change line 3 to this, it compiles, but crashes...

long value = (long) lpvParam;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慢慢从新开始2024-12-22 04:57:05

您正在尝试引用 void* 指针,这是不允许的。您必须首先将其转换为另一种指针类型。由于您的参数是指向 long 的指针,因此您需要这样做:

long *value = (long*) lpvParam;

或者,如果线程不需要访问原始变量:

long value = * (long*) lpvParam;

不过,Seth 是对的。当线程实际开始运行时,原始变量将消失。如果您尝试将其值传递给线程,请执行以下操作:

// notice the '&' operator is gone now...
hThread = CreateThread(NULL, 0, MyCallbackFunction, (LPVOID)value, NULL, &dwThreadId );
...    
long value = (long) lpvParam;

You are trying to deference a void* pointer, which is not allowed. You have to cast it to another pointer type first. Since your parameter is a pointer to a long, you need to do this instead:

long *value = (long*) lpvParam;

Or, if the thread does not need access to the original variable:

long value = * (long*) lpvParam;

Seth is right, though. The original variable will be gone by the time the thread actually starts running. If you are trying to pass its value to the thread, do this instead:

// notice the '&' operator is gone now...
hThread = CreateThread(NULL, 0, MyCallbackFunction, (LPVOID)value, NULL, &dwThreadId );
...    
long value = (long) lpvParam;
可是我不能没有你2024-12-22 04:57:05

您给出了一个指向局部变量的指针,该变量可以被线程运行的类型销毁。最好在调用 CreateThread 时将 long 转换为 LPVOID,然后将其转换回 long MyCallbackFunction 中的 code> (请注意,没有发生指针取消引用)。

You're giving a pointer to a local variable which can be destroyed by the type the thread runs. It would be better to cast the long to an LPVOID in the call to CreateThread, then cast it back into a long (note that there is no pointer dereferencing taking place) inside MyCallbackFunction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文