Win32 API functions vs. their CRT counterparts (e.g. CopyMemory vs. memcpy)

后端 未结 2 739
走了就别回头了
走了就别回头了 2021-01-05 00:46

In writing Win32 C/C++ code, is there any advantage (e.g. performance?) in using Windows-specific functions like lstrcpyn or CopyMemory instead of

相关标签:
2条回答
  • 2021-01-05 01:24

    It depends on the function and your requirements.

    For things like memcpy, there isn't any point whatsoever to choosing the Windows-specific versions. Stick with standard C to keep it simple and portable.

    For other things like mbstowcs, you might need to use things like MultiByteToWideChar instead -- depending on what functionality you need.

    Personally I go for the C versions if possible, and only go for Win32 versions afterwards -- because there's really no reason to write Windows-specific code when it could be written portably.

    0 讨论(0)
  • 2021-01-05 01:42

    At least some CRT functions use the Win32 functions internally. Also the CRT requires additional initialization (e.g. thread specific data for functions like strtok) and cleanup, that you might not want to have to happen.

    You could create a plain Win32 application, without any dependency on anything else including the CRT (much like you could create a plain NT application using NTDLL.DLL - I think smss.exe of Windows is such a process BTW).

    Having that said, I think that for most applications that doesn't matter.

    UPDATE Since people seem to get so hooked up on the difference of individual functions, in particular memcpy vs. CopyMemory, I would like to add that not all functions in CRT are wrappers around those in Win32. Naturally, some can be implemented without any help from Win32 (actually memcpy is a good example for that), while others (sensibly) can't. Something that, I believe, @Merdad hinted in his answer to.

    So, portability aside, I don't think performance is the next best argument for or against using the CRT. You should choose what fits best and that typically will be the CRT. And there is nothing speaking against using individual Win32 functions (with CRT equivalents), where you seem fit.

    0 讨论(0)
提交回复
热议问题