|
-
Aug 23rd, 2001, 11:07 AM
#1
Thread Starter
Black Cat
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:
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&)
PHP Code:
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)?
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.
-
Aug 23rd, 2001, 12:17 PM
#2
Monday Morning Lunatic
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
-
Aug 23rd, 2001, 01:47 PM
#3
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;i< lstrlen(buf); i++) {do the same thing;}
These are the same, except that lstrlen is not portable.
-
Aug 23rd, 2001, 01:57 PM
#4
Monday Morning Lunatic
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
-
Aug 23rd, 2001, 02:17 PM
#5
Thread Starter
Black Cat
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.
-
Aug 23rd, 2001, 02:23 PM
#6
Monday Morning Lunatic
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
-
Aug 24th, 2001, 04:33 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|