【100分】内存文件映射之后,急!!
内存文件映射之后,怎么读写文件啊?
最后一步
pA = MapViewOfFile(MapA, FILE_MAP_READ, 0, 0, 0)
之后,怎么读取或写入二进制文件
[解决办法]
参考:
http://topic.csdn.net/t/20010616/20/160969.html
http://www.fixdown.com/wz/article/14/56/2006/28517.htm
[解决办法]
请参考vc的代码:
class CFileMap
{
public:
CFileMap()
{
t_hFile = INVALID_HANDLE_VALUE;
t_hFileMap = NULL;
t_lpMap = NULL;
};
~CFileMap()
{
};
public:
LPVOID Open(LPCTSTR lpszFile2Map,LPCTSTR lpszMapName = NULL,const LPDWORD lpdwFileLen = NULL)
{
LPVOID lpRet = NULL;
if(lpdwFileLen)
*lpdwFileLen = 0;
::SetLastError(0);
__try
{
t_hFile = ::CreateFile(lpszFile2Map,GENERIC_WRITE|GENERIC_READ,
0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(!IsValidHandle(t_hFile))
{
DWORD dwError = ::GetLastError();
MBAboutError(dwError);//02227528256
//13910537794
//87896688-17791
__leave;
}
t_hFileMap = ::CreateFileMapping(t_hFile,NULL,PAGE_READWRITE,0,0,lpszMapName);
if(!IsValidHandle(t_hFileMap))
__leave;
if(::GetLastError() == ERROR_ALREADY_EXISTS)
{
SAFE_CLOSE_HANDLE(t_hFileMap);
t_hFileMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,lpszMapName);
if(!IsValidHandle(t_hFileMap))
__leave;
}
lpRet = ::MapViewOfFile(t_hFileMap,FILE_MAP_ALL_ACCESS,0,0,0);
}
__finally
{
if(!lpRet)
{
SAFE_CLOSE_HANDLE(t_hFileMap);
SAFE_CLOSE_HANDLE(t_hFile);
}
else
{
t_lpMap = lpRet;
if(lpdwFileLen)
*lpdwFileLen = ::GetFileSize(t_hFile,NULL);
}
}
return lpRet;
};
BOOL Close()
{
if(t_lpMap)
{
::FlushViewOfFile(t_lpMap,0);
::UnmapViewOfFile(t_lpMap);
t_lpMap = NULL;
SAFE_CLOSE_HANDLE(t_hFileMap);
SAFE_CLOSE_HANDLE(t_hFile);
}
return TRUE;
};
operator HANDLE() const
{
return t_hFileMap;
};
protected:
HANDLE t_hFile;
HANDLE t_hFileMap;
LPVOID t_lpMap;
};
[解决办法]
我现在有个好简单的问题撒
窗体上有一个TEXT,一个COMMANDBUTTON,一个ADODC,一个DATAGRID
我想在TEXT输入要查询的表名
DATAGRID就显示记录
Private Sub Command1_Click()
Adodc1.RecordSource = "select * from " + Trim(Text1.Text)
Set DataGrid1.DataSource = Adodc1
End Sub
现在我输入第一个表名能显示。但是输入第二个的时候,点击了COMMANDBUTTON之后,DATAGIRD显示的还是第一个表的记录。请问,应该怎么写代码???
[解决办法]
memcpy() 直接操作指针