0

I have been busy lately with using functions from the User32.dll in C#. For this you use a lot of

[ImportDll("User32.dll")]
public static extern void functionName();

And If you start googling for how to use a particular function everyone seems to be using the Fields wrong. When you should you be using which field and how to know how.

Fields and explanation:

BestFitMapping: Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.

CallingConvention:
Indicates the calling convention of an entry point.

CharSet:
Indicates how to marshal string parameters to the method and controls name mangling.

EntryPoint: Indicates the name or ordinal of the DLL entry point to be called.

ExactSpelling: Controls whether the DllImportAttribute.CharSet field causes the common language runtime to search an unmanaged DLL for entry-point names other than the one specified.

PreserveSig: Indicates whether unmanaged methods that have HRESULT or retval return values are directly translated or whether HRESULT or retval return values are automatically converted to exceptions.

SetLastError: Indicates whether the callee calls the SetLastError Win32 API function before returning from the attributed method.

ThrowOnUnmappableChar: Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.

source: DllImportAttribute Class (System.Runtime.InteropServices)

This is what I could find about them, can someone please explain these fields and when to which field.

Edit

example:

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

Is the SetLastError needed and the CharSet field and what do they do if they are. And this question for every field.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ian.V
  • 345
  • 3
  • 19
  • i have no idea what your asking – MichaelEvanchik Feb 14 '18 at 19:15
  • @MichaelEvanchik I editted the post hopefully this is more clear – Ian.V Feb 14 '18 at 19:22
  • you have cleared up the question. sadly all most all(other then the obvious) of these "parameters" are for .net only conversion/usage and i do not know there function. – MichaelEvanchik Feb 14 '18 at 19:27
  • @MichaelEvanchik Okay, thank you for your time anyways :) – Ian.V Feb 14 '18 at 19:29
  • 2
    All of this has already been explained, although the information is split into different questions (which is logical and correct, asking for multiple explanations in one question like this risks having the best explanation split between many different answers). The attribute properties related to error handling are explained here: https://stackoverflow.com/q/763724/103167 – Ben Voigt Feb 14 '18 at 19:29
  • 1
    EntryPoint is explained here: https://stackoverflow.com/q/11135607/103167 – Ben Voigt Feb 14 '18 at 19:31
  • 1
    Please do some research in existing [tag:dllimport] questions on StackOverflow and add that to your question. Then if you still have a question, it will be clearer and small enough to answer fully. – Ben Voigt Feb 14 '18 at 19:32
  • @BenVoigt Thank you for the feedback, I will keep this in mind next time, the Problem probaly was that I was looking in a to wide of range. – Ian.V Feb 14 '18 at 19:42
  • my go to resource on this is Adam Nathans https://www.amazon.com/NET-COM-Complete-Interoperability-Guide/dp/067232170X. covers dllimport as well as com – pm100 Mar 09 '23 at 22:32

0 Answers0