#include "stdafx.h"
#include<windows.h>
#include<strsafe.h>//win2003SDK必须安装 要不无此头文件。此文件是为了实现StringCchPrintf,StringCchLength。
#define MAX_THREADS 5
#define BUF_SIZE 255
typedef struct _MyData{
int val1;
int val2;
}MYDATA,*PMYDATA;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
HANDLE hStdout;
PMYDATA pData;
TCHAR msgBuf[BUF_SIZE];
size_t cchStringSize;
DWORD dwChars;
hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
if(hStdout==INVALID_HANDLE_VALUE)
return 1;
pData=(PMYDATA)lpParam;
StringCchPrintf(msgBuf,BUF_SIZE,TEXT("Parameters=%d,%d
"),pData->val1,pData->val2);
//printf("%d,%d
",pData->val1,pData->val2);
StringCchLength(msgBuf,BUF_SIZE,&cchStringSize);
WriteConsole(hStdout,msgBuf,cchStringSize,&dwChars,NULL);
HeapFree(GetProcessHeap(),0,pData);
return 0;
}
void main()
{
PMYDATA pData;
DWORD dwThreadId[MAX_THREADS];
HANDLE hThread[MAX_THREADS];
int i;
for(i=0;i<MAX_THREADS;i++)
{
pData=(PMYDATA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(MYDATA));
if(pData==NULL)
ExitProcess(2);
pData->val1=i;
pData->val2=i+100;
hThread[i]=CreateThread(
NULL,//defaultsecurityattributes
0,//usedefaultstacksize
ThreadProc,//threadfunction
pData,//argumenttothreadfunction
0,//usedefaultcreationflags
&dwThreadId[i]);//returnsthethreadidentifier
if(hThread[i]==NULL)
{
ExitProcess(i);
}
}
WaitForMultipleObjects(MAX_THREADS,hThread,TRUE,INFINITE);
for(i=0;i<MAX_THREADS;i++)
{
CloseHandle(hThread[i]);
}
system("pause");
}