//WindowsXp文件读写模块
//FileOp.h
//////////////////////////////////////////////////////
#ifndef __FILE_OP_H__
#define __FILE_OP_H__
//////////////////////////////////////////////////////
#include <Windows.h>
//////////////////////////////////////////////////////
#define FILE_OP_API __declspec(dllexport)
//////////////////////////////////////////////////////
FILE_OP_API HANDLE _CreateFileForRead(const wchar_t* _lpszFileName);
FILE_OP_API HANDLE _CreateFileForWrite(const wchar_t* _lpszFileName);
FILE_OP_API void _SetFilePointerToBegin(HANDLE _hFile);
FILE_OP_API void _SetFilePointerToEnd(HANDLE _hFile);
FILE_OP_API void _SetFilePointerToPos(HANDLE _hFile,LONG _lPos);
FILE_OP_API long _GetFilePointerPos(HANDLE _hFile);
FILE_OP_API long _ReadFile(HANDLE _hFile,void* _lpData,long _lSize);
FILE_OP_API long _WriteFile(HANDLE _hFile,void* _lpData,long _lSize);
FILE_OP_API void _CloseFile(HANDLE _hFile);
//////////////////////////////////////////////////////
#endif//__FILE_OP_H__
//////////////////////////////////////////////////////
//FileOp.cpp
//////////////////////////////////////////////////////
#include <windows.h>
#include "FileOp.h"
#include <assert.h>
//////////////////////////////////////////////////////
FILE_OP_API HANDLE _CreateFileForRead(const wchar_t* _lpszFileName)
{
int iCreateFileCount = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
iCreateFileCount = 0;
do
{
if (0 < iCreateFileCount)
{
Sleep(10);
}
hFile = CreateFile(_lpszFileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
iCreateFileCount ++;
}while (INVALID_HANDLE_VALUE == hFile && iCreateFileCount < 5);
assert (INVALID_HANDLE_VALUE != hFile);
if (INVALID_HANDLE_VALUE == hFile)
{
hFile = NULL;
}
return hFile;
}
FILE_OP_API HANDLE _CreateFileForWrite(const wchar_t* _lpszFileName)
{
int iCreateFileCount = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
iCreateFileCount = 0;
do
{
if (0 < iCreateFileCount)
{
Sleep(10);
}
hFile = CreateFile(_lpszFileName,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
iCreateFileCount ++;
}while (INVALID_HANDLE_VALUE == hFile && iCreateFileCount < 5);
assert (INVALID_HANDLE_VALUE != hFile);
if (INVALID_HANDLE_VALUE == hFile)
{
hFile = NULL;
}
return hFile;
}
FILE_OP_API void _SetFilePointerToBegin(HANDLE _hFile)
{
if (NULL != _hFile)
{
SetFilePointer(_hFile,0,0,FILE_BEGIN);
}
}
FILE_OP_API void _SetFilePointerToEnd(HANDLE _hFile)
{
if (NULL != _hFile)
{
SetFilePointer(_hFile,0,0,FILE_END);
}
}
FILE_OP_API void _SetFilePointerToPos(HANDLE _hFile,LONG _lPos)
{
if (NULL != _hFile)
{
SetFilePointer(_hFile,_lPos,0,FILE_BEGIN);
}
}
FILE_OP_API long _GetFilePointerPos(HANDLE _hFile)
{
if (NULL != _hFile)
{
return SetFilePointer(_hFile,0,0,FILE_CURRENT);
}
return 0;
}
FILE_OP_API long _ReadFile(HANDLE _hFile,void* _lpData,long _lSize)
{
BOOL rel = FALSE;
DWORD dwNumberOfBytesRead = 0;
if (NULL != _hFile)
{
rel = ReadFile(_hFile,_lpData,_lSize,&dwNumberOfBytesRead,0);
assert (rel);
if (rel)
{
return dwNumberOfBytesRead;
}
return -1;
}
return -2;
}
FILE_OP_API long _WriteFile(HANDLE _hFile,void* _lpData,long _lSize)
{
BOOL rel = FALSE;
DWORD dwNumberOfBytesWritten = 0;
if (NULL != _hFile)
{
rel = WriteFile(_hFile,_lpData,_lSize,&dwNumberOfBytesWritten,0);
assert (rel);
if (rel)
{
return dwNumberOfBytesWritten;
}
return -1;
}
return -2;
}
FILE_OP_API void _CloseFile(HANDLE _hFile)
{
if (NULL != _hFile)
{
CloseHandle(_hFile);
_hFile = NULL;
}
}
//////////////////////////////////////////////////////