I am trying to create a ListView
using the WinApi that is filled with strings. The problem is that there is a "Access Violation Reading Location" on the line that calls the ListView_InsertColumn
function. Here is the code that I am using to create the control.
HWND projectsListViewHandle;
projectsListViewHandle = CreateWindow(WC_LISTVIEW, L"", WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_EDITLABELS, 0, 0, 0, 0, hWnd, (HMENU)PROJECT_LIST_VIEW, pApp->hInstance, NULL);
std::vector<std::string> strings = {"text", "more text", "still text"};
…
LVCOLUMN lvC;
lvC.mask = LVCF_WIDTH | LVCF_TEXT;
lvC.fmt = LVCFMT_LEFT;
lvC.cx = 300;// w * .5;
if (ListView_InsertColumn(projectsListViewHandle, 0, &lvC) == -1)
return FALSE;
LVITEM lvI;
//initialize lvi memebers
lvI.mask = LVIF_TEXT | LVIF_STATE;
lvI.stateMask = 0;
lvI.iSubItem = 0;
lvI.state = 0;
for (int i = 0; i < strings.size(); i++)
{
const char* inString = projectsVector[i].c_str();
size_t size = strlen(inString) + 1;
wchar_t* outString = new wchar_t[size];
size_t outSize;
mbstowcs_s(&outSize, outString, size, inString, size - 1);
LPWSTR ptr = outString;
lvI.pszText = outString;
delete[] outString;
//insert items into the list
if (ListView_InsertItem(projectsListViewHandle, &lvI) == -1)
return FALSE;
}
return TRUE;
I first thought that the problem could be with calling delete[]
on the string pointer too soon; however, when I tried just not deleting itat all, I still got the same result.
Source: Windows Questions C++