简体   繁体   中英

C++ CreateThread() LPSTR argument error Windows 7 (64)

i'm trying to create a simple program using CreateThread() function. I need to pass an LPSTR argument to the new thread function inside struct pDataArray . Im following the example of MSDN ( MSDN CreateThread() Example ).

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

The main function (CreateNewThread) receives the LPSTR command variable with example text "hi all" from another function and works correctly and it introduces correctly into the struct, but when i try to send it to the threaded function (MyThreadFunction) it returns a lot of strange caracters, memory dump i supose:

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

The code of the CreateThread function is:

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;
}

And the code of MyThreadFunction() is:

DWORD WINAPI MyThreadFunction( LPVOID lpParam ){

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

}

Can someone tell me what is the correct way to send an LPSTR variable to a threaded function?

Thanks in advice.

It seems that you are passing a temporary LPSTR to CreateNewThread() , so by the time of execution of MyThreadFunction() it's already deleted. For example it can happen if you do something like this:

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

or like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM