meetraz has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing an XS module that will interface with the Win32 API. Most of the API and library functions take unicode strings as input, such as the type "LPCWSTR". (array of wchar_t)
When I check the lib\ExtUtils\typemap file, there is an entry for wchar_t* which seems to be what I need. But, the C code that XS produces is just casting char* to wchar_t* which doesn't make any sense.. and the library functions don't like it.
Yes, that's one way to do it, and it works fine. But it sure seems like a lot of work for every string, in every function, in every module. Even if I write a function to help with some of it, I would still have to free() the strings manually after I was done using them. That doesn't sound very perl-like. There has to be an easier way to do it... Does anybody know? I prefer to have something that handles memory allocation/deallocation automatically.// get function argument char* NodeName = (char *)SvPV(ST(0),PL_na); // get argument length int nNodeNameLen = strlen(NodeName) + 1; // alloc memory LPWSTR wNodeName = (LPWSTR)malloc(nNodeNameLen * sizeof(WCHAR)); // do conversion mbstowcs(wNodeName, NodeName, nNodeNameLen); // Here's where I'd actually use the data // I'm leaving out the part where I allocate widebuffer someLibFunction(wNodeName, widebuffer, buffersize); // de-allocate free(wNodeName); /* And now I'd have to do it all in reverse: allocate a new char* buffer, convert the output back to char*, deallocate the wide buffer, put the result back on the stack, and free the wide buffer. */
I've already looked at SvPVutf8() and sv_utf8_upgrade() but neither of these appear to do what I need. I've also tried passing in the string as unicode from the perl side, using pack/unpack or utf8 or Encode but that doesn't seem to work either. Is perl's notion of unicode not the same as "array of wchar_t"? Or am I confusing two different things?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unicode/Wide strings and XS
by PodMaster (Abbot) on Jan 09, 2004 at 07:30 UTC | |
by meetraz (Hermit) on Jan 09, 2004 at 15:46 UTC | |
by tye (Sage) on Jan 09, 2004 at 16:41 UTC |