Please help with List View..

Why do I get this Error.. when this code was copied from MSDN why do they write something that doesn't work?

||=== Build: Debug in ListView (compiler: GNU GCC Compiler) ===|
|In function 'BOOL InitListViewColumns(HWND)':|
|32|error: cannot convert 'WCHAR [256] {aka wchar_t [256]}' to 'LPSTR {aka char*}' in assignment|
|40|error: invalid conversion from 'int' to 'LPSTR {aka char*}' [-fpermissive]|
|3900|note: initializing argument 3 of 'int LoadStringA(HINSTANCE, UINT, LPSTR, int)'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#define _WIN32_IE 0x0300
#include <windows.h>
#include <commctrl.h>
#include "resources.h"

#define IDM_CODE_SAMPLES     1

HINSTANCE hInst;

HWND CreateListView(HWND hwnd)
{
    INITCOMMONCONTROLSEX icex;
    icex.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex);

    HWND hWndListView = CreateWindowW(WC_LISTVIEWW, L"", WS_CHILD | LVS_REPORT,
                                     5, 5, 150, 450, hwnd, (HMENU)IDM_CODE_SAMPLES, hInst, NULL);

    return(hWndListView);
}
BOOL InitListViewColumns(HWND hWndListView)
{
    WCHAR szText[256];
    LVCOLUMN lvc;
    int iCol;

    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

    for(iCol = 0; iCol < C_COLUMNS; iCol++)
    {
        lvc.iSubItem = iCol;
        lvc.pszText  = szText;
        lvc.cx       = 100;

        if(iCol < 2)
            lvc.fmt = LVCFMT_LEFT;
        else
            lvc.fmt = LVCFMT_RIGHT;

        LoadString(hInst, IDC_FIRSTCOLUMN, +iCol, sizeof(szText)/sizeof(szText[0]));

        if(ListView_InsertColumn(hWndListView, iCol, &lvc) == -1);
        return FALSE;
    }
    return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch(msg)
    {
    case WM_DESTROY:
            PostQuitMessage(0);
            break;
    default:
        return DefWindowProcW(hwnd, msg, wp, lp);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
    HWND hwnd;
    WNDCLASSW wc = {0};

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW + 1;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance     = hInst;
    wc.lpfnWndProc   = WndProc;
    wc.lpszClassName = L"myWindowClass";

    if(!RegisterClassW(&wc))
        return -1;

    hwnd = CreateWindowW(L"myWindowClass", L"List View", WS_OVERLAPPEDWINDOW,
                         350, 120, 700, 500, NULL, NULL, NULL, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    MSG msg = {0};

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}


Source: https://docs.microsoft.com/en-us/windows/win32/controls/add-list-view-columns
Last edited on
You're mixing narrow and wide strings. You should define UNICODE and probably _UNICODE too. The will make API calls like LoadString take wide string arguments rather than narrow.
> LoadString(hInst, IDC_FIRSTCOLUMN, +iCol, sizeof(szText)/sizeof(szText[0]));
How did you manage to screw up copy/paste?

1
2
3
4
5
        // Load the names of the column headings from the string resources.
        LoadString(g_hInst,
                   IDS_FIRSTCOLUMN + iCol,
                   szText,
                   sizeof(szText)/sizeof(szText[0]));

There is NO comma between IDS_FIRSTCOLUMN and iCol
Where's your szText ?

> if(ListView_InsertColumn(hWndListView, iCol, &lvc) == -1);
Watch that ; at the end.
Your code ALWAYS returns FALSE at the end of the first loop iteration.


Last edited on
The problem is the mix of 'ANSI' and 'UNICODE' version. You use CreateWindowW(....). Notice the W at the end of the function name. This tells that it is the 'UNICODE' version. Thus the error schould go away when you add the 'W' at the and where necessary. In your case:

LoadString(...) -> LoadStringW(...)
ohh... ok ok I got it :) Thank you so much..
Topic archived. No new replies allowed.