Software Archive
Read-only legacy content
17061 Discussions

Snapshot file save error

Scott_W_1
Beginner
435 Views

I've created a tool to take a snapshot of a gesture in order to create libraries for gesture recognition. It records finger and joint data from a single frame into an edit box. The user then clicks "save snapshot" to add the snapshot to a library by appending the data to a text file. This might help other developers, but I'm not ready to release it with this annoying bug. It still works, but you have to restart the program after saving one or two gesture

It does so successfully, appending one or two poses, then it crashes. Any insight would be appreciated.

Here's my code:

using namespace std;
BOOL GetSaveSnapshot(HWND hDlg)
{

	TCHAR   szFile[MAX_PATH] = TEXT("\0");
	OPENFILENAME   ofn;
	HANDLE hFile = INVALID_HANDLE_VALUE;

	HWND hEdit = NULL;
	DWORD dwTextLen = 0, bytesWritten = 0;
	TCHAR *wszEditText = NULL;
	char *szEditText = NULL;



	hEdit = GetDlgItem(hDlg, IDC_GESTURELIBRARY);
	//get the text length of the edit controls contents
	dwTextLen = GetWindowTextLength(hEdit);
	wszEditText = (TCHAR*)malloc((dwTextLen + 1)*sizeof(TCHAR));
	memset(wszEditText, 0, (dwTextLen + 1)*sizeof(TCHAR));
	//read edit controls contents into buffer
	GetWindowText(hEdit, wszEditText, dwTextLen + 1);
	szEditText = (char*)malloc(dwTextLen + 1);
	//convert the wide char read from edit control to char
	//wcstombs(szEditText, wszEditText, dwTextLen);
	String fileName;
	fileName = (std::wstring)wszEditText;
	fileName += L".txt";
	

	memset(&(ofn), 0, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hDlg;
	ofn.lpstrFile = wszEditText;
	ofn.nMaxFile = MAX_PATH;
	//ofn.lpstrFilter = L"RSSDK clip (*.rssdk)\0*.rssdk\0All Files (*.*)\0*.*\0";
	ofn.lpstrFilter = TEXT("Text (*.txt)\0*.txt\0");
	//ofn.lpstrTitle = TEXT("Save File As");
	ofn.lpstrTitle = wszEditText;
	ofn.Flags = OFN_HIDEREADONLY;
	ofn.lpstrDefExt = TEXT("txt");
	//get the filename the user wants to save to
	ofn.lpstrFileTitle = wszEditText;
	if (GetSaveFileName(&ofn))
	{


		//ofn.lpstrFile contains the full path of the file, get a handle to it
		
		if (GetLibraryState(hDlg)) {

			hFile = CreateFile(ofn.lpstrFile, FILE_APPEND_DATA, FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
			SetFilePointer(hFile, 0, 0, FILE_END);	
		}
		else{
			hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
			
		}
		if (hFile == INVALID_HANDLE_VALUE)
			return FALSE;

		hEdit = GetDlgItem(hDlg, IDC_MOCAPDATA);	
		//get the text length of the edit controls contents
		dwTextLen = GetWindowTextLength(hEdit);
		wszEditText = (TCHAR*)malloc((dwTextLen + 1)*sizeof(TCHAR));
		memset(wszEditText, 0, (dwTextLen + 1)*sizeof(TCHAR));
		//read edit controls contents into buffer
		GetWindowText(hEdit, wszEditText, dwTextLen + 1);
		szEditText = (char*)malloc(dwTextLen + 1);
		//convert the wide char read from edit control to char
		wcstombs(szEditText, wszEditText, dwTextLen);
		//save the contents into file
		if (WriteFile(hFile, szEditText, dwTextLen, &bytesWritten, NULL))

		//free resources
		free(wszEditText);
		free(szEditText);
		CloseHandle(hFile);
	}
	return TRUE;
}

 

0 Kudos
0 Replies
Reply