Thread: C Under Windows Problem

  1. #16
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Thank You I Have Got That What I Was trying To Do
    Quote Originally Posted by anonytmouse
    No, for the reasons pointed out by vart. In my example, the size of the array is 512, and GetWindowText will return a maximum of 512 characters, as indicated by the last argument.

    If you need to return an unknown number of characters and therefore can not use a fixed size array, you can allocate memory dynamically. This function shows how to do that:
    Code:
    char* AllocAndGetText(HWND hwndControl)
    {
    	int count = GetWindowTextLength(hwndControl);
    	char* result = malloc(count * sizeof(char));
    
    	if (result != NULL)
    	{
    		GetWindowText(hwndControl, result, count);
    	}
    
    	return result;
    }
    and the function would be used in the following manner:
    Code:
    char* edit_text = AllocAndGetText(hwndEdit);
    
    /* Use text in edit_text */
    
    /* Free memory when you're done with it. */
    free(edit_text);
    However, usually there is a reasonable maximum number of characters you would expect and you can use an array.

    This tutorial is very good if you haven't seen it.
    char* result = malloc(count * sizeof(char));
    Didn't Work
    I Used
    char *result = (char *) malloc(count * sizeof(char));
    But It Gets 1 Character less That The Text In The textbox

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by neel_basu
    But It Gets 1 Character less That The Text In The textbox
    You need 1 more character for the Null-symbol
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    Quote Originally Posted by CornedBee
    This ain't MFC ...
    Yes, my mistake, I did'nt read the first post thoroughly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPv6 ping in windows...problem..lots of ode:(
    By Neill KElly in forum C Programming
    Replies: 3
    Last Post: 04-27-2009, 11:50 PM
  2. Exit Windows problem
    By cfrost in forum Windows Programming
    Replies: 7
    Last Post: 08-18-2004, 05:21 PM
  3. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  4. Application Termination Problem in Windows XP
    By wasabee in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 12:53 PM
  5. C++ Gurus, UNIX vs. Windows ascii problem perhaps?
    By puck in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2003, 10:33 PM