Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. a problem on threads

a problem on threads

Scheduled Pinned Locked Moved C / C++ / MFC
securityhelptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    keerthikaaa
    wrote on last edited by
    #1

    hai to all i have created a thread function void ThreadRoute1( void* arg ) { USES_CONVERSION; HANDLE hDir = CreateFile( "C:\\", // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024]; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString helper_txt; switch(Buffer[0].Action) { case FILE_ACTION_ADDED: break; case FILE_ACTION_REMOVED:break; case FILE_ACTION_MODIFIED: break; case FILE_ACTION_RENAMED_OLD_NAME: break; case FILE_ACTION_RENAMED_NEW_NAME: break; } int i=0; do { m_Sec.Lock(); int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) ); pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S")); i++; m_Sec.Unlock(); } while (!Buffer[i].NextEntryOffset); } } i have called the thre

    P S 2 Replies Last reply
    0
    • K keerthikaaa

      hai to all i have created a thread function void ThreadRoute1( void* arg ) { USES_CONVERSION; HANDLE hDir = CreateFile( "C:\\", // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024]; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString helper_txt; switch(Buffer[0].Action) { case FILE_ACTION_ADDED: break; case FILE_ACTION_REMOVED:break; case FILE_ACTION_MODIFIED: break; case FILE_ACTION_RENAMED_OLD_NAME: break; case FILE_ACTION_RENAMED_NEW_NAME: break; } int i=0; do { m_Sec.Lock(); int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) ); pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S")); i++; m_Sec.Unlock(); } while (!Buffer[i].NextEntryOffset); } } i have called the thre

      P Offline
      P Offline
      Parthiban
      wrote on last edited by
      #2

      hi u can pass a void pointer parameter to thread by _beginthread() function. _beginthread() syntex: _beginthread(threadname, stack_size, void *parameter); in ur case, _beginthread(ThreadRoute1, 0, (void *)filepath); In thread(): void ThreadRoute1( void* arg ) { char *filepath; filepath=arg; ......... ........ } By Parthiban

      K 1 Reply Last reply
      0
      • K keerthikaaa

        hai to all i have created a thread function void ThreadRoute1( void* arg ) { USES_CONVERSION; HANDLE hDir = CreateFile( "C:\\", // pointer to the file name FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_DELETE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024]; DWORD BytesReturned; while( ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_CREATION| FILE_NOTIFY_CHANGE_LAST_ACCESS| FILE_NOTIFY_CHANGE_LAST_WRITE| FILE_NOTIFY_CHANGE_SIZE| FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions &BytesReturned, // bytes returned NULL, // overlapped buffer NULL// completion routine )) { CTime tm = CTime::GetCurrentTime(); CString helper_txt; switch(Buffer[0].Action) { case FILE_ACTION_ADDED: break; case FILE_ACTION_REMOVED:break; case FILE_ACTION_MODIFIED: break; case FILE_ACTION_RENAMED_OLD_NAME: break; case FILE_ACTION_RENAMED_NEW_NAME: break; } int i=0; do { m_Sec.Lock(); int item = pList1->InsertItem(pList1->GetItemCount(), CString(Buffer[i].FileName).Left(Buffer[i].FileNameLength / 2) ); pList1->SetItemText(item, 1, tm.Format("%Y/%m/%d/ - %H:%M:%S")); i++; m_Sec.Unlock(); } while (!Buffer[i].NextEntryOffset); } } i have called the thre

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        struct DataYouWantToPass {     CString AString;     int ANumber; };   // Now to pass the data. DataYouWantToPass *pData = new DataYouWantToPass; // Set data here. _beginthread(ThreadRoute1, 0, reinterpret_cast<void*>(pData));   // As the thread function. void ThreadRoute1( void* arg ) {     DataYouWantToPass *pData = reinterpret_cast<DataYouWantToPass*>(arg); // Blah, blah, blah... Don't forget to see that the data is deleted when it's no longer needed. Steve

        K 1 Reply Last reply
        0
        • P Parthiban

          hi u can pass a void pointer parameter to thread by _beginthread() function. _beginthread() syntex: _beginthread(threadname, stack_size, void *parameter); in ur case, _beginthread(ThreadRoute1, 0, (void *)filepath); In thread(): void ThreadRoute1( void* arg ) { char *filepath; filepath=arg; ......... ........ } By Parthiban

          K Offline
          K Offline
          keerthikaaa
          wrote on last edited by
          #4

          hai parthiban thanks for ur reply. but a small problem is there an error is coming when i do this. can u rectify it. error C2440: '=' : cannot convert from 'void *' to 'char *'

          S 1 Reply Last reply
          0
          • S Stephen Hewitt

            struct DataYouWantToPass {     CString AString;     int ANumber; };   // Now to pass the data. DataYouWantToPass *pData = new DataYouWantToPass; // Set data here. _beginthread(ThreadRoute1, 0, reinterpret_cast<void*>(pData));   // As the thread function. void ThreadRoute1( void* arg ) {     DataYouWantToPass *pData = reinterpret_cast<DataYouWantToPass*>(arg); // Blah, blah, blah... Don't forget to see that the data is deleted when it's no longer needed. Steve

            K Offline
            K Offline
            keerthikaaa
            wrote on last edited by
            #5

            hai stephen thanks for ur reply but in my problem i have got the directory path in a edit box control variable. so how can i pass that variable to the thread function please help me to solve this problem :)

            P S 2 Replies Last reply
            0
            • K keerthikaaa

              hai parthiban thanks for ur reply. but a small problem is there an error is coming when i do this. can u rectify it. error C2440: '=' : cannot convert from 'void *' to 'char *'

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              C++ (unlike C) has to implicit converion from void*. Use explicit conversion: char *pFilePath = reinterpret_cast<char*>(arg); Steve

              1 Reply Last reply
              0
              • K keerthikaaa

                hai stephen thanks for ur reply but in my problem i have got the directory path in a edit box control variable. so how can i pass that variable to the thread function please help me to solve this problem :)

                P Offline
                P Offline
                Parthiban
                wrote on last edited by
                #7

                hi wat is type of the member variable? if type of the variable is value then u can directly assign it to the cstring variable of structure and pass it. eg: structvar.data=m_FilePath;

                1 Reply Last reply
                0
                • K keerthikaaa

                  hai stephen thanks for ur reply but in my problem i have got the directory path in a edit box control variable. so how can i pass that variable to the thread function please help me to solve this problem :)

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  You have to be very careful. He's why: // Bad code!!! void ThreadRoute1( void* arg ) { int *pInt = reinerpret_cast<int*>(arg); // Use 'pInt' here... }   // Call to start thread. { int v = 1; _beginthread(ThreadRoute1, 0, reinterpret_cast<int*>(&v)); } // After this point "v" no longer exists. This is a recipe for disaster because for all you know the parent thread can make it to the point where "v" is out of scope before it is accessed in the child thread. This is why I use new in my previous example. He's a reworked version. void ThreadRoute1( void* arg ) { int *pInt = reinerpret_cast<int*>(arg); // Use pInt. delete pInt; }   // Call to start thread. int *pInt = new int(1); if ( _beginthread(ThreadRoute1, 0, reinterpret_cast<void*>(pInt)) == -1 ) { delete pInt; } Steve

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups