Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Is it possible to force .NET to use the unicode version of P/Invoke method in its own code

For example if i call the method System.IO.Directory.CreateDirectory, within the .NET code the Win32 API method CreateDirectory from kernel32.dll is called.

In the .NET source code the method is declared as following:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)]
internal static extern bool CreateDirectory(
  string path,
  Win32Native.SECURITY_ATTRIBUTES lpSecurityAttributes);

CharSet is set to CharSet.Auto. It seems that always the Ansi version is called.

My question is if i can force .NET to use the unicode version instead of the Ansi version.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It seems that always the Ansi version is called.

How do you know this? Are you using .NET core on Linux or something? https://docs.microsoft.com/en-us/dotnet/standard/native-interop/charset

In any case, instead of this:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)]

This:

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]

Alternatively, all Win32 APIs are really macros that map to either an A postfixed version of W postfixed version for ANSI and WIDE (unicode) respectively.

So calling CreateDirectoryW directly should work:

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
internal static extern bool CreateDirectoryW(
  string path,
  Win32Native.SECURITY_ATTRIBUTES lpSecurityAttributes);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda