#include
#include
#include
#include
#include
#define BUF_SIZE
typedef struct MyData {
int nVal1;
int nVal2;
} MYDATA, *PMYDATA;
using namespace std;
DWORD WINAPI MyThread( LPVOID lpParam );
int main(void)
{
HANDLE hProcess = NULL;
DWORD dwProcessId = ;
HANDLE hThread = NULL;
DWORD dwThreadId = ;
PMYDATA pData;
hProcess = GetCurrentProcess(); //进程伪句柄
cout <
DuplicateHandle(GetCurrentProcess(),
GetCurrentProcess(),
GetCurrentProcess(),
&hProcess,
,
FALSE,
DUPLICATE_SAME_ACCESS
);
cout <
dwProcessId = GetCurrentProcessId();
cout <
// Allocate memory for thread data.
pData = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));
if( pData == NULL )
{
ExitProcess();
}
// Generate unique data for each thread.
pData->nVal1 = ;
pData->nVal2 = ;
// Create Thread
hThread = CreateThread( NULL, // default security attributes
, // use default stack size
MyThread, // thread function
pData, // argument to thread function
, // use default creation flags
&dwThreadId); // returns the thread identifier
cin.get();
return ;
}
DWORD WINAPI MyThread( LPVOID lpParam )
{
HANDLE hThread = NULL;
DWORD dwThreadId = ;
hThread = GetCurrentThread(); //线程伪句柄
cout <
DuplicateHandle(GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&hThread,
,
FALSE,
DUPLICATE_SAME_ACCESS
);
cout <
dwThreadId = GetCurrentThreadId();
cout <
HANDLE hStdout;
PMYDATA pData;
TCHAR msgBuf[BUF_SIZE] = {};
size_t cchStringSize;
DWORD dwChars;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if( hStdout == INVALID_HANDLE_VALUE )
return ;
// Cast the parameter to the correct data type.
pData = (PMYDATA)lpParam;
// Print the parameter values using thread-safe functions.
StringCchPrintf(msgBuf,
BUF_SIZE,
TEXT("Parameters = %d, %d\n"),
pData->nVal1,
pData->nVal2);
StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
WriteConsole(hStdout, msgBuf, cchStringSize, &dwChars, NULL);
return ;
}