Shoutbox

[Scripting Example] GetUserName - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Scripting Example] GetUserName (/showthread.php?tid=62027)

[Scripting Example] GetUserName by matty on 06-29-2006 at 03:30 PM

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;
}

RE: [Scripting Example] GetUserName by JonnyT on 06-29-2006 at 03:46 PM

theres a lsight spelling mistake

code:
var sBuffer = Interop.Allocate(2*(100+2));
and in the debug i get nothing just "sBuffer: "

RE: [Scripting Example] GetUserName by matty on 06-29-2006 at 03:49 PM

quote:
Originally posted by JonnyT
theres a lsight spelling mistake
code:
var sBuffer = Interop.Allocate(2*(100+2));
and in the debug i get nothing just "sBuffer: "
Hmmm it should work... I will take a look at it when I get home, I am at work right now can't debug it.
RE: [Scripting Example] GetUserName by Eljay on 06-29-2006 at 04:03 PM

not as nicely styled as matty's but at least mine works :P

code:
function GetUserName()
{
    var sBuffer = Interop.Allocate((255+1) * 2);
    var nBufferLength = Interop.Allocate(4);
    nBufferLength.WriteDWORD(0, 255);
    Interop.Call('advapi32.dll', 'GetUserNameW', sBuffer, nBufferLength);
    Debug.Trace('sBuffer: ' + sBuffer.ReadString(0));
}

RE: [Scripting Example] GetUserName by matty on 06-29-2006 at 04:04 PM

quote:
Originally posted by Eljay
not as nicely styled as matty's but at least mine works :P

code:
function GetUserName()
{
    var sBuffer = Interop.Allocate((255+1) * 2);
    var nBufferLength = Interop.Allocate(4);
    nBufferLength.WriteDWORD(0, 255);
    Interop.Call('advapi32.dll', 'GetUserNameW', sBuffer, nBufferLength);
    Debug.Trace('sBuffer: ' + sBuffer.ReadString(0));
}

Oops... I forgot about the last param needing to be a datablock too. I will update mine.
RE: [Scripting Example] GetUserName by CookieRevised on 06-29-2006 at 04:40 PM

PS: A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.

And Lmcons.h defines UNLEN as being 256 characters. So I suppose the correct length for the (unicode) buffer should be 2*(256+1)

PS2:

quote:
// Free the memory we allocated
   sBuffer.Size = 0;
   lBuffer.Size = 0;
Will be done automatically as soon as the function ends since the two are local variables though.
RE: [Scripting Example] GetUserName by JonnyT on 06-29-2006 at 04:41 PM

ah yeah that one works :)

so what else could this be used for?


RE: [Scripting Example] GetUserName by matty on 06-29-2006 at 04:42 PM

quote:
Originally posted by CookieRevised
PS:
A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.

and Lmcons.h defines UNLEN as being 256 characters. So I suppose the correct length for the buffer should be 2*256+2
Ya your write. Haha I will fix that. I am thinking maybe we should make a thread where scripters post examples for people to learn. That way we can keep all these types of threads in one place.

quote:
Originally posted by JonnyT
ah yeah that one works :)

so what else could this be used for?

In all reality nothing really, this is just an example of using Datablocks and Interop.Call/Allocate. But there are menu things you can do with the API etc.
RE: [Scripting Example] GetUserName by matty on 06-29-2006 at 05:14 PM

quote:
Originally posted by CookieRevised
PS2: Will be done automatically as soon as the function ends since the two are local variables though.

code:
function TraceWin32Error(){
    var LastError = Interop.GetLastError();
    if(LastError != 0){
        var MsgBuffer = Interop.Allocate(1024);
        Interop.Call("Kernel32", "FormatMessageW", 0x1000, 0, LastError, 0, MsgBuffer, 1024, 0);
        Debug.Trace(MsgBuffer.ReadString(0));
        MsgBuffer.Size = 0; //Release the allocated memory now
    }
}

Why does Patchou do it in his example if its not needed?
Aside from it being good practise to get used to.