大家... 我刚刚开始学习如何使用 Windows API 创建进程和线程。如果我希望线程在屏幕上显示整数,我的代码确实可以工作,但是在使用字符数组时它不起作用......有人可以告诉我我做错了什么吗?编码:
#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#include<tchar.h>
#include<strsafe.h>
#include<string.h>
#define N 4
#define bufferSize 255
DWORD WINAPI Threader(LPVOID Parameter);
typedef struct Data {
char value[bufferSize];
} Data, *pToData;
int main()
{
int i;
char c[bufferSize];
pToData threadData[N];
HANDLE handleArray[N];
DWORD threadID[N];
FILE *file=fopen("niti.txt", "r");
for(i=0; i<N; i++)
{
fscanf(file, "%s", c);
threadData[i] = (pToData) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Data));
strcpy(threadData[i]->value, c);
handleArray[i]=CreateThread(NULL, 0, Threader, threadData[i], 0, &threadID[i]);
}
WaitForMultipleObjects(N, handleArray, TRUE, INFINITE);
for(i=0; i<N; i++)
{
CloseHandle(handleArray[i]);
if(threadData[i] != NULL)
{
HeapFree(GetProcessHeap(), 0, threadData[i]);
threadData[i] = NULL;
}
}
fclose(file);
return 0;
}
DWORD WINAPI Threader(LPVOID Parameter)
{
HANDLE hStdOut;
TCHAR messageBuffer[bufferSize];
size_t cchStringSize;
DWORD dwChars;
pToData dataArray;
char temp[bufferSize];
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if( hStdOut == INVALID_HANDLE_VALUE )
return 1;
dataArray=(pToData)Parameter;
StringCchPrintf(messageBuffer, bufferSize, TEXT("Parameter = %s\n"), dataArray->value);
StringCchLength(messageBuffer, bufferSize, &cchStringSize);
WriteConsole(hStdOut, messageBuffer, (DWORD)cchStringSize, &dwChars, NULL);
return 0;
}
如果我将结构中的值更改为int并在main中执行相同操作,则输出是正确的。有什么建议么?谢谢!