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:

for(i= 0; i < NumOfItems; i++)
            {


              ListView_GetItemText(hWndListView, i, 0, text, 256);
              ListView_GetItemText(hWndListView, i, 1, text2, 256);

            string s = text;
            ListViewText[i][0] = s;
            string s2 = text2;
            ListViewText[i][1] = s2;



            }

This is the whole code. I'm sorry for posting all of it, but the problem might be somewhere else.

#define _WIN32_IE 0x600
#include "resource.h"
#include <windows.h>
#include <commctrl.h>

#include <string>
#include <vector>

HWND hWndButton2;
HWND hWndButton;
HWND hWndButton3;
HWND hWndEditBox;
HWND hWndEditBox2;
HFONT hFont;
HWND hWnd;
HWND hWndListView;
LVCOLUMN LvCol; // Make Coluom struct for ListView
LVITEM LvItem;
int iSelect = 0;
int i;
int NumOfItems;
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[] = "WindowsApp";

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR
  lpszArgument, int nFunsterStil)

{


  MSG messages; /* Here messages to the application are saved */
  WNDCLASSEX wincl; /* Data structure for the windowclass */

  /* The Window structure */
  wincl.hInstance = hThisInstance;
  wincl.lpszClassName = szClassName;
  wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  wincl.cbSize = sizeof(WNDCLASSEX);

  /* Use default icon and mouse-pointer */
  wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
  wincl.lpszMenuName = NULL; /* No menu */
  wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  wincl.cbWndExtra = 0; /* structure or the window instance */
  /* Use Windows's default color as the background of the window */
  wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND;

  /* Register the window class, and if it fails quit the program */
  if (!RegisterClassEx(&wincl))
    return 0;

  /* The class is registered, let's create the program*/
  hWnd = CreateWindowEx(0,  /* Extended possibilites for variation */
  szClassName,  /* Classname */
  "To-do list",  /* Title Text */
  WS_SYSMENU | WS_MINIMIZEBOX,  /* default window */
  CW_USEDEFAULT,  /* Windows decides the position */
  CW_USEDEFAULT,  /* where the window ends up on the screen */
  347,  /* The programs width */
  266,  /* and height in pixels */
  HWND_DESKTOP,  /* The window is a child-window to desktop */
  NULL,  /* No menu */
  hThisInstance,  /* Program Instance handler */
  NULL /* No Window Creation data */
  );

  /* Make the window visible on the screen */
  ShowWindow(hWnd, nFunsterStil);

  /* Run the message loop. It will run until GetMessage() returns 0 */
  while (GetMessage(&messages, NULL, 0, 0))
  {
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
  }

  /* The program return-value is 0 - The value that PostQuitMessage() gave */
  return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM
  lParam)
{
  switch (message) /* handle the messages */
  {
    case WM_CREATE:
      {
        //Add Button
        HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
        hWndButton = CreateWindowEx(
        0,
        "BUTTON",
        "ADD",
        WS_VISIBLE | WS_CHILD,
        250,
        0,
        90,
        20,
        hWnd,
        (HMENU)IDB_BUTTON,
        hInstance,
        NULL);
        //font
        hFont = CreateFont(0, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET,
          OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN,
          "Courier New");
        //date box
        hWndEditBox = CreateWindow(
        "EDIT",
        "",
        WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
        0,
        0,
        70,
        20,
        hWnd,
        (HMENU)IDC_EDITBOX_TEXT,
        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
        NULL);
        //item button
        hWndEditBox2 = CreateWindow("EDIT",
        "",
        WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
        75,
        0,
        175,
        20,
        hWnd,
        (HMENU)IDC_EDITBOX_TEXT2,
        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
        NULL);
        SendMessage(hWndEditBox, WM_SETFONT, (WPARAM)hFont, TRUE);
        SendMessage(hWndEditBox2, WM_SETFONT, (WPARAM)hFont, TRUE);
        //listview
        hWndListView = CreateWindowEx(
        0L,
        WC_LISTVIEW,
        "",
        WS_VISIBLE | WS_CHILD | LVS_REPORT,
        0,
        21,
        340,
        190,
        hWnd,
        (HMENU)ID_LISTVIEW,
        hInstance,
        NULL);
        ListView_SetExtendedListViewStyle(hWndListView, LVS_EX_FULLROWSELECT);
        //Delete button
        hWndButton2 = CreateWindowEx(
        0,
        "BUTTON",
        "Delete",
        WS_VISIBLE | WS_CHILD,
        0,
        213,
        90,
        20,
        hWnd,
        (HMENU)IDB_BUTTON2, hInstance,
        NULL);
        //delete all btn
        hWndButton3 = CreateWindowEx(
        0,
        "BUTTON",
        "Delete All",
        WS_VISIBLE | WS_CHILD,
        91,
        213,
        90,
        20,
        hWnd,
        (HMENU)IDB_BUTTON3,
        hInstance,
        NULL);
        // Here we put the info on the Coulom headers
        // this is not data, only name of each header we like
        memset(&LvCol, 0, sizeof(LvCol)); // Reset Coluom
        LvCol.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; // Type of mask
        LvCol.cx = 0x0; // width between each coloum
        LvCol.pszText = "Date"; // First Header
        LvCol.cx = 0x40;


        // Inserting Couloms as much as we want
        SendMessage(hWndListView, LVM_INSERTCOLUMN, 0, (LPARAM) &LvCol);
          // Insert/Show the coloum

        LvCol.cx = 0x110;
        LvCol.pszText = "Item";
        SendMessage(hWndListView, LVM_INSERTCOLUMN, 2, (LPARAM) &LvCol);





      }


      break;
    case WM_DESTROY:

      PostQuitMessage(0); /* send a WM_QUIT to the message queue */
      break;
    case WM_COMMAND:
      {
        switch (LOWORD(wParam))
        {
        case IDB_BUTTON:
          {
            switch (HIWORD(wParam))
            {
            case BN_CLICKED:

              int TxtSize = GetWindowTextLength(hWndEditBox) + 1;
              char *Entry = new char[TxtSize];
              GetWindowText(hWndEditBox, Entry, TxtSize);
              int TxtSize2 = GetWindowTextLength(hWndEditBox2) + 1;
              char *Entry2 = new char[TxtSize2];
              GetWindowText(hWndEditBox2, Entry2, TxtSize2);




              LvItem.mask = LVIF_TEXT; // Text Style
              LvItem.cchTextMax = 256; // Max size of test

              LvItem.iItem = 0; // choose item
              LvItem.iSubItem = 0; // Put in first coluom
              LvItem.pszText = Entry;
                // Text to display (can be from a char variable) (Items)
              SendMessage(hWndListView, LVM_INSERTITEM, 0, (LPARAM) &LvItem);
                // Send to the Listview
              // Insert subitem
              LvItem.iSubItem = 1;
              LvItem.pszText = Entry2;
              SendMessage(hWndListView, LVM_SETITEM, 0, (LPARAM) &LvItem);
                // Enter text to SubItems
delete [] Entry;
delete [] Entry2;
NumOfItems++;
              break;
            }
          }
          break;
        case IDB_BUTTON2:
          {
            switch (HIWORD(wParam))
            {
            case BN_CLICKED:
            iSelect=SendMessage(hWndListView,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
            SendMessage(hWndListView, LVM_DELETEITEM, iSelect, 0);
            NumOfItems--;
            using namespace std;

            vector<string>* ListViewText;
            ListViewText = new vector<string>();
            char *text = new char[256];
            char *text2 = new char[256];
            for(i= 0; i < NumOfItems; i++)
            {


              ListView_GetItemText(hWndListView, i, 0, text, 256);
              ListView_GetItemText(hWndListView, i, 1, text2, 256);

            string s = text;
            ListViewText[i][0] = s;
            string s2 = text2;
            ListViewText[i][1] = s2;



            }

            delete ListViewText;
            delete [] text;
            delete [] text2;


            }
            break;
          }
        case IDB_BUTTON3:
          {
            switch (HIWORD(wParam))
            {
            case BN_CLICKED:
              SendMessage(hWndListView, LVM_DELETEALLITEMS, 0, 0);

            }
            break;
          }
        }
        return 0;
      }
      break;
    default:
       /* for messages that we don't deal with */
      return DefWindowProc(hWnd, message, wParam, lParam);

  }

  return 0;
}

Thanks for the 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.

vector< vector<string> > ListViewText;
            ListViewText.resize(NumOfItems);
            char text char[256];
            for(i= 0; i < NumOfItems; i++)
            {
                ListViewText[i].resize(2);
                ListView_GetItemText(hWndListView, i, 0, text, 256);
                ListViewText[i][0] = text;
                ListView_GetItemText(hWndListView, i, 1, text, 256);
                ListViewText[i][1] = text;
            }

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 ?

This article has been dead for over six months. Start a new discussion instead.