Thread: [c+Win32] Windows heaps

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    [c+Win32] Windows heaps

    hello guys,

    i don't know whats wrong here, when i tried to make heap allocation for my program using HeapCreate(), it compiles but always terminates.

    The example i studied used GetProcessHeap.

    this is my code:

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    void CreateMenuBar(HWND);
    void OpenDialog(HWND);
    void LoadFile(LPSTR);
    
    
    #define IDM_FILE_NEW 1
    HWND ghwndEdit;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
        MSG msg;
        WNDCLASS wc = {0};
        wc.lpszClassName = TEXT("OpenDialog");
        wc.hInstance = hInstance;
        wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.lpfnWndProc = WndProc;
        wc.hCursor = LoadCursor(0, IDC_ARROW);
    
        RegisterClass(&wc);
        CreateWindow(wc.lpszClassName, TEXT("OpenDialog"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 150, 150, 265, 200, 0, 0, hInstance, 0);
    
        while(GetMessage(&msg, NULL, 0, 0)){
            DispatchMessage(&msg);
        }
        
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
        switch(msg){
            case WM_CREATE:
                ghwndEdit = CreateWindowEx(WS_EX_RIGHTSCROLLBAR, TEXT("edit"), NULL, WS_VISIBLE | WS_CHILD | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE, 0, 0, 260, 180, hwnd, (HMENU)1, NULL, NULL);
                CreateMenuBar(hwnd);
                break;
            case WM_SIZE:
                SetWindowPos(ghwndEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOMOVE | SWP_NOZORDER);
                break;
            case WM_COMMAND:
                if(wParam == IDM_FILE_NEW)
                    OpenDialog(hwnd);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
        }
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    void CreateMenuBar(HWND hwnd){
        HMENU hMenuBar;
        HMENU hMenu;
    
        hMenuBar = CreateMenu();
        hMenu = CreateMenu();
    
        AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hMenu, TEXT("&File"));
        AppendMenu(hMenu, MF_STRING, IDM_FILE_NEW, TEXT("&Open"));
        SetMenu(hwnd, hMenuBar);
    
    }
    
    void OpenDialog(HWND hwnd){
        OPENFILENAME ofn;
        TCHAR szFile[MAX_PATH];
    
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.lpstrFile = szFile;
        ofn.lpstrFile[0] = '\0';
        ofn.hwndOwner = hwnd;
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = TEXT("All files(*.*)\0*.*\0");
        ofn.nFilterIndex = 1;
        ofn.lpstrInitialDir = NULL;
        ofn.lpstrFileTitle = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
        if(GetOpenFileName(&ofn))
            LoadFile(ofn.lpstrFile);
    }
    
    void LoadFile(LPSTR File){
        HANDLE hFile;
        DWORD dwSize;
        DWORD dw;
    
        LPBYTE lpBuffer = NULL;
    
        hFile = CreateFile(File, GENERIC_READ |  GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
        dwSize = GetFileSize(hFile, NULL); 
        lpBuffer = (LPBYTE) HeapAlloc (HeapCreate(HEAP_GENERATE_EXCEPTIONS, 0, 0), HEAP_ZERO_MEMORY, dwSize);
        ReadFile(hFile, lpBuffer, dwSize, &dw, NULL);
        CloseHandle(hFile);
        lpBuffer[dwSize] = 0;
        SetWindowText(ghwndEdit, (LPSTR)lpBuffer);
        HeapFree(GetProcessHeap(), 0, lpBuffer);
    }

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    ...it compiles but always terminates...
    Of course. You call HeapCreate() without persisting the returned HANDLE. Then you call HeapFree() where you specify freeing a memory allocation from a different heap from the one in which it was created.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Why don't you use malloc and free, they are easier to use.
    Code:
    void LoadFile(LPSTR File) 
    {
      HANDLE hFile;
      DWORD dwSize;
      DWORD dw;
      char *buffer;
      
      hFile = CreateFile(File, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
      if (hFile == INVALID_HANDLE_VALUE)
      {
        // handle error
        return;
      }
    
      dwSize = GetFileSize(hFile, NULL);
      buffer = (char *)malloc(dwSize + 1);
      if (buffer != NULL)
      {
        ReadFile(hFile, buffer, dwSize, &dw, NULL);
        CloseHandle(hFile);
        buffer[dwSize] = 0;
        SetWindowTextA(ghwndEdit, buffer);
        free(buffer);
      }
      else
      {
        // handle out of mem here
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to windows programming, win32
    By *nick in forum Windows Programming
    Replies: 3
    Last Post: 09-15-2012, 11:06 PM
  2. win32 api MDI windows
    By TheNewOne in forum Windows Programming
    Replies: 5
    Last Post: 03-20-2009, 09:11 PM
  3. Which one :MFC, Win32, Windows Forms (.Net)?
    By Robert_Sitter in forum Windows Programming
    Replies: 6
    Last Post: 11-17-2005, 06:15 AM
  4. child windows with win32 API
    By hiya in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2005, 03:08 AM
  5. Windows Using Win32 API With M$ VC++
    By MicroFiend in forum Windows Programming
    Replies: 3
    Last Post: 11-01-2002, 03:17 PM

Tags for this Thread