Should I use Ansi or Unicode encoding with dllimport?

When you use DllImport to import a function, you can tell CharSet to use. I noticed that in C #, C ++, and Visual Basic .Net runtime uses Ansi by default instead of Unicode for this. Therefore, for any system call with versions A and W, the version A..Net will be called by default, it uses unicode inside, and if I'm not mistaken, new versions of windows will also translate everything to Unicode, so this means a lot of additional assembly overhead.

I got the habit of always specifying unicode here, is this the right way to do something or will it cause problems?

+4
source share
1 answer

If you use ANSI variants, you will have problems if (for example) there is a file with a name that cannot be represented in the ANSI code page used. The only drawback to using Unicode is that your code may not work on older versions of Windows (95, 98, and ME).

There are several functions where you will need to use ANSI because they are actually using ASCII (e.g. GetProcAddress).

+6
source

All Articles