Win98 CreateThread()

Discussion in 'Programmer's Corner' started by TheFox, Jul 15, 2012.

  1. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    I'm trying to get a program to create a thread on win98. I'm compiling it on winxp x64

    My issue with the following code:
    it's simply not creating the thread, it fails on the CreateThread(). Not exactly sure what I'm doing wrong on this. I'm probably missing a step, but they don't support win98 on msdn anymore.

    Sadly, it works correctly on winxp x64

    Any hints as to where I'm going wrong, would be grateful.

    It would be a lot easier to not have to compile on the target computer, if at all possible.

    I'm using Code::Blocks and gcc.


    //Timer.cpp
    Code ( (Unknown Language)):
    1. #include "Timer.hpp"
    2. #include <time.h>
    3. #include <windows.h>
    4. #include <iostream>
    5. tm *TargetTime = NULL;
    6. HANDLE ThreadHandle = 0;
    7. DWORD *ThreadID = NULL;
    8. SECURITY_ATTRIBUTES SecurityAttributes;
    9.  
    10. DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam );
    11.  
    12. void TimeStartup()
    13. {
    14.     std::cout << "Message: Timer loading...";
    15.     time_t rawtime;
    16.  
    17.     time ( &rawtime );
    18.     TargetTime = localtime ( &rawtime );
    19.     SECURITY_DESCRIPTOR SD;
    20.     if(InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION))std::cout << "...";//std::cout << "inti\n";
    21.     else{std::cout << "InitializeSecurityDescriptor fail\n";return ;}
    22.     if(SetSecurityDescriptorDacl(&SD, TRUE,(PACL)NULL, FALSE))std::cout << "...";//std::cout << "SetSecurity\n";
    23.     else{std::cout << "SetSecurityDescriptorDacl fail\n";return ;}
    24.     SecurityAttributes.lpSecurityDescriptor = &SD;
    25.  
    26.     ThreadHandle = CreateThread(&SecurityAttributes, 0, __Thread_TimeCheck, ThreadID, /*CREATE_SUSPENDED*/ 0, ThreadID);
    27.     if(ThreadHandle != 0 ) std::cout << "...";
    28.     else
    29.     {
    30.         std::cout << "ThreadCreation Fail\n"; return ;
    31.     }
    32.     ResumeThread(ThreadHandle);
    33.     std::cout << "Loaded\n";
    34.     return ;
    35. }
    36. void SetTargetTime(int HH,int MM,int SS)
    37. {
    38.     TargetTime->tm_hour = HH;
    39.     TargetTime->tm_min = MM;
    40.     TargetTime->tm_sec = SS;
    41.     return ;
    42. }
    43.  
    44. bool IsTimeAt()
    45. {
    46.     time_t rawtime;
    47.     tm * timeinfo;
    48.  
    49.     time ( &rawtime );
    50.     timeinfo = localtime ( &rawtime );
    51.     if(TargetTime->tm_hour != timeinfo->tm_hour)return false;
    52.     if(TargetTime->tm_min != timeinfo->tm_min)return false;
    53.     if(TargetTime->tm_sec != timeinfo->tm_sec)return false;
    54.  
    55.     Sleep(100);
    56.     return true;
    57. }
    58.  
    59. void TimeEnd()
    60. {
    61.     CloseHandle(ThreadHandle);
    62.     return ;
    63. }
    64.  
    65. tm* GetTime()
    66. {
    67.     return TargetTime;
    68. }
    69. DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam )
    70. {
    71.     std::cout << "\nMessage: Thread created.\nCommand:";
    72.     while(true)
    73.     {
    74.         if(IsTimeAt() == true)SendMoMessage("ATZ");
    75.         Sleep(15);
    76.     }
    77.     return 0;
    78. }
     
  2. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    TheFox likes this.
  3. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    Thanks, it's my problem, read that. It's why I have:

    Code ( (Unknown Language)):
    1.  
    2. DWORD *ThreadID = NULL;
    3. ThreadHandle = CreateThread(&SecurityAttributes, ThreadID, __Thread_TimeCheck, 0, /*CREATE_SUSPENDED*/ 0, ThreadID);
    4.  
    I know it's typedef of DWORD*, but I even tried LPDWORD ThreadID = NULL;

    With GetLastError(), I'm getting the same error as the guy you linked me. Thanks for that.
     
  4. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    I was wrong. My issue was with the SECURITY_ATTRIBUTES, I removed it, and now it works correctly.

    Here's the fixed code

    Code ( (Unknown Language)):
    1. #include "Timer.hpp"
    2. #include <time.h>
    3. #include <windows.h>
    4. #include <iostream>
    5. tm *TargetTime = NULL;
    6. HANDLE ThreadHandle = 0;
    7. DWORD ThreadID = NULL;
    8. SECURITY_ATTRIBUTES SecurityAttributes;
    9.  
    10. DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam );
    11.  
    12. bool TimeStartup()
    13. {
    14.     std::cout << "Message: Timer loading...";
    15.     time_t rawtime;
    16.  
    17.     time ( &rawtime );
    18.     TargetTime = localtime ( &rawtime );
    19.  
    20.  
    21.     ThreadHandle = CreateThread(NULL, 0, __Thread_TimeCheck, 0, 0, &ThreadID);
    22.     if(ThreadHandle == 0 )
    23.     {
    24.         std::cout << "ThreadCreation Failz:: " << GetLastError() <<'\n'; return false;
    25.     }
    26.     std::cout << "Loaded\n";
    27.     return true;
    28. }
    29.  
    30.  
    31. void SetTargetTime(int HH,int MM,int SS)
    32. {
    33.     TargetTime->tm_hour = HH;
    34.     TargetTime->tm_min = MM;
    35.     TargetTime->tm_sec = SS;
    36.     return ;
    37. }
    38.  
    39. bool IsTimeAt()
    40. {
    41.     time_t rawtime;
    42.     tm * timeinfo;
    43.  
    44.     time ( &rawtime );
    45.     timeinfo = localtime ( &rawtime );
    46.     if(TargetTime->tm_hour != timeinfo->tm_hour)return false;
    47.     if(TargetTime->tm_min != timeinfo->tm_min)return false;
    48.     //if(TargetTime->tm_sec != timeinfo->tm_sec)return false;
    49.  
    50.     Sleep(10000);
    51.     return true;
    52. }
    53.  
    54. void TimeEnd()
    55. {
    56.     CloseHandle(ThreadHandle);
    57.     return ;
    58. }
    59.  
    60. tm* GetTime()
    61. {
    62.     return TargetTime;
    63. }
    64. DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam )
    65. {
    66.     std::cout << "\nMessage: Thread created.\nCommand:";
    67.     while(true)
    68.     {
    69.         if(IsTimeAt() == true)SendMoMessage("ATZ");
    70.         Sleep(15);
    71.     }
    72.     return 0;
    73. }
     
  5. aprillove1719

    New Member

    Jul 17, 2012
    13
    0
    did you ask your problem to any programer? maybe they can answer your question.:)
     
  6. Wendy

    Moderator

    Mar 24, 2008
    20,765
    2,535
    Programmer? He is the programmer. DIY programming.
     
  7. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    Thanks, but I am the programmer. The last post I made in this thread was how I fixed my issue. Thanks to help of cheezewizz.