WideChar
-
You don't - strings are wide characters (Unicode) by default in .NET. You actually have to convert to ANSI when calling native functions on Windows (as opposed to Windows NT). We have convered this countless times in the past, so I urge you both now and in the future to search first for previous similar questions. You can search this forum by clicking "Search comments" above. For an introduction to marshaling strings with Platform Invoke (P/Invoke), read Marshaling Strings[^]. Basically, when you use the
DllImportAttribute
to P/Invoke native DLL entry points, or define a structure for use with a native function using theStructLayoutAttribute
, you set theCharSet
field that both have toCharSet.Ansi
(default for P/Invoke) to call only ANSI functions (where native strings are declared aschar*
),CharSet.Unicode
to call only Unicode functions (where native strings are declared aswchar_t*
, almost always the case for OLE/COM), orCharSet.Auto
when you want to call ANSI functions in Windows (i.e., Windows 9x/ME) and Unicode functions in Windows NT (i.e., Windows NT 4, Windows 2000, Windows XP, Windows 2003, and "Longhorn", plus all future Windows platforms). Windows only supports ANSI (although there is a Unicode layer, but that's an extra install) and Windows NT typically supports both but it's better to use Unicode because it allows for a much larger character set and you don't have to mess with codepages. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]