Type conversions and buffers make me sick

This is a discussion on Type conversions and buffers make me sick within the Windows Programming forums, part of the Platform Specific Boards category; Im trying to take different values of type DWORD, HWND and add them to a listbox. However everytime i try ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    15

    Angry Type conversions and buffers make me sick

    Im trying to take different values of type DWORD, HWND and add them to a listbox. However everytime i try to run the program it runs but locks down the computer and it does not work. What am i doing wrong?

    Code:
    char *buf;
    //Returns a DWORD
    processID = startup();
    
    buf = (char*)GlobalAlloc(GPTR, 50);
    itoa(processID, buf, 10);
    index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by favetelinguis View Post
    What am i doing wrong?
    Not posting the part of your code with the error, as in, what you've posted would work fine (assuming the success of all those functions)

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    I got it to work now, it was not wrong with the code as you said. Just a strange error at another place in the code.

    How would i do a dynamic allocation, as it is now buf is always 50 but i would like it to be only as big as it needs to house the DWORD. Also do i have to free any memory after this or can i use Alloc over and over without clearing anything?

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    To make it more clear is this how you use GlobalAlloc and only work with one variabel?

    Code:
    int index;
    char *buf;
    int strLen;
    
    
    buf = (char*)GlobalAlloc(GPTR, 50);
    
    itoa((int)myHWND, buf, 10);
    index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
    
    itoa((int)myHWND, buf, 10);
    index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
    
    strLen = GetWindowTextLength(myHWND);
    buf = (char*)GlobalAlloc(GPTR, strLen+1);
    GetWindowText(myHWND, buf, strLen);
    index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    There's no need to use any allocation for the DWORD or the HWND, they have a set number of digits so just use stack buffers.
    If you're using C++ you can make a big enough buffer for all the digits by including <limits> and using
    Code:
    const DWORD numDwordDigits = std::numeric_limits<DWORD>::digits10 + 1;
    char dwordBuffer[numDwordDigits + 1];
    // In C, you'll have to include <limits.h> and use this
    char dwordBuffer[((CHAR_BIT * sizeof (DWORD)) * 301L / 1000) + 2];
    _ultoa(processID, dwordBuffer, 10); // using _ultoa as that won't interpret big DWORDs as negative numbers
    itoa doesn't work correctly with HWNDs, because they're 64-bit values if you compile your program as 64-bit. It's easiest to use _ui64toa instead. HWNDs are also usually displayed in base-16 rather than base-10.
    Code:
    const DWORD numHwndDigits = std::numeric_limits<HWND>::digits10 + 1;
    char hwndBuffer[numHwndDigits + 1];
    // In C, you'll have to include <limits.h> and use this
    char hwndBuffer[((CHAR_BIT * sizeof (HWND)) * 301L / 1000) + 2];
    _ui64toa((unsigned long long)myHWND, hwndBuffer, 16);
    The text string one is alright, but just use malloc / new [] instead. Using GlobalAlloc doesn't gain you anything but extra typing. And yes you have to GlobalFree (for GlobalAlloc)/free (for malloc)/delete[] (for new[]) when you're finished with it (i.e. after calling SendDlgItemMessage)
    Last edited by adeyblue; 01-25-2012 at 05:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! About Type Conversions
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 05-28-2006, 09:24 PM
  2. type conversions
    By kocika73 in forum C Programming
    Replies: 2
    Last Post: 10-06-2004, 09:45 AM
  3. Type Conversions
    By Vicious in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2004, 08:09 AM
  4. Type conversions
    By sean in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2002, 02:52 PM
  5. Type conversions
    By Jerant in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21