4

I have a wchar_t * that I need to use on a function that needs a CFStringref

I tried using CFStringCreateWithCharacters but I'm not getting anywhere with it.

So, if I have:

wchar_t * widecharvar = L"some value";
CFStringRef stringref;

How do I convert and copy widecharvar to stringref? This value will be used in SecRequirementCreateWithString()

Thanks

Mr Aleph
  • 1,887
  • 5
  • 28
  • 44

1 Answers1

5

I'm doing this off the top of my head and memory; if it has trouble, comment and I'll actually test it.

// Check our byte order. Assuming we made the string as in your example
CFStringEncoding encoding = (CFByteOrderLittleEndian == CFByteOrderGetCurrent()) ? 
                              kCFStringEncodingUTF32LE : kCFStringEncodingUTF32BE;

int widecharvarLen = wcslen(widecharvar); 

CFStringRef string = CFStringCreateWithBytes(NULL, 
                                             widecharvar, 
                                             (widecharvarLen * sizeof(wchar_t)),  
                                             encoding,
                                             false);

That last false means that string does not include a BOM (Byte Order Mark), which is the kind of string I assume you're dealing with.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610