NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Name Matching Algorithm

PInvoke looks up the name of the unmanaged function in the DLL you specify, in order to load and call that code. However, in order to make things easier for the programmer, PInvoke actually carries out a fuzzy match of the name provided. For example, you may say you want to call the function “MessageBox” in the user32.dll, but PInvoke will actually call MessageBoxA (the version that accepts ANSI strings) or MessageBoxW (the version that accepts wide, or Unicode, strings), depending upon whether you are executing on a platform where strings are ANSI or Unicode. In fact, there is no such function as “MessageBox” in user32.dll at all.

The function name provided in the [sysimport] directive can be in one of two forms: either a regular name, such as “MessageBox”, or the ordinal of the target function in the DLL, specified as a decimal number preceded by #. For example, “#123”.

If the function ‘name’ is its ordinal, then PInvoke simply calls a GetProcAddress for that ordinal number.

If the function name is a regular name, then PInvoke does a match. For example, suppose the code asks for function “Foo”. Then:

  1. Ansi – if “Foo” exists return “Foo” elseif “FooA” exists return “FooA”
  2. Unicode – if “FooW” exists return “FooW” elseif “Foo” exists return “Foo”
  3. Auto – follow 1. or 2. above, depending on whether the platform is Ansi or Unicode
  4. Still not found? – if “_Foo@7” exists return “_Foo@7” where 7 is the number of bytes required for the parameter list.

If the noMangle bit is specified however, the matching is exact – given “Foo” we look only for “Foo”.

Note that the matching rule for Ansi or Unicode is quite different: