33,319
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdio.h>
typedef BOOL(* PrinLog)(char *, DWORD);
PrinLog PrinLogDll = NULL;
//HINSTANCE hDllInst;
DWORD WINAPI Thread(LPVOID)
{
char szLog_thir[MAX_PATH] = {"\0"};
sprintf(szLog_thir,"Thread call PrinLogDll Function!");
PrinLogDll(szLog_thir, (strlen(szLog_thir)+1));
return 0;
}
BOOL main()
{
HINSTANCE hDllInst;
hDllInst = LoadLibrary("E:\\sourcesafe\\PrintLogDll\\Debug\\PrintLogDll.dll");
if (hDllInst == NULL)
{
printf("Load Library failed,GLE=%d\n",GetLastError());
return FALSE;
}
else
{
PrinLogDll = (PrinLog)GetProcAddress(hDllInst, "PrinLogDll");
}
HANDLE hThread[5];
for (int i= 0; i < 5; i++)
{
hThread[i] = CreateThread( NULL, 0, Thread, NULL,0, NULL);
if (hThread == INVALID_HANDLE_VALUE)
{
printf("CreateThread is failed,GLE=%d",GetLastError());
}
//Sleep(200);
}
WaitForMultipleObjects(5, hThread, FALSE, INFINITE);
FreeLibrary(hDllInst);
return TRUE;
}
extern "C" _declspec(dllexport)BOOL PrinLogDll(char *lpszLog, DWORD dwLogSizes)
{
WaitForSingleObject(hSemaphore, INFINITE);
//printf("%s",szLogName);
DWORD dwWriteFile = 0;
BOOL fSucess = FALSE;
DWORD dwFileSize = 0;
DWORD dwWriteSize = 0;
BOOL bwrtSucess = FALSE;
char szPrinLog[MAX_PATH] = {"\0"};
HANDLE hFile = NULL;
//dwFileSize = GetFileSize(g_hFile, NULL);
hFile = CreateFile(g_szLogName, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE )
{
printf("Create file failed,GLE=%d\n",GetLastError());
return FALSE;
}
dwFileSize = GetFileSize(hFile, NULL);
SYSTEMTIME stLocal;
char szCrtTime[MAX_PATH] = {"\0"};
char szLogPrint[MAX_PATH] = {"\0"};
GetLocalTime(&stLocal);
sprintf(szCrtTime, "%d/%d/%d %d:%d", stLocal.wYear,
stLocal.wMonth, stLocal.wDay,
stLocal.wHour, stLocal.wMinute,stLocal.wMinute);
sprintf(szLogPrint,"%s\t\t%s\t\t",szCrtTime,lpszLog);
printf("%s\n",szLogPrint);
fSucess = WriteFile(hFile, szLogPrint, (dwLogSizes + strlen(szCrtTime)+1),
&dwWriteFile, NULL);
if (!fSucess)
{
printf("Write file failed, GLE=%d",GetLastError());
CloseHandle(hFile);
return FALSE;
}
CloseHandle(hFile);
ReleaseSemaphore(hSemaphore, 1, NULL);
return TRUE;
}