NO

Author Topic: GetTextLen Problems  (Read 12119 times)

gromit

  • Guest
GetTextLen Problems
« on: April 13, 2013, 12:27:01 PM »
Hi all
In using the following code
i was losing the global string g_catstr
it seems that GetWindowTextLength may return a longer length than is actually in the control
having tried it with
GetDlgItemText(hDlg, ADDPART_PER_COMBO,st_part.perstr,12);
12 being initialised len of st_part.perstr
it works and does not overwrite g_catstr
so what is the best way to obtain len and not have to state a hardcoded figurelike 12


Ansi version
//int clen  = GetWindowTextLength(GetDlgItem(hDlg, ADDPART_PER_COMBO));
Wide version   
      int clen  = GetWindowTextLengthW(GetDlgItem(hDlg, ADDPART_PER_COMBO));
   if (clen>0)
      {
      wchar_t xstr[50];
      swprintf(xstr, L"%d",clen); //always evaluates to 26 for ansi or wide
      CheckMemoryStatus();
      MessageBox(g_hwnd,xstr, L"len is", MB_OK | MB_ICONINFORMATION);

      MessageBox(g_hwnd,g_catstr, L"In AddEditDlog topOK", MB_OK | MB_ICONINFORMATION);


      
      GetDlgItemText(hDlg, ADDPART_PER_COMBO,st_part.perstr,12);
   
if i use clen+1 it overwrites g_catstr and shows it as empty
//GetWindowText(GetDlgItem(hDlg, ADDPART_PER_COMBO),st_part.perstr,clen+1);
   
      MessageBox(hDlg,st_part.perstr, L"per string INFO", MB_OK | MB_ICONINFORMATION);
MessageBox(g_hwnd,g_catstr, L"In AddEditPArtsDlog botOK", MB_OK | MB_ICONINFORMATION);
}

gromit

  • Guest
Re: GetTextLen Problems
« Reply #1 on: April 13, 2013, 04:23:21 PM »
Hi again
Replying to my own enquiry here

Having, as i stated above, found why i was loosing a global variable.

I have reached the conclusion that it woul be far better if there was a way to simply recall from within the program the length at which the original string was initialised.

So does anyone know of a way to return the length the string was initialised at
i.e wchar_t xstring[50]
i want that 50
i can obviously get it by forming an array of lengths or enum it
but is there a way that is already avaible.

GetIniTialisedStringLen() sort o thing

an odd one i know
But any help Gratefully appreciated

Gromit


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: GetTextLen Problems
« Reply #2 on: April 13, 2013, 04:35:46 PM »
So does anyone know of a way to return the length the string was initialised at
i.e wchar_t xstring[50]
i want that 50
Do you mean sizeof(xstring)

EDIT: sizeof(xstring)/sizeof(xstring[0])
« Last Edit: April 13, 2013, 06:03:06 PM by timovjl »
May the source be with you

gromit

  • Guest
Re: GetTextLen Problems
« Reply #3 on: April 13, 2013, 04:59:30 PM »
Thankyou for your reply timovjl
Appreciated

struct
   {
   wchar_t qtystr[40];
   wchar_t  itemstr[200];
   wchar_t sizestr[30];
   wchar_t  pricestr[12];
   wchar_t  perstr[12];
   }st_part;
clen = sizeof(st_part.perstr);
swprintf(xstr, L"%d",clen);
MessageBox(g_hwnd,xstr, L"hopefully initial  len of st_part.perstr is", MB_OK ,MB_ICONINFORMATION);
This returns 24 for len perstr which is is initialised in above struct at 12
???


czerny

  • Guest
Re: GetTextLen Problems
« Reply #4 on: April 13, 2013, 05:38:33 PM »
This returns 24 for len perstr which is is initialised in above struct at 12
???
12 times sizeof(wchar_t) equals 24.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: GetTextLen Problems
« Reply #5 on: April 13, 2013, 06:12:11 PM »
Code: [Select]
   wchar_t  perstr[12];
   #define lengthof(x) sizeof(x)/sizeof(x[0])
   printf("\n***** static strlen=%i chars ***** \n", lengthof(perstr));
(lengthof is a built-in function for Masm & JWasm - strange that C doesn't know it...)

gromit

  • Guest
Re: GetTextLen Problems
« Reply #6 on: April 13, 2013, 10:05:52 PM »
Thanks jj2007

I just tested another string and of course dead right
Length initialised is half the sizeof () returned;

That has cleared up my little mystery for today
I did think i was losing it when perstr was obliterating a global e.g g_catstr
obviously being overwritten .

Big Thanks to all for advice

May the sun shine on your gardens
Its all good fun
:) :) :)


Offline jj2007

  • Member
  • *
  • Posts: 536
Re: GetTextLen Problems
« Reply #7 on: April 14, 2013, 12:40:46 AM »
Thanks jj2007

You are welcome. However, it seems that timovjl posted the same solution a few minutes before me, so the credits should better go to him ;-)