views:

69

answers:

3

Hi, I've been trying to use the cpw library for some graphics things. I got it set up, and I seem to be having a problem compiling. Namely, in a string header it provides support for unicode via

#if defined(UNICODE) | defined(_UNICODE)
#define altstrlen wstrlen
#else
#define altstrlen strlen

Now, there's no such thing as wstrlen on Windows afaik, so I tried changing that to wcslen, but now it gives me and error because it tried to convert a char * to a wchar_t *. I'm kinda scared that if I just had it use strlen either way, something else would screw up.

What do you think stackoverflow?

A: 

If you're using Unicode on Windows, you need to change all of your character types to wchar_t. That means, instead of:

char *str = "Hello World";
int len = strlen(str);

You need:

wchar_t *str = L"Hello World";
int len = wcslen(str);

Notice that the character type has been changed to wchar_t and the string literal is prefixed with L.

Dean Harding
Thing is, this isn't my code, I'm using cpw window library for OpenGL.http://www.mathies.com/cpw/about.html . I'm just wondering what I should do about it (ie go through the library and change all the char to wchar_t, use strlen and hope for the best, etc).
Xzhsh
+1  A: 

Maybe you can define wcslen as wstrlen before including the lib header:

#define wstrlen wcslen
#include "cpw.h"

The error you are getting is likely to be due to you passing a char* that into something that ends up calling one of those functions.

Igor Zevaka
I checked over the library source, and it turns out they didn't follow their own format. Swapping their char * with wchar_t * when the macro was defined helped. Thanks
Xzhsh
A: 

If your data is char* based to begin with, then there is no need to call a Unicode version of strlen(), just use the regular strlen() by itself, since it takes char* as input. Unless your char* data is actually UTF-8 encoded and you are trying to determine the unencoded Unicode length, in which case you need to decode the UTF-8 first before then calling wcslen().

Remy Lebeau - TeamB