PDA

Click to See Complete Forum and Search --> : lstrlen


JoshT
Aug 23rd, 2001, 11:07 AM
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:


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



char * chBuff; //data buffer
chBuff = "Location: ./win2000.gif\n\n";
retval = WriteFile(hStdOut, chBuff, lstrlen(chBuff), &dwBytesWritten, NULL);


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)?

parksie
Aug 23rd, 2001, 12:17 PM
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.

jim mcnamara
Aug 23rd, 2001, 01:47 PM
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.



char buf[100];
// A:

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

// B:

for (int i = 0;i< lstrlen(buf); i++) {do the same thing;}



These are the same, except that lstrlen is not portable.

parksie
Aug 23rd, 2001, 01:57 PM
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.

JoshT
Aug 23rd, 2001, 02:17 PM
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.

parksie
Aug 23rd, 2001, 02:23 PM
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 ;))

jim mcnamara
Aug 24th, 2001, 04:33 PM
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.