04-07-2006 03:41 AM
04-07-2006 06:19 AM
04-07-2006 07:42 AM
04-07-2006 08:32 AM - edited 04-07-2006 08:32 AM
Well, SetEndOfFile does behave correctly on files opened with windows API functions. Look at this function that selects a file and truncates it to 256 bytes.
int CVICALLBACK TruncateFile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char file[MAX_PATHNAME_LEN], msg[512];
DWORD error = 0;
HANDLE fH = NULL;
if (event != EVENT_COMMIT) return 0;
if (FileSelectPopup ("", "*.*", "", "File to truncate",
VAL_SELECT_BUTTON, 0, 0, 1, 0, file) == VAL_NO_FILE_SELECTED)
return 0;
//HANDLE CreateFile (
// LPCTSTR lpFileName, // file name
// DWORD dwDesiredAccess, // access mode
// DWORD dwShareMode, // share mode
// LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
// DWORD dwCreationDisposition, // how to create
// DWORD dwFlagsAndAttributes, // file attributes
// HANDLE hTemplateFile // handle to template file
//);
fH = CreateFile (file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fH == INVALID_HANDLE_VALUE) {
//DWORD GetLastError (VOID);
error = GetLastError ();
goto Error;
}
//DWORD SetFilePointer (
// HANDLE hFile, // handle to file
// LONG lDistanceToMove, // bytes to move pointer
// PLONG lpDistanceToMoveHigh, // bytes to move pointer
// DWORD dwMoveMethod // starting point
//);
if (SetFilePointer (fH, 0xff, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
error = GetLastError ();
goto Error;
}
//BOOL SetEndOfFile (
// HANDLE hFile // handle to file
//);
if (!SetEndOfFile (fH)) {
error = GetLastError ();
}
Error:
if (fH != INVALID_HANDLE_VALUE) CloseHandle(fH);
if (error) {
memset (msg, 0, 512);
//DWORD FormatMessage(
// DWORD dwFlags, // source and processing options
// LPCVOID lpSource, // message source
// DWORD dwMessageId, // message identifier
// DWORD dwLanguageId, // language identifier
// LPTSTR lpBuffer, // message buffer
// DWORD nSize, // maximum size of message buffer
// va_list *Arguments // array of message inserts
//);
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, msg, 511, NULL);
if (!strlen (msg)) sprintf (msg, "Error %d", error);
MessagePopup ("Error", msg);
}
return 0;
}
Message Edited by Roberto Bozzolo on 04-07-2006 03:36 PM
04-07-2006 09:20 AM
04-07-2006 09:23 AM
04-07-2006 11:01 AM
04-07-2006 11:27 AM
04-30-2012 09:02 AM
I was quite happy at your post.
It works well.
I open the file with function OpenFile as a CVI function.
In order to use the CreateFile Function I needed to
set the parameter FILE SHARE_WRITE in the CreateFile function.
Then the file was created by a CVI function and closed and reopend
with the CreateFile function.
Thanks a lot for your post.
Yours sincerely
Othmar Widmer