C++ CreateThread() LPSTR 参数错误 Windows 7 (64)

标签 c++ windows createthread

我正在尝试使用 CreateThread() 函数创建一个简单的程序。我需要将 LPSTR 参数传递给结构 pDataArray 中的新线程函数。我遵循 MSDN ( MSDN CreateThread() Example) 的示例。

typedef struct MyData {
LPSTR val1;
int val2;
} MYDATA, *PMYDATA;

主函数 (CreateNewThread) 从另一个函数接收带有示例文本 "hi all"LPSTR 命令 变量并正常工作,并将其正确引入结构中,但是当我尝试将它发送到线程函数 (MyThreadFunction) 时,它会返回很多奇怪的字符,我想是内存转储:

DEBUG: Inside CreateThread string: hi all
DEBUG: Before send to function: hi all
DEBUG: Inside MyThreadFunction data: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠☻☻☻☻WinSock 2.0

CreateThread函数的代码是:

int CreateNewThread(LPSTR command, int numThread)
{
    printf("\nDEBUG: Inside CreateThread string: %s\n",command);
    PMYDATA pDataArray[MAX_THREADS];
    DWORD   dwThreadIdArray[MAX_THREADS];
    HANDLE  hThreadArray[MAX_THREADS]; 

    // Allocate memory for thread data.

    pDataArray[numThread] = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
            sizeof(MYDATA));

    if( pDataArray[numThread] == NULL )
    {
        ExitProcess(2);
    }

    // Generate unique data for each thread to work with.

    pDataArray[numThread]->val1 = command;
    pDataArray[numThread]->val2 = 100;

    printf("\nDEBUG: Before send to function: %s\n",pDataArray[numThread]->val1);

    // Create the thread to begin execution on its own.

    hThreadArray[numThread] = CreateThread( 
        NULL,                           // default security attributes
        0,                              // use default stack size  
        MyThreadFunction,               // thread function name
        pDataArray[numThread],          // argument to thread function 
        0,                              // use default creation flags 
        &dwThreadIdArray[numThread]);   // returns the thread identifier 


    if (hThreadArray[numThread] == NULL) 
    {
       ErrorHandler(TEXT("CreateThread"));
       ExitProcess(3);
    }   
    return 0;
}

MyThreadFunction() 的代码是:

DWORD WINAPI MyThreadFunction( LPVOID lpParam ){

PMYDATA pDataArray;
pDataArray = (PMYDATA)lpParam;
LPSTR data = pDataArray->val1;
printf("\nDEBUG: Inside MyThreadFunction data: %s\n",data);
return 1;

}

谁能告诉我将 LPSTR 变量发送到线程函数的正确方法是什么?

谢谢你的建议。

最佳答案

您似乎正在将临时 LPSTR 传递给 CreateNewThread(),因此在执行 MyThreadFunction() 时它已经删除。例如,如果你做这样的事情就会发生:

CreateNewThread(std::string("hi all").c_str(), 0);

或者像这样:

std::string f() { return "hi all"; }
CreateNewThread(f().c_str(), 0);

关于C++ CreateThread() LPSTR 参数错误 Windows 7 (64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672281/

相关文章:

c++ - 在 C++ 复 vector 的每个元素上调用 conj() 函数

c++ - 在 Fedora 26 中从源代码安装 g++

c++ - 在 C++ lldb 中使用重载运算符评估表达式

c - 如何使用 CreateThread() 创建多个线程,每个线程具有不同的 ThreadProc() 函数

c++ - 在 C# (protobuf-net) 中序列化,在 C++ (protobuf) 中反序列化 : More than 5 fields in class

windows - Angular 4 错误 : No provider for ChildrenOutletContexts in Karma-Jasmine Test

c++ - 我必须在线程句柄上使用 CloseHandle() 吗?

Python:如何启动完整进程而不是子进程并检索 PID

正确终止正在运行的线程 "works"

c - TerminateThread() 在终止线程时返回错误