Results 1 to 7 of 7

Thread: lstrlen

  1. #1

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032

    lstrlen

    Does the Win32 function lstrlen work on a array of characters the way VB's Len would work on a string. It seems to work ok, but I'm not too familiar with the different Win32 data types (it wants a LPCTSTR but I pass it a pointer to a char).

    Here's the code I'm converting:

    VB Code:
    1. Dim strHTTP As String 'data buffer for HTTP header output
    2. strHTTP = "Location: ./win2000.gif" & vbCrLf & vbCrLf
    3. retval = WriteFile(STDOUT, ByVal strHTTP, Len(strHTTP), lngBytesWritten, ByVal 0&)

    PHP Code:
    char chBuff//data buffer
    chBuff "Location: ./win2000.gif\n\n";
    retval WriteFile(hStdOutchBufflstrlen(chBuff), &dwBytesWrittenNULL); 
    PS - Does anyone know of a good reference for converting VB string functions (like Left$ or Space$) to the equivalent Win32 API functions (not ANSI C/C++ functions)?
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes. It's there so you don't have to include the C or C++ Runtime Library if you need strlen.

    LPCTSTR = const TCHAR* - TCHAR is normally char unless you're compiling for Unicode.

    If you want things like Left$ or Mid$ it's usually easiest to use the standard library string class. However, if you don't mind messing around with buffers it isn't too hard to do.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    jim mcnamara
    Guest
    lstrlen is already 'cast' to the right datatype, plus it's an api.

    strlen is ansi C; it returns size_t, an unsigned integer. You have two choices; one calls the same api from embedded code, one the api directly. lstrlen is therefore generally faster under Windows.


    PHP Code:
    char buf[100];
    // A:

    for (int i 0;i< (int) strlen(buf);i++) { do something;}

    // B:

    for (int i 0;ilstrlen(buf); i++) {do the same thing;} 
    These are the same, except that lstrlen is not portable.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    There's no speed difference in them. The library version does a loop through the string itself. Plus you don't need to cast size_t, since it's usually int.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Ok, thanks.

    I'm actually trying to avoid library functions and stick with the API, to further my understanding of it.

    I think what might be confusing me is the way Windows has ANSI or UNICODE string types. I'll have to read up on it when I get a chance.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Until you understand them, don't bother and anything that takes LPCSTR or LPCTSTR, pass char*. If it specifically has a W in it (like LPWSTR, LPCWSTR) then ask about it then and I'll give you some pointers (*cough*...sorry )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    jim mcnamara
    Guest
    FWIW -
    this generates a compiler warning:

    for(int i = 0;i<strlen(str);i++)

    unsigned and signed mismatch

    this does not generate a compiler warning

    for (int i = o;i<lstrlen(str);i++)

    size_t is really UINT and int is signed. will definitely cause problems if you mix 'n' match types. It's a no-no.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width