I am trying to get the size of a network file and then call a batch
file as an alarm. Also, I want to create a log text file and post the
results to that file. This will occur continuously at a regular interval
and the results will continually be appended to the log file.
I keep getting various errors from my first CreateFile call. The
last one was 87.
All of this is occurring on an NT network. I have full control of
the file and the directory but very few other accounts do.
I am completely lost on the CreateFile call. The first file will be
written to at any time and so I cannot lock the file so that it's
application cannot get to it. I don't understand all the security stuff.
Where can I go to understand how to use the security options? The second
file (the log file) will be on the local drive and the only app that
might lock it is this app.
How should I write my CreateFile and GetFileSize calls for the
results that I am looking for? How can I learn about the security
options used in CreateFile?
case WM_COMMAND :
switch( LOWORD( wParam ) )
{
case IDM_SAMPLE :
{
HANDLE hFile; // Used for the Watch file and the log file
DWORD dwWritten; // Used for CreateFile
SYSTEMTIME st; // A structure that holds local time info.
char szBuffer[128];
DWORD lnErrorMessage;
LPDWORD lpdwHigh; //This is used to store the HIGH part of the file
size.
LPSECURITY_ATTRIBUTES Security;
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,
Security,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | SECURITY_IMPERSONATION, NULL);
lnErrorMessage = GetLastError();
if (lnErrorMessage != ERROR_SUCCESS)
{
SysErrHandler( hWnd, lnErrorMessage);
}
dwSize = GetFileSize( hFile, lpdwHigh );
//dwSize = GetFileSize( hFile, NULL);
lnErrorMessage = GetLastError();
CloseHandle(hFile);
if (lnErrorMessage != ERROR_SUCCESS)
{
SysErrHandler( hWnd, lnErrorMessage);
}
if (dwSize != 0xFFFFFFFF)
{
if (dwSize > nMaxFileSize )
{
_spawnl(_P_WAIT, szCallFile, szCallFile, "_spawnl","two", NULL);
}
PostMessage( hWnd, WM_PAINT, 0, 0);
//Write results to the log file.
hFile = CreateFile( szLogFile, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, Security, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL |SECURITY_IMPERSONATION, NULL);
SetFilePointer( hFile, 0, 0, FILE_END);
GetLocalTime( &st );
if (dwSize > nMaxFileSize )
{
wsprintf( szBuffer, "FILE HAS EXCEEDED FILE SIZE LIMIT!!! %d:%d on
%d/%d/%d - %s %d/%d \n",
st.wHour, st.wMinute, st.wMonth,
st.wDay,st.wYear, szFileName, dwSize, nMaxFileSize);
}
else
{
wsprintf( szBuffer, "%d:%d on %d/%d/%d - %s %d/%d \n",
st.wHour, st.wMinute, st.wMonth,
st.wDay,st.wYear, szFileName, dwSize, nMaxFileSize);
}
WriteFile( hFile, szBuffer, lstrlen(szBuffer), &dwWritten, NULL);
CloseHandle(hFile);
}
else
{
MessageBox(hWnd,"There has been an error attempting \nto determine
the file size of the given \nfile.","File Size Determination
Error",MB_OK | MB_ICONEXCLAMATION);
}