| | |
Having problems with my word editor pprogram in API
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 104
Reputation:
Solved Threads: 1
I was going through a tutorial and had some trouble with an example program. Here's the coding I have and I can't fix it.
Here is the main cpp file.
Here is my header file.
And here is the resource file.
I keep getting this error:
1>c:\users\chris\desktop\word editor.cpp(85) : error C2440: 'initializing' : cannot convert from 'HGLOBAL' to 'int *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Here is the main cpp file.
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include "resource.h" BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_INITDIALOG: // This is where we set up the dialog box, and initialise any default values SetDlgItemText(hwnd, IDC_TEXT, "This is a string"); SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE); break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_ADD: { // When somebody clicks the Add button, first we get the number of // they entered BOOL bSuccess; int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE); if(bSuccess) { // 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)); if(len > 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); } else { MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK); } } else { MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK); } } break; case IDC_REMOVE: { // When the user clicks the Remove button, we first get the number // of selected items HWND hList = GetDlgItem(hwnd, IDC_LIST); int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0); if(count != LB_ERR) { if(count != 0) { // And then allocate room to store the list of selected items. int i; int *buf = GlobalAlloc(GPTR, sizeof(int) * count); SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf); // Now we loop through the list and remove each item that was // selected. // WARNING!!! // We loop backwards, because if we removed items // from top to bottom, it would change the indexes of the other // items!!! for(i = count - 1; i >= 0; i--) { SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0); } GlobalFree(buf); } else { MessageBox(hwnd, "No items selected.", "Warning", MB_OK); } } else { MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK); } } break; case IDC_CLEAR: SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0); break; case IDC_LIST: switch(HIWORD(wParam)) { case LBN_SELCHANGE: { // Get the number of items selected. HWND hList = GetDlgItem(hwnd, IDC_LIST); int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0); if(count != LB_ERR) { // We only want to continue if one and only one item is // selected. if(count == 1) { // Since we know ahead of time we're only getting one // index, there's no need to allocate an array. int index; int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index); if(err != LB_ERR) { // Get the data we associated with the item above // (the number of times it was added) int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0); SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE); } else { MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK); } } else { // No items selected, or more than one // Either way, we aren't going to process this. SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-"); } } else { MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK); } } break; } 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); }
Here is my 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_NUMBER 1001 #define IDC_LIST 1002 #define IDC_ADD 1003 #define IDC_CLEAR 1004 #define IDC_REMOVE 1005 #define IDC_SHOWCOUNT 1006 // 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
And 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, 207, 156 STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Controls One" FONT 8, "MS Sans Serif" BEGIN LTEXT "Add",IDC_STATIC,7,10,14,8 EDITTEXT IDC_TEXT,25,7,120,14,ES_AUTOHSCROLL EDITTEXT IDC_NUMBER,150,7,21,14,ES_NUMBER LTEXT "times.",IDC_STATIC,177,10,23,8 LISTBOX IDC_LIST,7,25,138,106,LBS_NOINTEGRALHEIGHT | LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP PUSHBUTTON "&Add",IDC_ADD,150,30,50,14 PUSHBUTTON "&Remove",IDC_REMOVE,150,47,50,14 PUSHBUTTON "&Clear",IDC_CLEAR,150,63,50,14 LTEXT "This item was added",IDC_STATIC,7,141,66,8 CTEXT "-",IDC_SHOWCOUNT,77,141,32,8 LTEXT "times",IDC_STATIC,114,141,17,8 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 /////////////////////////////////////////////////////////////////////////////
I keep getting this error:
1>c:\users\chris\desktop\word editor.cpp(85) : error C2440: 'initializing' : cannot convert from 'HGLOBAL' to 'int *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
Have a look at this point..
c++ Syntax (Toggle Plain Text)
//... int *buf = (int*)GlobalAlloc(GPTR,Len); //...
![]() |
Similar Threads
- Problems with Office 2008 for mac (OS X)
- Please Help Me (again:)) Microsoft Word Problems (Viruses, Spyware and other Nasties)
- Unresponsive/very slow Office 2003 programs on Vista 64bit (Windows Software)
- Office word editor (ASP.NET)
- problem with Sendkeys (Visual Basic 4 / 5 / 6)
- How to detect when Word has closed (Visual Basic 4 / 5 / 6)
- Problems with Word 2000 (Windows 95 / 98 / Me)
- problems with WORD documents (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Help with compiler error for self-teacher
- Next Thread: Class problem
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





