Use of linked lists in Win32 (winapi) C ++

1

Good morning, I would like to know how to use double linked lists in Winapi, more specifically, where to initialize the list? and within the CALLBACK, how to use the list? (or simply by declaring it in the right place can it be used without problems?) I currently use Visual Studio 2015.

In MsgDlgProc there are only buttons to go to the other windows.

In MsgDlg2Proc, there is 3 Listbox to show the data of the list and a button to return to the window 1. To show the information I intend to use if I can get direct "name" from the linked list

SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, NULL, (LPARAM)nombre);

In MsgDlg3Proc, there are 2 EditControl to receive the data and a button to save them in the list and empty the Editbox and a button to cancel

Here the code:

HWND hDlg;
HINSTANCE ghDlg;

struct registro {
    registro *sig;
    int folio; //char en statictext ---> int en lista
    char nombre[70];
    int edad;//char en editcontrol ---> int en lista
    registro *ant;
};

BOOL CALLBACK MsgDlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK MsgDlg2Proc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK MsgDlg3Proc(HWND, UINT, WPARAM, LPARAM);

BOOL CALLBACK MsgDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_INITDIALOG:
            return true;

        case WM_COMMAND: {
            switch (LOWORD(wParam)) {

                case IDC_BUTTON1: {
                    ShowWindow(hDlg, SW_HIDE);
                    DialogBox(ghDlg, MAKEINTRESOURCE(IDD_DIALOG3), 0, MsgDlg3Proc);
                }
                break;

                case IDC_BUTTON2: {
                    ShowWindow(hDlg, SW_HIDE);
                    DialogBox(ghDlg, MAKEINTRESOURCE(IDD_DIALOG2), 0, MsgDlg2Proc);
                }
                break;

            }
            return false;
        }
        break;

        case WM_CLOSE:
            DestroyWindow(hDlg);
            return true;

        case WM_DESTROY:
            PostQuitMessage(0);
            return true;
    }
    return false;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd) {
    hDlg = CreateDialog(ghDlg, MAKEINTRESOURCE(IDD_DIALOG1), 0, MsgDlgProc);
    ShowWindow(hDlg, showCmd);
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));
    while (GetMessage(&msg, 0, 0, 0)) {
        if (hDlg == 0 || !IsDialogMessage(hDlg, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int)msg.wParam;
}

BOOL CALLBACK MsgDlg2Proc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_INITDIALOG:
            return true;

        case WM_COMMAND: {
            switch (LOWORD(wParam)) {

                case IDC_BUTTON1: {
                    ShowWindow(hDlg, SW_HIDE);
                    DialogBox(ghDlg, MAKEINTRESOURCE(IDD_DIALOG1), 0, MsgDlgProc);
                }
                break;
            }

            return false;
        }
        break;

        case WM_CLOSE:

        DestroyWindow(hDlg);
            return true;

        case WM_DESTROY:
            PostQuitMessage(0);
            return true;
    }

    return false;
}

BOOL CALLBACK MsgDlg3Proc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        case WM_INITDIALOG:
            return true;

        case WM_COMMAND: {
            switch (LOWORD(wParam)) {

                case IDC_BUTTON1: {
                }
                break;

                case IDC_BUTTON2: {
                    ShowWindow(hDlg, SW_HIDE);
                    DialogBox(ghDlg, MAKEINTRESOURCE(IDD_DIALOG1), 0, MsgDlgProc);
                }
                break;
            }
            return false;
        }
        break;

        case WM_CLOSE:
            DestroyWindow(hDlg);
            return true;

        case WM_DESTROY:
            PostQuitMessage(0);
            return true;
    }

    return false;
}

Thanks

    
asked by Dan Silva 07.06.2016 в 04:25
source

2 answers

1

Lists, regardless of their nature, serve only for your own purposes.

The Windows API serves a variety of functions to interact with the operating system. This interface usually works with handlers, a kind of identifier that allows access to different operating system resources.

In short, the linked lists will only exist in your application. The operating system has no need to ask or provide linked lists. If the SO needs to make use of this type of structure, it will implement and manage it internally and provide you with a more or less generic interface that will not expose these structures. Why? Basically because of the risks that it entails.

Another reason is that a linked list is something very abstract:

  • What's in the linked list? An integer, a structure, operating system resources, ... You can not create a generic list that deals with all possible cases in C (WinAPI interface)
  • Is the list linked simple, double? Is it flat or circular? Too many possible causes
  • Who and how do you manage the memory associated with that list? If it is the application then it does not make sense for the OS to present an interface for its management. If the OS does, the application loses power over that memory.
  • Which implementation is the most appropriate? The best management algorithm will depend on the use of the list. It is easy to understand that there can be as many managements as installed applications, then giving a generic version is not the solution and giving a specific one can complicate the interface too much.

What you have to do is see how to structure your application so that all those sections of it that need to use a linked list can access the interface that allows working with them. This interface can either create it from scratch or import it from a library that incorporates this feature ... but of course it will not be the responsibility of the OS or its management or maintenance.

The OS API will have to be used when you really want to communicate with the OS.

Greetings.

    
answered by 07.06.2016 в 08:59
0
  

Good morning, I would like to know how to use double linked lists in Winapi

As eferion 1 already indicates, the use of a doubly linked list is independent to the use of WinAPI. In other words: the use of the tool is independent of the task. To put an allegory: it is the same to use a screwdriver to change a plug that to disassemble an Ikea® furniture, the screwdriver still serves for the same.

On the other hand, you are apparently reinventing the wheel by creating your own list with the structure struct registro (which I see in your code you are not even using); C ++ saves you the job of developing your own double-linked list by offering you in the standard template library (stl) an implementation: std::list (the highlight is mine):

  

std::list is a container that supports fast insertion and deletion of elements from any part of the container. It is not fast in random access. It is implemented as a double linked list .

Let's continue ...

  

more specifically, where to initialize the list? and within the CALLBACK, how to use the list? (or simply by declaring it in the right place can it be used without problems?) I currently use Visual Studio 2015.

Without knowing exactly what you are looking for, you could declare the list as global in the same scope as hDlg or ghDlg , which would allow you to access the list from anywhere in the same file:

HWND hDlg;
HINSTANCE ghDlg;
struct registro {
    int folio = 0; //char en statictext ---> int en lista
    std::string nombre = "";
    int edad = 0;//char en editcontrol ---> int en lista
};
std::list<registro> lista;

When you need to save data in the list you should do something like this:

// Leer datos
int folio = LeerFolio();
std::string nombre = LeerNombre();
int edad = LeerEdad();

// Guardar datos
lista.push_back({folio, nombre, edad});

When you need to read data from the list, you should do something like this:

// Por cada elemento contenido en la lista...
for (const auto &r: lista)
{
    // Mostramos el folio del elemento.
    std::cout << "Folio: " << r.folio << '\n';
    // Mostramos el nombre del elemento.
    std::cout << "Nombre: " << r.nombre << '\n';
    // Mostramos la edad del elemento.
    std::cout << "Edad: " << r.edad << '\n';
}

1 For a change, I get ahead of myself when I answer, I'm early riser than me or write faster.

    
answered by 07.06.2016 в 09:27