创建的线程的地址和退出线程的地址不同

时间:2013-09-13 08:39:10

标签: multithreading memory-address

首先,它不是我的代码。我需要压缩文件,所以复制,粘贴,编辑一些代码。

直到昨天,它才起作用。但今天,当我重建这段代码时,我感觉有些不好的事情。这段代码卡在某些地方。现在也许我发现了这个问题。 它在创建的线程的地址和退出的线程的地址之间是不同的。

让我们看一下代码:

void compress(char* zipNamePath, char* targetFolderPath)
{
DWORD strlen = 0;
char szFrom[256],
     szTo[256];

strcpy(szFrom, targetFolderPath);
strcpy(szTo, zipNamePath);

HRESULT hResult;
IShellDispatch *pISD;
Folder *pToFolder = NULL;
VARIANT vDir, vFile, vOpt;
BSTR strptr1, strptr2;

CoInitialize(NULL);

hResult = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&pISD);

if  (SUCCEEDED(hResult) && pISD != NULL)
{
    strlen = MultiByteToWideChar(CP_ACP, 0, szTo, -1, 0, 0);
    strptr1 = SysAllocStringLen(0, strlen);
    MultiByteToWideChar(CP_ACP, 0, szTo, -1, strptr1, strlen);

    VariantInit(&vDir);
    vDir.vt = VT_BSTR;
    vDir.bstrVal = strptr1;
    hResult = pISD->NameSpace(vDir, &pToFolder);

    if  (SUCCEEDED(hResult))
    {
        strlen = MultiByteToWideChar(CP_ACP, 0, szFrom, -1, 0, 0);
        strptr2 = SysAllocStringLen(0, strlen);
        MultiByteToWideChar(CP_ACP, 0, szFrom, -1, strptr2, strlen);

        VariantInit(&vFile);
        vFile.vt = VT_BSTR;
        vFile.bstrVal = strptr2;

        VariantInit(&vOpt);
        vOpt.vt = VT_I4;
        vOpt.lVal = 4;          // Do not display a progress dialog box

        hResult = NULL;
        printf("Copying %s to %s ...\n", szFrom, szTo);
        hResult = pToFolder->CopyHere(vFile, vOpt); //NOTE: this appears to always return S_OK even on error
        /*
         * 1) Enumerate current threads in the process using Thread32First/Thread32Next
         * 2) Start the operation
         * 3) Enumerate the threads again
         * 4) Wait for any new threads using WaitForMultipleObjects
         *
         * Of course, if the operation creates any new threads that don't exit, then you have a problem. 
         */
        if (hResult == S_OK) {
            //NOTE: hard-coded for testing - be sure not to overflow the array if > 5 threads exist
            HANDLE hThrd[5]; 
            HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPALL ,0);  //TH32CS_SNAPMODULE, 0);
            DWORD NUM_THREADS = 0;
            if (h != INVALID_HANDLE_VALUE) {
                THREADENTRY32 te;
                te.dwSize = sizeof(te);
                if (Thread32First(h, &te)) {
                    do {
                        if (te.dwSize >= (FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)) ) {
                            //only enumerate threads that are called by this process and not the main thread
                            if((te.th32OwnerProcessID == GetCurrentProcessId()) && (te.th32ThreadID != GetCurrentThreadId()) ){
                                printf("Process 0x%04x Thread 0x%04x\n", te.th32OwnerProcessID, te.th32ThreadID);
                                hThrd[NUM_THREADS] = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
                                NUM_THREADS++;
                            }
                        }
                        te.dwSize = sizeof(te);
                    } while (Thread32Next(h, &te));
                }
                //CloseHandle(h);

                printf("waiting for all threads to exit...\n");
                //Wait for all threads to exit

                WaitForMultipleObjects(NUM_THREADS, hThrd , TRUE , INFINITE);

                //Close All handles
                for ( DWORD i = 0; i < NUM_THREADS ; i++ ){
                    CloseHandle( hThrd[i] );
                }
            } //if invalid handle
        } //if CopyHere() hResult is S_OK

        SysFreeString(strptr2);
        pToFolder->Release();
    }

    SysFreeString(strptr1);
    pISD->Release();
}

CoUninitialize();

printf ("Press ENTER to exit\n");
getchar();    
}

问题部分是:

if (Thread32First(h, &te)) {
    do {
        if (te.dwSize >= (FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)) ) {
            //only enumerate threads that are called by this process and not the main thread
            if((te.th32OwnerProcessID == GetCurrentProcessId()) && (te.th32ThreadID != GetCurrentThreadId()) ){
                printf("Process 0x%04x Thread 0x%04x\n", te.th32OwnerProcessID, te.th32ThreadID);
                hThrd[NUM_THREADS] = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
                NUM_THREADS++;
            }
        }
        te.dwSize = sizeof(te);
    } while (Thread32Next(h, &te));
}

创建它们时,这些线程地址为:

0x0de4, 0x0e34

当它们退出时,这些线程地址是:

0x0fbc, 0x0e34

第一个线程的地址已更改。请帮我。必须在韩国感恩节之前完成。

0 个答案:

没有答案