944,175 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3048
  • C++ RSS
Oct 6th, 2007
0

windows api help

Expand Post »
I'm working on a simple program. It will compile,but whenever I press the delete button a message pops up and says it needs to close. I looked at the error details and It's error code was 0xc0000005.
This is where I think the problem is:
C++ Syntax (Toggle Plain Text)
  1. for(i= 0; i < NumOfItems; i++)
  2. {
  3.  
  4.  
  5. ListView_GetItemText(hWndListView, i, 0, text, 256);
  6. ListView_GetItemText(hWndListView, i, 1, text2, 256);
  7.  
  8. string s = text;
  9. ListViewText[i][0] = s;
  10. string s2 = text2;
  11. ListViewText[i][1] = s2;
  12.  
  13.  
  14.  
  15. }
This is the whole code. I'm sorry for posting all of it, but the problem might be somewhere else.
C++ Syntax (Toggle Plain Text)
  1. #define _WIN32_IE 0x600
  2. #include "resource.h"
  3. #include <windows.h>
  4. #include <commctrl.h>
  5.  
  6. #include <string>
  7. #include <vector>
  8.  
  9. HWND hWndButton2;
  10. HWND hWndButton;
  11. HWND hWndButton3;
  12. HWND hWndEditBox;
  13. HWND hWndEditBox2;
  14. HFONT hFont;
  15. HWND hWnd;
  16. HWND hWndListView;
  17. LVCOLUMN LvCol; // Make Coluom struct for ListView
  18. LVITEM LvItem;
  19. int iSelect = 0;
  20. int i;
  21. int NumOfItems;
  22. /* Declare Windows procedure */
  23. LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
  24.  
  25. /* Make the class name into a global variable */
  26. char szClassName[] = "WindowsApp";
  27.  
  28. int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR
  29. lpszArgument, int nFunsterStil)
  30.  
  31. {
  32.  
  33.  
  34. MSG messages; /* Here messages to the application are saved */
  35. WNDCLASSEX wincl; /* Data structure for the windowclass */
  36.  
  37. /* The Window structure */
  38. wincl.hInstance = hThisInstance;
  39. wincl.lpszClassName = szClassName;
  40. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  41. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  42. wincl.cbSize = sizeof(WNDCLASSEX);
  43.  
  44. /* Use default icon and mouse-pointer */
  45. wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  46. wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  47. wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
  48. wincl.lpszMenuName = NULL; /* No menu */
  49. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  50. wincl.cbWndExtra = 0; /* structure or the window instance */
  51. /* Use Windows's default color as the background of the window */
  52. wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
  53.  
  54. /* Register the window class, and if it fails quit the program */
  55. if (!RegisterClassEx(&wincl))
  56. return 0;
  57.  
  58. /* The class is registered, let's create the program*/
  59. hWnd = CreateWindowEx(0, /* Extended possibilites for variation */
  60. szClassName, /* Classname */
  61. "To-do list", /* Title Text */
  62. WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
  63. CW_USEDEFAULT, /* Windows decides the position */
  64. CW_USEDEFAULT, /* where the window ends up on the screen */
  65. 347, /* The programs width */
  66. 266, /* and height in pixels */
  67. HWND_DESKTOP, /* The window is a child-window to desktop */
  68. NULL, /* No menu */
  69. hThisInstance, /* Program Instance handler */
  70. NULL /* No Window Creation data */
  71. );
  72.  
  73. /* Make the window visible on the screen */
  74. ShowWindow(hWnd, nFunsterStil);
  75.  
  76. /* Run the message loop. It will run until GetMessage() returns 0 */
  77. while (GetMessage(&messages, NULL, 0, 0))
  78. {
  79. /* Translate virtual-key messages into character messages */
  80. TranslateMessage(&messages);
  81. /* Send message to WindowProcedure */
  82. DispatchMessage(&messages);
  83. }
  84.  
  85. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  86. return messages.wParam;
  87. }
  88.  
  89.  
  90. /* This function is called by the Windows function DispatchMessage() */
  91.  
  92. LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM
  93. lParam)
  94. {
  95. switch (message) /* handle the messages */
  96. {
  97. case WM_CREATE:
  98. {
  99. //Add Button
  100. HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
  101. hWndButton = CreateWindowEx(
  102. 0,
  103. "BUTTON",
  104. "ADD",
  105. WS_VISIBLE | WS_CHILD,
  106. 250,
  107. 0,
  108. 90,
  109. 20,
  110. hWnd,
  111. (HMENU)IDB_BUTTON,
  112. hInstance,
  113. NULL);
  114. //font
  115. hFont = CreateFont(0, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET,
  116. OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN,
  117. "Courier New");
  118. //date box
  119. hWndEditBox = CreateWindow(
  120. "EDIT",
  121. "",
  122. WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
  123. 0,
  124. 0,
  125. 70,
  126. 20,
  127. hWnd,
  128. (HMENU)IDC_EDITBOX_TEXT,
  129. (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
  130. NULL);
  131. //item button
  132. hWndEditBox2 = CreateWindow("EDIT",
  133. "",
  134. WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
  135. 75,
  136. 0,
  137. 175,
  138. 20,
  139. hWnd,
  140. (HMENU)IDC_EDITBOX_TEXT2,
  141. (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
  142. NULL);
  143. SendMessage(hWndEditBox, WM_SETFONT, (WPARAM)hFont, TRUE);
  144. SendMessage(hWndEditBox2, WM_SETFONT, (WPARAM)hFont, TRUE);
  145. //listview
  146. hWndListView = CreateWindowEx(
  147. 0L,
  148. WC_LISTVIEW,
  149. "",
  150. WS_VISIBLE | WS_CHILD | LVS_REPORT,
  151. 0,
  152. 21,
  153. 340,
  154. 190,
  155. hWnd,
  156. (HMENU)ID_LISTVIEW,
  157. hInstance,
  158. NULL);
  159. ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT);
  160. //Delete button
  161. hWndButton2 = CreateWindowEx(
  162. 0,
  163. "BUTTON",
  164. "Delete",
  165. WS_VISIBLE | WS_CHILD,
  166. 0,
  167. 213,
  168. 90,
  169. 20,
  170. hWnd,
  171. (HMENU)IDB_BUTTON2, hInstance,
  172. NULL);
  173. //delete all btn
  174. hWndButton3 = CreateWindowEx(
  175. 0,
  176. "BUTTON",
  177. "Delete All",
  178. WS_VISIBLE | WS_CHILD,
  179. 91,
  180. 213,
  181. 90,
  182. 20,
  183. hWnd,
  184. (HMENU)IDB_BUTTON3,
  185. hInstance,
  186. NULL);
  187. // Here we put the info on the Coulom headers
  188. // this is not data, only name of each header we like
  189. memset(&LvCol, 0, sizeof(LvCol)); // Reset Coluom
  190. LvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; // Type of mask
  191. LvCol.cx = 0x0; // width between each coloum
  192. LvCol.pszText = "Date"; // First Header
  193. LvCol.cx = 0x40;
  194.  
  195.  
  196. // Inserting Couloms as much as we want
  197. SendMessage(hWndListView, LVM_INSERTCOLUMN, 0, (LPARAM) &LvCol);
  198. // Insert/Show the coloum
  199.  
  200. LvCol.cx = 0x110;
  201. LvCol.pszText = "Item";
  202. SendMessage(hWndListView, LVM_INSERTCOLUMN, 2, (LPARAM) &LvCol);
  203.  
  204.  
  205.  
  206.  
  207.  
  208. }
  209.  
  210.  
  211. break;
  212. case WM_DESTROY:
  213.  
  214. PostQuitMessage(0); /* send a WM_QUIT to the message queue */
  215. break;
  216. case WM_COMMAND:
  217. {
  218. switch (LOWORD(wParam))
  219. {
  220. case IDB_BUTTON:
  221. {
  222. switch (HIWORD(wParam))
  223. {
  224. case BN_CLICKED:
  225.  
  226. int TxtSize = GetWindowTextLength(hWndEditBox) + 1;
  227. char *Entry = new char[TxtSize];
  228. GetWindowText(hWndEditBox, Entry, TxtSize);
  229. int TxtSize2 = GetWindowTextLength(hWndEditBox2) + 1;
  230. char *Entry2 = new char[TxtSize2];
  231. GetWindowText(hWndEditBox2, Entry2, TxtSize2);
  232.  
  233.  
  234.  
  235.  
  236. LvItem.mask = LVIF_TEXT; // Text Style
  237. LvItem.cchTextMax = 256; // Max size of test
  238.  
  239. LvItem.iItem = 0; // choose item
  240. LvItem.iSubItem = 0; // Put in first coluom
  241. LvItem.pszText = Entry;
  242. // Text to display (can be from a char variable) (Items)
  243. SendMessage(hWndListView, LVM_INSERTITEM, 0, (LPARAM) &LvItem);
  244. // Send to the Listview
  245. // Insert subitem
  246. LvItem.iSubItem = 1;
  247. LvItem.pszText = Entry2;
  248. SendMessage(hWndListView, LVM_SETITEM, 0, (LPARAM) &LvItem);
  249. // Enter text to SubItems
  250. delete [] Entry;
  251. delete [] Entry2;
  252. NumOfItems++;
  253. break;
  254. }
  255. }
  256. break;
  257. case IDB_BUTTON2:
  258. {
  259. switch (HIWORD(wParam))
  260. {
  261. case BN_CLICKED:
  262. iSelect=SendMessage(hWndListView,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
  263. SendMessage(hWndListView, LVM_DELETEITEM, iSelect, 0);
  264. NumOfItems--;
  265. using namespace std;
  266.  
  267. vector<string>* ListViewText;
  268. ListViewText = new vector<string>();
  269. char *text = new char[256];
  270. char *text2 = new char[256];
  271. for(i= 0; i < NumOfItems; i++)
  272. {
  273.  
  274.  
  275. ListView_GetItemText(hWndListView, i, 0, text, 256);
  276. ListView_GetItemText(hWndListView, i, 1, text2, 256);
  277.  
  278. string s = text;
  279. ListViewText[i][0] = s;
  280. string s2 = text2;
  281. ListViewText[i][1] = s2;
  282.  
  283.  
  284.  
  285. }
  286.  
  287. delete ListViewText;
  288. delete [] text;
  289. delete [] text2;
  290.  
  291.  
  292. }
  293. break;
  294. }
  295. case IDB_BUTTON3:
  296. {
  297. switch (HIWORD(wParam))
  298. {
  299. case BN_CLICKED:
  300. SendMessage(hWndListView, LVM_DELETEALLITEMS, 0, 0);
  301.  
  302. }
  303. break;
  304. }
  305. }
  306. return 0;
  307. }
  308. break;
  309. default:
  310. /* for messages that we don't deal with */
  311. return DefWindowProc(hWnd, message, wParam, lParam);
  312.  
  313. }
  314.  
  315. return 0;
  316. }
Thanks for the help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
curt22 is offline Offline
56 posts
since Sep 2007
Oct 6th, 2007
0

Re: windows api help

Would you please also post resource.h so that I can compile it ?
Last edited by Ancient Dragon; Oct 6th, 2007 at 10:22 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 6th, 2007
0

Re: windows api help

I think your problem is the way you are using ListViewText array beginning at line 272. This is a better way to do it. Note there is NO dynamic allocation needed and only one character buffer. You are allowed (and encouraged) to reuse data whenever possible and wherever it makes sense.

Note that I have added a couple lines to resize the vector to the correct number of elements. That was one of the main reasons your code did not work -- you were setting strings to non-existant rows and columns. I have not tested this code, but I THINK it will work.

C++ Syntax (Toggle Plain Text)
  1. vector< vector<string> > ListViewText;
  2. ListViewText.resize(NumOfItems);
  3. char text char[256];
  4. for(i= 0; i < NumOfItems; i++)
  5. {
  6. ListViewText[i].resize(2);
  7. ListView_GetItemText(hWndListView, i, 0, text, 256);
  8. ListViewText[i][0] = text;
  9. ListView_GetItemText(hWndListView, i, 1, text, 256);
  10. ListViewText[i][1] = text;
  11. }

Another problem with the above is: so what? You are putting all the strings in a 2d vector of strings which are immediately destroyed because of where that vector is declared. What do you intend to do with it ?
Last edited by Ancient Dragon; Oct 6th, 2007 at 10:34 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: NEED help pls. about linked list
Next Thread in C++ Forum Timeline: Track of the cost and calories for a lunch.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC