NO

Author Topic: lstrlen  (Read 2319 times)

Grincheux

  • Guest
lstrlen
« on: January 02, 2020, 05:54:24 PM »
Why when a string is defined as a constant the c compiler calls the lstrlen function? It knows its length at compile time. I don't understand.

Example :
Code: [Select]
static void Main_OnPaint(HWND hwnd)
{
    PAINTSTRUCT ps;
    RECT rc;

   int   _i = lstrlen("Hello, Windows!") ;

    BeginPaint(hwnd, &ps);
    GetClientRect(hwnd, &rc);
    DrawText(ps.hdc, _T("Hello, Windows!"), -1, &rc, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
    EndPaint(hwnd, &ps);
}


The asm code is :

Quote
lea rcx,[StringAdr]
call qword ptr [__imp_lstrlenA]

Is there an other way to compute a string length in that case.
I have strings as the following:

Code: [Select]
char str1[] = "String" ;
char str2[] = "\n"
"a file to \"generate\""
"%s\n" ;

From Pelle's help file :
Quote
6. Adjacent string literal tokens are concatenated into one string literal token ("abc" "def" becomes "abcdef").

An other question. In the previous code I have noticed that if a string exists more than once, it is created only once. Please confirm.
« Last Edit: January 02, 2020, 05:59:15 PM by Grincheux »

Grincheux

  • Guest
Re: lstrlen
« Reply #1 on: January 02, 2020, 06:45:43 PM »
The only way to compute string length when the string is known at compile time is :


int   _i = sizeof(szString) / sizeof(char) ;

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2144
Re: lstrlen
« Reply #2 on: January 03, 2020, 11:30:56 AM »
you presumably mean:
int   _i = sizeof(szString) / sizeof(TCHAR) ;
May the source be with you

Grincheux

  • Guest
Re: lstrlen
« Reply #3 on: January 03, 2020, 07:02:42 PM »
Yes >:(

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: lstrlen
« Reply #4 on: January 05, 2020, 05:38:04 PM »
Why when a string is defined as a constant the c compiler calls the lstrlen function?
Because all Windows functions are just that: functions. Black boxes. Unknown what the function does.
Use one of the standard C functions, that the compiler have any reason to care about, like strlen() or wcslen().

It knows its length at compile time.
No it doesn't. See above.
/Pelle