Я не знаю, что показывает SoftIce, но передача this в качестве lpParameter функции CreateThread приводит к
Речь шла о _beginthreadex...CreateThread использовать не надо. У Рихтера описано почему.
У меня краха нет. Все работает.
И вообще:
"Пипыл - зацени мой ирокез":
.H -файл:
//---------------------------------------------------------------------------
// file mth.h 04.04.05
//---------------------------------------------------------------------------
#ifndef __MTH_H__
#define __MTH_H__
//---------------------------------------------------------------------------
#include "sasha.h"
//---------------------------------------------------------------------------
#define VER_W32S 0
#define VER_W95 1
#define VER_W98 2
#define VER_WNT 3
#define VER_WXP 4
#define MTH_DEBUG
/*
tpIdle The thread executes only when the system is idle-Windows
won't interrupt other threads to execute a thread
with tpIdle priority.
tpLowest The thread's priority is two points below normal.
tpLower The thread's priority is one point below normal.
tpNormal The thread has normal priority.
tpHigher The thread's priority is one point above normal.
tpHighest The thread's priority is two points above normal.
tpTimeCritical The thread gets highest priority.
*/
//---------------------------------------------------------------------------
class MyThread
{
private:
// void *make_addr (unsigned long (__stdcall MyThread::*)(void *));
volatile BOOL Terminated; // =false
HANDLE Handle;
void *prou;
int retcode;
unsigned WINAPI __Execute__ (LPVOID) ;
volatile char go_flag;
volatile int uninstall_place;
volatile int iw;
unsigned idTH;
int err;
unsigned CreateSuspended;
protected:
public:
MyThread (BOOL CreateSuspended=FALSE);
virtual ~MyThread(void);
virtual int WINAPI Execute () ;
virtual int WINAPI uninstall (void);
BOOL WINAPI GetTerminate (void);
void WINAPI type_message (char *mess, char *cap, unsigned int w);
void WINAPI Resume(void);
void WINAPI Suspend(void);
void WINAPI Terminate(void);
int WINAPI GetPriority (void);
void WINAPI SetPriority (int pr);
};
//---------------------------------------------------------------------------
#endif
.cpp - файл:
//---------------------------------------------------------------------------
// file mth.cpp 04.04.05
// класс - поток
//---------------------------------------------------------------------------
#ifndef _MT
#define _MT
#endif
#include <windows.h>
#include <process.h>
#include <string.h>
#include <stdio.h>
#include "mth.h"
#include "sasha.h"
#ifdef MTH_DEBUG
#include "vd.hpp"
#include "vsd.hpp"
#include "fj.ext"
#endif
//---------------------------------------------------------------------------
MyThread::MyThread (BOOL CrSus)
{
char pstr[20];
uninstall_place=0;
Terminated = false;
err=0;
go_flag=-1;
// преобразование типов:
// prou = make_addr (this->__Execute__);
sprintf (pstr, "%x", this->__Execute__);
sscanf (pstr, "%x", &prou);
if (CrSus) { CreateSuspended = CREATE_SUSPENDED; }
else { CreateSuspended = 0; }
Handle =NULL;
Handle =(HANDLE) _beginthreadex
(
(void *) NULL, // pointer to thread security attributes
(unsigned) 0, // initial thread stack size, in bytes
(unsigned (WINAPI *)(void *) )prou,
(void *) this, // argument for new thread
(unsigned) CreateSuspended, // creation flags
(unsigned *) (& idTH) // pointer to returned thread identifier
);
#ifdef MTH_DEBUG
sprintf (sss, "TROU=%x %x", prou, *((DWORD *)prou)); vs2->type_string (sss);
sprintf (sss, "THIS=%x %x", this, *((DWORD *)this)); vs2->type_string (sss);
sprintf (sss, "THIS->EXE=%x",this->__Execute__); vs2->type_string (sss);
#endif
if (Handle == NULL)
{
type_message
(
"Install Thread:ERR",
"Install Thread:ERR",
MB_OK | MB_ICONERROR | MB_APPLMODAL
);
err=1;
}
}
//---------------------------------------------------------------------------
MyThread::~MyThread(void)
{
}
//---------------------------------------------------------------------------
void WINAPI MyThread::type_message (char *mess, char *cap, unsigned int w)
{
::MessageBox(NULL,mess,cap,w);
}
//------------------------------------------------------------------------
int WINAPI MyThread::Execute ()
{
#ifdef MTH_DEBUG
vs2->type_string ("START EXECUTE", RGB(200,0,200));
#endif
for ( ; ; )
{
if ( MyThread::GetTerminate () ) break;
}
#ifdef MTH_DEBUG
vs2->type_string ("END EXECUTE", RGB(200,0,200));
#endif
return 0;
}
//------------------------------------------------------------------------
unsigned WINAPI MyThread::__Execute__ (LPVOID)
{
go_flag=0;
retcode = MyThread::Execute ();
go_flag=1;
_endthreadex (retcode);
return 0;
}
//------------------------------------------------------------------------
//
// Автомат завершения потока.
// Включается в WM_TIMER ~200-400- ms
//
// 0 - поток не завершен
// 1 - поток нормально завершен
// 2 - авария - что то сломалось.
//
int WINAPI MyThread::uninstall (void)
{
switch(uninstall_place)
{
case 0:
MyThread::Terminate();
uninstall_place=1;
iw=0;
return 0;
case 1:
if(go_flag leq 1){ uninstall_place=2;}
return 0;
case 2:
{
DWORD rcode;
if ( ::GetExitCodeThread ((HANDLE)Handle, &rcode) )
{
if (rcode lne STILL_ACTIVE) return 1;
}
else { ++ iw; }
if (iw leq 5) return 2;
return 0;
}
}
return 0;
}
//------------------------------------------------------------------------
void WINAPI MyThread::Resume (void)
{
if (err lne 0) return;
if (Handle == NULL) return;
::ResumeThread (Handle);
}
//------------------------------------------------------------------------
void WINAPI MyThread::Suspend (void)
{
if (err lne 0) return;
if (Handle == NULL) return;
::SuspendThread (Handle);
}
//------------------------------------------------------------------------
void WINAPI MyThread::Terminate (void)
{
Terminated=true;
}
//------------------------------------------------------------------------
BOOL WINAPI MyThread::GetTerminate (void)
{
return Terminated;
}
//------------------------------------------------------------------------
void WINAPI MyThread::SetPriority (int pr)
{
if (Handle leq NULL) return;
::SetThreadPriority (Handle,pr);
}
//------------------------------------------------------------------------
int WINAPI MyThread::GetPriority (void)
{
if (Handle leq NULL) return 0;
return (::GetThreadPriority (Handle) );
}
//---------------------------------------------------------------------------
/*
void *MyThread::make_addr (unsigned long (__stdcall MyThread::*)(void *))
{
void *p;
__asm
{
mov eax,[ebp+8]
mov p,eax
}
return p;
}
*/
//---------------------------------------------------------------------------