M
mkarja
Hi,
I have a windows MDI program that draws some shapes that can
be saved into a file and read from that file.
The save seems to work with the WriteFile function, but for some
reason
the ReadFile function fails. These WriteFile/ReadFile are win32 api
functions.
I'll post both, the read and write function codes here so you can take
a
look at and perhaps see what the problem is.
---- code WriteFile start -----
BOOL WriteDrzFile(HWND hwnd, LPSTR pstrFile, PTHINGZDATA pThingzData)
{
HANDLE theFile;
BOOL bSuccess = FALSE;
DWORD dwSize;
char szTmp[255], szOut[255];
PtrThingzShapeSave thingzSaveData;
pThingzData->pController->shapez = pThingzData->pController-
theFile = CreateFile(pstrFile, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (theFile != INVALID_HANDLE_VALUE)
{
DWORD structLength;
structLength = sizeof(thingzSaveData);
if (structLength > 0)
{
RECT rect;
DWORD dwBufferSize = structLength + 1;
if (pThingzData != NULL)
{
DWORD dwWritten;
if(WriteFile(theFile, thingzSaveData, structLength, &dwWritten,
NULL))
{
bSuccess = TRUE;
}
}
}
CloseHandle(theFile);
}
return bSuccess;
}
---- code WriteFile end -----
---- code ReadFile start -----
BOOL ReadDrzFile(HWND hwnd, LPSTR pstrFile, static Controller * pCtrl)
{
HANDLE theFile;
DWORD dwSize;
char szTmp[255], szOut[255];
PtrThingzShapeSave thingzSaveData;
PTHINGZDATA pThingzData;
pThingzData->pController = pCtrl;
theFile = CreateFile(pstrFile, GENERIC_READ, FILE_SHARE_DELETE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);
if (theFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(theFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
if (pThingzData != NULL)
{
DWORD dwRead;
if(ReadFile(theFile, thingzSaveData, dwFileSize,
&dwRead, NULL))
{
pThingzData->pController->ShapeDrawFile(thingzSaveData, hwnd);
}
}
}
CloseHandle(theFile);
}
return FALSE;
}
---- code ReadFile end -----
In the code the thingzSaveData is a structure which holds the saved
elements.
If there's some more explanations needed, I will try to answer any
questions to
make it clearer.
Thanks in advance.
----
I have a windows MDI program that draws some shapes that can
be saved into a file and read from that file.
The save seems to work with the WriteFile function, but for some
reason
the ReadFile function fails. These WriteFile/ReadFile are win32 api
functions.
I'll post both, the read and write function codes here so you can take
a
look at and perhaps see what the problem is.
---- code WriteFile start -----
BOOL WriteDrzFile(HWND hwnd, LPSTR pstrFile, PTHINGZDATA pThingzData)
{
HANDLE theFile;
BOOL bSuccess = FALSE;
DWORD dwSize;
char szTmp[255], szOut[255];
PtrThingzShapeSave thingzSaveData;
pThingzData->pController->shapez = pThingzData->pController-
thingzSaveData = pThingzData->pController->shapez.thingzSaveData;_model.GetShapes ();
theFile = CreateFile(pstrFile, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (theFile != INVALID_HANDLE_VALUE)
{
DWORD structLength;
structLength = sizeof(thingzSaveData);
if (structLength > 0)
{
RECT rect;
DWORD dwBufferSize = structLength + 1;
if (pThingzData != NULL)
{
DWORD dwWritten;
if(WriteFile(theFile, thingzSaveData, structLength, &dwWritten,
NULL))
{
bSuccess = TRUE;
}
}
}
CloseHandle(theFile);
}
return bSuccess;
}
---- code WriteFile end -----
---- code ReadFile start -----
BOOL ReadDrzFile(HWND hwnd, LPSTR pstrFile, static Controller * pCtrl)
{
HANDLE theFile;
DWORD dwSize;
char szTmp[255], szOut[255];
PtrThingzShapeSave thingzSaveData;
PTHINGZDATA pThingzData;
pThingzData->pController = pCtrl;
theFile = CreateFile(pstrFile, GENERIC_READ, FILE_SHARE_DELETE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);
if (theFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(theFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
if (pThingzData != NULL)
{
DWORD dwRead;
if(ReadFile(theFile, thingzSaveData, dwFileSize,
&dwRead, NULL))
{
pThingzData->pController->ShapeDrawFile(thingzSaveData, hwnd);
}
}
}
CloseHandle(theFile);
}
return FALSE;
}
---- code ReadFile end -----
In the code the thingzSaveData is a structure which holds the saved
elements.
If there's some more explanations needed, I will try to answer any
questions to
make it clearer.
Thanks in advance.
----