C Under Windows Problem

This is a discussion on C Under Windows Problem within the Windows Programming forums, part of the Platform Specific Boards category; Thank You I Have Got That What I Was trying To Do Originally Posted by anonytmouse No, for the reasons ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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

Page 2 of 2 FirstFirst 12
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, 09:33 PM

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