0

我想使用 _beginthreadex 并传递一个字符串,即域的名称。通过它的正确方法/最佳实践是什么?

  1. 通过变量本身(sDomain)?
    WCHAR sDomain[256] = {0};
    //...copy domain into sDomain
    UINT threadID = 0;
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &Thread_SaveDomainName, sDomain, 0, &threadID);
  1. 还是通过变量的地址(&sDomain)?
    WCHAR sDomain[256] = {0};
    //...copy domain into sDomain
    UINT threadID = 0;
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &Thread_SaveDomainName, &sDomain, 0, &threadID);
  1. 还是我制作一个结构并传递结构元素(&sDomain[0])?
    struct strDomain {TCHAR sDomain[256];};
    strDomain *sDomain = new strDomain[1]();
    //...copy domain into strDomain[0].sDomain
    UINT threadID = 0;
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &Thread_SaveDomainName, &sDomain[0], 0, &threadID);
4

1 回答 1

1

下面是最简单的代码实现,当然你可以自定义一些类型来传递,因为线程函数的参数类型是void,你可以做任何转换

#include <iostream>
using namespace std;

UINT Thread_SaveDomainName(LPVOID params)
{
    char* szDomain = (char*)params;
    cout<<szDomain<<endl;
    
    return 0;
}

int main()
{
    char* szDomain = "8080";
    UINT threadID = -1;
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &Thread_SaveDomainName, (void*)szDomain, 0, &threadID);
    
    return 0;
}
于 2020-01-09T08:31:11.767 回答