This example shows how to use the following:
Interop.Call
Interop.Allocate
code:
function GetUserName(){
/*
Allocate memory needed to store the returned username.
Why do we need to use (2*(100)+2) for the size of the allocated memory?
We do this because the maximum length of the returned string is
100 characters (changes with different APIs). Since these are
handled in Unicode we need to multiply the size by 2. Now the
reason we add 2 is for a null character in unicode.
(Unicode*(MaxLength)+NullTerminatedString)
*/
// Thanks Cookie for pointing out that its actually UNLEN+1 and UNLEN is 256
var sBuffer = Interop.Allocate(2*(256)+2);
// Thanks LJ I forgot I needed this.
var lBuffer = Interop.Allocate(4);
lBuffer.WriteDWORD(0, 255);
Interop.Call('advapi32.dll', 'GetUserNameW', sBuffer, lBuffer);
Debug.Trace('sBuffer: '+sBuffer.ReadString(0));
// Free the memory we allocated
sBuffer.Size = 0;
lBuffer.Size = 0;
}