|
Prev: CSocket pump message error again and again ...
Next: How to integrate an MFC 7.0 DLL into a standard C++ application
From: mars on 26 Feb 2005 03:28 (OpenService(h, _T("NameOfServiceHere"), ...);) can solve some of my problem. DeviceIoControl(hFile, IOCTL_ENUM_ADAPTERS, NULL, 0, Buffer, sizeof(Buffer), &bytesReturned, NULL ) I do not want to have any unicode problem.but the "Buffer" returned by the function is unicode. Should I use MultiByteToWideChar(CP_UTF8,0,Buffer,-1,buffer,0); or WideCharToMultiByte(CP_UTF8,0,Buffer,-1,buffer,0,NULL,NULL); or any other function? really puzzled about the unicode.
From: Michael K. O'Neill on 26 Feb 2005 15:51 "mars" <makefriend8(a)hotmail.com> wrote in message news:%23mQ12y9GFHA.588(a)TK2MSFTNGP15.phx.gbl... > (OpenService(h, _T("NameOfServiceHere"), ...);) can solve some of my > problem. > > DeviceIoControl(hFile, > IOCTL_ENUM_ADAPTERS, > NULL, > 0, > Buffer, > sizeof(Buffer), > &bytesReturned, > NULL > ) > I do not want to have any unicode problem.but the "Buffer" returned by the > function is unicode. > Should I use MultiByteToWideChar(CP_UTF8,0,Buffer,-1,buffer,0); > or > WideCharToMultiByte(CP_UTF8,0,Buffer,-1,buffer,0,NULL,NULL); > or any other function? > really puzzled about the unicode. > > Some people #define a "CountOf" macro that they use in place of "sizeof", for example: #define countof(x) (sizeof(x)/sizeof(x[0])) // unicode-aware replacement for sizeof Use countof like this in your call to DeviceIoControl, and it will accommodate both unicode and non-unicode builds: > DeviceIoControl(hFile, > IOCTL_ENUM_ADAPTERS, > NULL, > 0, > Buffer, > countof(Buffer), > &bytesReturned, > NULL > )
From: Joseph M. Newcomer on 27 Feb 2005 18:02
In the case of DeviceIoControl, the API works in terms of bytes; it doesn't care if they are 8-bit characters, Unicode characters, or a sequence of DWORDs interlaced with an occasional double or float. Bytes are just bytes, and that's all it understands. So in this case, sizeof(buffer) is the correct reprsentation; if you did sizeof(buffer)/sizeof(TCHAR) then you would only read half the bytes you intended to. The fact that these are Unicode bytes is irrelevant. Now, if you have an 8-bit app otherwise, and you are reading Unicode, you have some potential problems. For example, if you did WideCharToMultiByte(CP_ACP...) then you might find that you could not translate all the incoming Unicode characters to ANSI (a silly, rather ethnocentric name--read "the native 8-bit character set for a given locale"). If you use CP_UTF8, as you suggest, you are now into a multibyte character set, (MBCS) which imposes a different set of rules on you. For example, you must now be careful to never assume that you can examine all the "characters" in an 8-bit string by doing for(i = 0; i < s.GetLength(); i++) { DoSomething(s[i]); } because some of those "characters" are 2, 3, or 4 bytes wide. There's lots of support to work safely on a MBCS, but you must make sure you use them consistently. On the whole, it is harder to retrofit MBCS to an app than to take an 8-bit app and make it Unicode. My own inclination would be to convert the entire app to Unicode so that you don't have a discrepancy between the two sets. If you need UTF8, what I've done in the past is to write out UTF8 when needed, but only after converting from Unicode, and to write UTF8 immediately after having converted from Unicode, but use Unicode uniformly in the app. If you have an 8-bit app, you can usually spend a few intense days of code examination and editing and have a bimodal (8-bit or Unicode) at the end of it. Chances are higher that it will work immeidately than if you try to use UTF8 internally as a MBCS. Having someone else do a code walkthrough with you often helps; after you think you've gotten everything right, a second viewer will usually find the failure modes (this has happened to me both times I did a major Unicode conversion...60K-80K source lines...where I missed a couple spots). If you do not really need full Unicode/MBCS compatibility, using CP_ACP on what appears to be a list of simple names ("enumerate adapters") will probably work just fine if you are using an 8-bit app. joe On Sat, 26 Feb 2005 16:28:21 +0800, "mars" <makefriend8(a)hotmail.com> wrote: >(OpenService(h, _T("NameOfServiceHere"), ...);) can solve some of my >problem. > >DeviceIoControl(hFile, > IOCTL_ENUM_ADAPTERS, > NULL, > 0, > Buffer, > sizeof(Buffer), > &bytesReturned, > NULL > ) >I do not want to have any unicode problem.but the "Buffer" returned by the >function is unicode. >Should I use MultiByteToWideChar(CP_UTF8,0,Buffer,-1,buffer,0); >or >WideCharToMultiByte(CP_UTF8,0,Buffer,-1,buffer,0,NULL,NULL); >or any other function? >really puzzled about the unicode. > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm |