The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

    • RT @MarcBerns1: @zeeger Je zou wel gek zijn als je op erfpacht woont en in verduurzaming investeert: wordt meteen je erfpacht verhoogt, kan… 1 hour ago
    • RT @isotopp: TIL my potato server at home can create a million directories on XFS in 16s, single threaded. Using multiprocessing(10), it t… 1 hour ago
    • RT @hansdamen: Dat ging snel, het Twitteraccount @MinBijleveld is binnen de kortste keren omgezet in het generieke @DefensieMin. Gemakkelij… 1 hour ago
    • RT @locuta: OPROEP AAN ALLEN: Kijk vanavond om 20:25 naar 'Alleen Tegen De Staat' op NPO2, documentaire over de Toeslagenaffaire. 1 hour ago
    • RT @LarsWienand: Ein Kunde in einer Tankstelle gerät wegen der fehlenden Maske in Streit, kommt umgezogen und mit Maske wieder und erschieß… 1 hour ago
  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,465 other followers

Archive for February 28th, 2012

P/Invoke: usually you need CharSet.Auto (via: .NET Column: Calling Win32 DLLs in C# with P/Invoke)

Posted by jpluimers on 2012/02/28

I don’t do P/Invoke often, and somehow I have trouble remembering the value of CharSet to pass with DllImport.

In short, pass CharSet.Auto unless you P/Invoke a function that is specific to CharSet.Ansi or CharSet.Unicode. The default is CharSet.Ansi, which you usually don’t want:

when Char or String data is part of the equation, set the CharSet property to CharSet.Auto. This causes the CLR to use the appropriate character set based on the host OS. If you don’t explicitly set the CharSet property, then its default is CharSet.Ansi. This default is unfortunate because it negatively affects the performance of text parameter marshaling for interop calls made on Windows 2000, Windows XP, and Windows NT®.

The only time you should explicitly select a CharSet value of CharSet.Ansi or CharSet.Unicode, rather than going with CharSet.Auto, is when you are explicitly naming an exported function that is specific to one or the other of the two flavors of Win32 OS. An example of this is the ReadDirectoryChangesW API function, which exists only in Windows NT-based operating systems and supports Unicode only; in this case you should use CharSet.Unicode explicitly.

–jeroen

via: .NET Column: Calling Win32 DLLs in C# with P/Invoke.

Posted in .NET, Ansi, C#, Delphi, Development, Encoding, Prism, Software Development, Unicode | 3 Comments »

 
%d bloggers like this: