I'm coding something about parsing text of which based on the logging text written by the target application on windows platform. In order to fetch specified text and make it work, I do this by the following steps:
1. Copy the target logging file to my parse folder
2. Read the logging text from my folder
3. Parse and some sh*t like that.
In step 1. the copy-file process by win32 api `CopyFile` may failed with errorcode 32 , and yes you can see that which means the target file has been occupied by the application.
So my question is there anyway to access the content of the file in some way ?
I mean , when the application was running , I use notepad.exe to open the logging text file , it never failed. I'm sure there must be a way to do this!
How to do this ?
What I have tried:
My copy-file code chunks like this :
int ArchiveLogFiles(pLoger loger) {
char archive_path[MAX_PATH]={0};
GetCurrentDirectory(MAX_PATH,archive_path);
strcat(archive_path,"\\Archives");
DWORD att=GetFileAttributes(archive_path);
if((att&FILE_ATTRIBUTE_DIRECTORY)!=0) CreateDirectory(archive_path,NULL);
strcat(archive_path,"\\");
char exists_file[MAX_PATH]={0};
sprintf(exists_file,"%s%s",loger->folder,loger->file);
char archive_file[MAX_PATH]={0};
sprintf(archive_file,"%s%s",archive_path,loger->file);
if(0!=CopyFile(exists_file,archive_file,FALSE)) {
memset(loger->folder,0x00,sizeof(loger->folder));
strcpy(loger->folder,archive_path);
return 0;
} else {
char error_msg[1024]={0};
DWORD error=GetLastError();
LPSTR lpMsgBuf=NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0,NULL);
sprintf(error_msg,"拷贝文件\n[%s]\n至\n[%s]\n失败(%d-%s)!",exists_file,archive_file,error,lpMsgBuf);
LocalFree(lpMsgBuf);
SendMessage(GMAJOR->major_hwnd,MSG_HINT,(WPARAM)error_msg,(LPARAM)0);
if(loger->innerCode==DXWORKER||
loger->innerCode==MAIN_TXT||
loger->innerCode==CORELOG) {
}
return -1;
}
}
If I open the target logging file directly without copy, also failed:
int loadfile(char* filename,char** mem_ref,size_t* psize) {
*psize=0;
*mem_ref=NULL;
int result=0;
HANDLE hFile=CreateFile(filename,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE) {
DWORD error=GetLastError();
LPSTR lpMsgBuf=NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0,NULL);
printf("打开文件[%s]失败,%d-%s\n",filename,error,lpMsgBuf);
LocalFree(lpMsgBuf);
return -1;
}
*psize=GetFileSize(hFile,NULL);
char* buffer=(char*)calloc(sizeof(char)*((*psize)+1),1);
if(buffer==NULL) {
printf("内存分配异常: 分析文件 [%s] 失败。\n",filename);
CloseHandle(hFile);
return -1;
}
DWORD bytesRead=0;
if(!ReadFile(hFile,buffer,*psize,&bytesRead,NULL)) {
result=-1;
*psize=0;
printf("读取文件[%s]内容失败,%d\n",filename,GetLastError());
} else {
*psize=bytesRead;
*mem_ref=buffer;
}
CloseHandle(hFile);
return result;
}