Below is a simple ‘Hello World’ example. This shows how you use a [sysimport] directive to ‘annotate’ your code. You supply three ‘arguments’ to the [sysimport] directive:
This information tells the C++ compiler all it needs to emit the correct PInvoke metadata into the object file it is building for you.
#import <mscorlib.dll> // to get Microsoft::Runtime::String [sysimport(dll="msvcrt", name="puts", charset="ansi")] int puts(String*); void main(void) { String *pStr = L"Hello World!"; puts(pStr); }
Notice how the managed String is Unicode, and therefore prefixed with “L” and that charset=”ansi”.