Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Thursday, June 01, 2006

use CAuxStdThunk with _beginthreadex

There is some guess that CAuxStdThunk could be used along with _beginthreadex, but nobody posted anything after googling, so I share a small code snippet here.

 

typedef unsigned (__stdcall *start_address )( void * );

class RedirectProcess:public CAuxStdThunk<RedirectProcess>

{

public:

        RedirectProcess( LPCTSTR application_name, LPCTSTR command_line):

        m_application_name(application_name), m_command_line(command_line)

        {

                InitThunk( (TMFP)&RedirectProcess::thread_procedure,this);

        }

        ~RedirectProcess()

        {

                OutputDebugString(__FUNCTION__);

        }

        unsigned __stdcall thread_procedure(void *p)

        {

                std::basic_stringstream<TCHAR> ts;

                ts<< _T("Process ID:") << getpid()

                  <<_T(" Thread id:")<<GetCurrentThreadId()

                        <<_T(", exe: ")<<m_application_name

                        <<_T(", command line:")<<m_command_line

                <<_T(" sample made by Onega (www.fruitfruit.com) ");

                OutputDebugString(ts.str().c_str());

                ts.clear();

                delete this;

                return 0;

        }

private:

        std::basic_string<TCHAR> m_application_name;

        std::basic_string<TCHAR> m_command_line;

};

 

RedirectProcess *ptr_thread; // must be created in heap

ptr_thread = new RedirectProcess(exe,cmd_line); // need to pass two parameters

 

_beginthreadex(NULL, 0, (start_address)ptr_thread->GetThunk(), 0, 0, 0);

 

0 Comments:

Post a Comment

<< Home