| | |
Save function not working
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 104
Reputation:
Solved Threads: 1
I was making a function to save info and it doesn't work. It's
actually made up of 2 functions. I've eliminated my problems down to
the second function and can't figure out what to do. Here is the
function:
Here are the errors I get:
error C2275: 'HWND' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'hEdit'
error C2065: 'hEdit' : undeclared identifier
error C2065: 'hEdit' : undeclared identifier
actually made up of 2 functions. I've eliminated my problems down to
the second function and can't figure out what to do. Here is the
function:
C++ Syntax (Toggle Plain Text)
void DoFileSave(HWND hwnd) { OPENFILENAME ofn; char szFileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.* \0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "txt"; ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; if(GetSaveFileName(&ofn)) { int i, a; char etxt[100]; i = SendMessage(IDC_LIST, LB_GETCOUNT, 0, 0); //gets the number of items in listbox for (a=0; a<i; a++) { SendMessage(IDC_LIST, LB_GETTEXT, a, etxt[a]); //get all text } HWND hEdit = GetDlgItem(hwnd, etxt[a]); SaveTextFileFromListBox(hEdit, szFileName); } }
error C2275: 'HWND' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'hEdit'
error C2065: 'hEdit' : undeclared identifier
error C2065: 'hEdit' : undeclared identifier
Last edited by Ancient Dragon; Jul 5th, 2009 at 3:19 pm. Reason: add code tags
•
•
Join Date: Jun 2009
Posts: 104
Reputation:
Solved Threads: 1
Ok this is the smallest I could make.
Here is the header file:
Here is the resource file:
And here's the CPP file:
Sorry if it's still a little lengthy.
Here is the header file:
C++ Syntax (Toggle Plain Text)
//{{NO_DEPENDENCIES}} // Microsoft Developer Studio generated include file. // Used by ctl_one.rc // #define IDD_MAIN 101 #define IDC_TEXT 1000 #define IDC_OTEXT 1001 #define IDC_LIST 1002 #define IDC_OLIST 1011 #define IDC_ADD 1003 #define IDC_SHOWCOUNT 1006 #define IDC_SAVE 1008 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1007 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif
Here is the resource file:
C++ Syntax (Toggle Plain Text)
//Microsoft Developer Studio generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #ifndef __BORLANDC__ #include "winres.h" #endif ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // English (U.S.) resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) #ifdef _WIN32 LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US #pragma code_page(1252) #endif //_WIN32 #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // 1 TEXTINCLUDE DISCARDABLE BEGIN "resource.h\0" END 2 TEXTINCLUDE DISCARDABLE BEGIN "#ifndef __BORLANDC__\r\n" "#include ""winres.h""\r\n" "#endif\r\n" "\0" END 3 TEXTINCLUDE DISCARDABLE BEGIN "\0" END #endif // APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Dialog // IDD_MAIN DIALOG DISCARDABLE 0, 0, 300, 302 STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Spanish Word Editor" FONT 8, "MS Sans Serif" BEGIN LTEXT "Add Spanish",IDC_STATIC,7,10,56,8 EDITTEXT IDC_TEXT,50,7,60,14,ES_AUTOHSCROLL LTEXT "Add English",IDC_STATIC,112,10,56,8 EDITTEXT IDC_OTEXT,151,7,60,14,ES_AUTOHSCROLL LISTBOX IDC_LIST,7,25,103,106,LBS_NOINTEGRALHEIGHT | LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP LISTBOX IDC_OLIST,112,25,103,106,LBS_NOINTEGRALHEIGHT | LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP PUSHBUTTON "&Add",IDC_ADD,220,30,50,14 PUSHBUTTON "&Save",IDC_SAVE,220,80,50,14 END ///////////////////////////////////////////////////////////////////////////// // // DESIGNINFO // #ifdef APSTUDIO_INVOKED GUIDELINES DESIGNINFO DISCARDABLE BEGIN IDD_MAIN, DIALOG BEGIN LEFTMARGIN, 7 RIGHTMARGIN, 200 VERTGUIDE, 145 VERTGUIDE, 150 TOPMARGIN, 7 BOTTOMMARGIN, 149 END END #endif // APSTUDIO_INVOKED #endif // English (U.S.) resources /////////////////////////////////////////////////////////////////////////////
And here's the CPP file:
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include "resource.h" BOOL SaveTextFileFromListBox(HWND hEdit, LPCTSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile != INVALID_HANDLE_VALUE) { DWORD dwTextLength; dwTextLength = GetWindowTextLength(hEdit); // No need to bother if there's no text. if(dwTextLength > 0) { LPSTR pszText; DWORD dwBufferSize = dwTextLength + 1; pszText = GlobalAlloc(GPTR, dwBufferSize); if(pszText != NULL) { if(GetWindowText(hEdit,pszText, dwBufferSize)) { DWORD dwWritten; if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE; } GlobalFree(pszText); } } CloseHandle(hFile); } return bSuccess; } void DoFileSave(HWND hwnd) { OPENFILENAME ofn; char szFileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = "txt"; ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; if(GetSaveFileName(&ofn)) { int i, a; char etxt[100]; i = SendMessage(IDC_LIST, LB_GETCOUNT, 0, 0); //gets the number of items in listbox for (a=0; a<i; a++) { SendMessage(IDC_LIST, LB_GETTEXT, a, etxt[a]); //get all text } HWND hEdit = GetDlgItem(hwnd, etxt[a]); SaveTextFileFromListBox(hEdit, szFileName); } } BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_ADD: { int nTimes = 1; // Then we get the string they entered // First we need to find out how long it is so that we can // allocate some memory int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT)); int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_OTEXT)); if(len > 0 && len2 > 0) { // Now we allocate, and get the string into our buffer int i; char* buf; buf = (char*)GlobalAlloc(GPTR, len + 1); GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1); // Now we add the string to the list box however many times // the user asked us to. for(i = 0;i < nTimes; i++) { int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf); // Here we are associating the value nTimes with the item // just for the heck of it, we'll use it to display later. // Normally you would put some more useful data here, such // as a pointer. SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes); } // Dont' forget to free the memory! GlobalFree((HANDLE)buf); buf = (char*)GlobalAlloc(GPTR, len2 + 1); GetDlgItemText(hwnd, IDC_OTEXT, buf, len2 + 1); // Now we add the string to the list box however many times // the user asked us to. for(i = 0;i < nTimes; i++) { int index = SendDlgItemMessage(hwnd, IDC_OLIST, LB_ADDSTRING, 0, (LPARAM)buf); // Here we are associating the value nTimes with the item // just for the heck of it, we'll use it to display later. // Normally you would put some more useful data here, such // as a pointer. SendDlgItemMessage(hwnd, IDC_OLIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes); } // Dont' forget to free the memory! GlobalFree((HANDLE)buf); } else { MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK); } } break; break; case IDC_SAVE: DoFileSave(hwnd); break; } break; case WM_CLOSE: EndDialog(hwnd, 0); break; default: return FALSE; } return TRUE; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc); }
I compiled the *.cpp file with VC++ 2008 Express and got some errors
As for the first error: GlobalAlloc() returns a HGLOBAL object, not a char*. And GlobalAlloc() is just an unnecessary complication, just use standard c++ dynamic memory allocation, such as new and delete[].
The first argument to SendMessage() must be a window handle HWIND, not a macro from the resource.h file.
•
•
•
•
1>c:\dvlp\wintest\wintest\wintest.cpp(23) : error C2440: '=' : cannot convert from 'HGLOBAL' to 'LPSTR'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\dvlp\wintest\wintest\wintest.cpp(61) : error C2664: 'SendMessageA' : cannot convert parameter 1 from 'int' to 'HWND'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\dvlp\wintest\wintest\wintest.cpp(64) : error C2664: 'SendMessageA' : cannot convert parameter 1 from 'int' to 'HWND'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\dvlp\wintest\wintest\Debug\BuildLog.htm"
1>wintest - 3 error(s), 0 warning(s)
The first argument to SendMessage() must be a window handle HWIND, not a macro from the resource.h file.
Last edited by Ancient Dragon; Jul 5th, 2009 at 3:36 pm.
http://www.cplusplus.com/doc/tutorial/dynamic/
cpp Syntax (Toggle Plain Text)
type *var = new type[size]; if(!var) return(false); /*or somesort of error handling*/ delete [] var;
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
you are kidding aren't you? You mean you don't know diddley-squat about c++ yet you are attempting to write a windows program
C++ Syntax (Toggle Plain Text)
if(hFile != INVALID_HANDLE_VALUE) { DWORD dwTextLength; dwTextLength = GetWindowTextLength(hEdit); // No need to bother if there's no text. if(dwTextLength > 0) { char* pszText; DWORD dwBufferSize = dwTextLength + 1; try { pszText = new char[dwBufferSize]; if(GetWindowText(hEdit,pszText, dwBufferSize)) { DWORD dwWritten; if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE; } delete[] pszText; } catch(...) { // allocation failed } }
Last edited by Ancient Dragon; Jul 5th, 2009 at 3:52 pm.
![]() |
Similar Threads
- mail function is not working properly??? (PHP)
- xmldoc.load function not working (JSP)
- generating random id and save it into db (PHP)
- Recursive function not fully working (C++)
- help with save and continue function (C++)
- I need help with a save function in my game. (C++)
- function save () (JavaScript / DHTML / AJAX)
- print function not working (C++)
- "Save Target As.." isn't working in IE6 (Web Browsers)
- my php session_destroy() function not working (PHP)
Other Threads in the C++ Forum
- Previous Thread: Help with a vector<vector<string> *>
- Next Thread: Value-Returning Functions Help
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






