Win98 CreateThread()

Thread Starter

TheFox

Joined Apr 29, 2009
66
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
Rich (BB code):
#include "Timer.hpp"
#include <time.h>
#include <windows.h>
#include <iostream>
tm *TargetTime = NULL;
HANDLE ThreadHandle = 0;
DWORD *ThreadID = NULL;
SECURITY_ATTRIBUTES SecurityAttributes;

DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam );

void TimeStartup()
{
    std::cout << "Message: Timer loading...";
    time_t rawtime;

    time ( &rawtime );
    TargetTime = localtime ( &rawtime );
    SECURITY_DESCRIPTOR SD;
    if(InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION))std::cout << "...";//std::cout << "inti\n";
    else{std::cout << "InitializeSecurityDescriptor fail\n";return ;}
    if(SetSecurityDescriptorDacl(&SD, TRUE,(PACL)NULL, FALSE))std::cout << "...";//std::cout << "SetSecurity\n";
    else{std::cout << "SetSecurityDescriptorDacl fail\n";return ;}
    SecurityAttributes.lpSecurityDescriptor = &SD;

    ThreadHandle = CreateThread(&SecurityAttributes, 0, __Thread_TimeCheck, ThreadID, /*CREATE_SUSPENDED*/ 0, ThreadID);
    if(ThreadHandle != 0 ) std::cout << "...";
    else
    {
        std::cout << "ThreadCreation Fail\n"; return ;
    }
    ResumeThread(ThreadHandle);
    std::cout << "Loaded\n";
    return ;
}
void SetTargetTime(int HH,int MM,int SS)
{
    TargetTime->tm_hour = HH;
    TargetTime->tm_min = MM;
    TargetTime->tm_sec = SS;
    return ;
}

bool IsTimeAt()
{
    time_t rawtime;
    tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    if(TargetTime->tm_hour != timeinfo->tm_hour)return false;
    if(TargetTime->tm_min != timeinfo->tm_min)return false;
    if(TargetTime->tm_sec != timeinfo->tm_sec)return false;

    Sleep(100);
    return true;
}

void TimeEnd()
{
    CloseHandle(ThreadHandle);
    return ;
}

tm* GetTime()
{
    return TargetTime;
}
DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam )
{
    std::cout << "\nMessage: Thread created.\nCommand:";
    while(true)
    {
        if(IsTimeAt() == true)SendMoMessage("ATZ");
        Sleep(15);
    }
    return 0;
}
 

Thread Starter

TheFox

Joined Apr 29, 2009
66
Ideally after the call to CreateThread fails you should be calling GetLastError() to find the error code returned and the reason for failure. Could it be related to an issue like this?

https://groups.google.com/forum/?fromgroups#!topic/microsoft.public.win32.programmer.kernel/_pWwM8H6hrU
Thanks, it's my problem, read that. It's why I have:

Rich (BB code):
DWORD *ThreadID = NULL;
ThreadHandle = CreateThread(&SecurityAttributes, ThreadID, __Thread_TimeCheck, 0, /*CREATE_SUSPENDED*/ 0, ThreadID);
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.
 

Thread Starter

TheFox

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

Here's the fixed code

Rich (BB code):
#include "Timer.hpp"
#include <time.h>
#include <windows.h>
#include <iostream>
tm *TargetTime = NULL;
HANDLE ThreadHandle = 0;
DWORD ThreadID = NULL;
SECURITY_ATTRIBUTES SecurityAttributes;

DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam );

bool TimeStartup()
{
    std::cout << "Message: Timer loading...";
    time_t rawtime;

    time ( &rawtime );
    TargetTime = localtime ( &rawtime );


    ThreadHandle = CreateThread(NULL, 0, __Thread_TimeCheck, 0, 0, &ThreadID);
    if(ThreadHandle == 0 )
    {
        std::cout << "ThreadCreation Failz:: " << GetLastError() <<'\n'; return false;
    }
    std::cout << "Loaded\n";
    return true;
}


void SetTargetTime(int HH,int MM,int SS)
{
    TargetTime->tm_hour = HH;
    TargetTime->tm_min = MM;
    TargetTime->tm_sec = SS;
    return ;
}

bool IsTimeAt()
{
    time_t rawtime;
    tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    if(TargetTime->tm_hour != timeinfo->tm_hour)return false;
    if(TargetTime->tm_min != timeinfo->tm_min)return false;
    //if(TargetTime->tm_sec != timeinfo->tm_sec)return false;

    Sleep(10000);
    return true;
}

void TimeEnd()
{
    CloseHandle(ThreadHandle);
    return ;
}

tm* GetTime()
{
    return TargetTime;
}
DWORD WINAPI __Thread_TimeCheck( LPVOID lpParam )
{
    std::cout << "\nMessage: Thread created.\nCommand:";
    while(true)
    {
        if(IsTimeAt() == true)SendMoMessage("ATZ");
        Sleep(15);
    }
    return 0;
}
 
Top