[Scripting Example] GetUserName |
Author: |
Message: |
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
O.P. [Scripting Example] GetUserName
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;
}
This post was edited on 06-29-2006 at 04:50 PM by matty.
|
|
06-29-2006 03:30 PM |
|
|
JonnyT
Junior Member
Posts: 28
Joined: Jun 2006
|
RE: [Scripting Example] GetUserName
theres a lsight spelling mistake code: var sBuffer = Interop.Allocate(2*(100+2));
and in the debug i get nothing just "sBuffer: "
|
|
06-29-2006 03:46 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
O.P. RE: [Scripting Example] GetUserName
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.
|
|
06-29-2006 03:49 PM |
|
|
Eljay
Elite Member
:O
Posts: 2949 Reputation: 77
– / / –
Joined: May 2004
|
RE: [Scripting Example] GetUserName
not as nicely styled as matty's but at least mine works
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));
}
|
|
06-29-2006 04:03 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
O.P. RE: [Scripting Example] GetUserName
quote: Originally posted by Eljay
not as nicely styled as matty's but at least mine works
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.
|
|
06-29-2006 04:04 PM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: [Scripting Example] GetUserName
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.
This post was edited on 06-29-2006 at 04:57 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
06-29-2006 04:40 PM |
|
|
JonnyT
Junior Member
Posts: 28
Joined: Jun 2006
|
RE: [Scripting Example] GetUserName
ah yeah that one works
so what else could this be used for?
This post was edited on 06-29-2006 at 04:42 PM by JonnyT.
|
|
06-29-2006 04:41 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
O.P. RE: [Scripting Example] GetUserName
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.
This post was edited on 06-29-2006 at 04:45 PM by matty.
|
|
06-29-2006 04:42 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
O.P. RE: [Scripting Example] GetUserName
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.
This post was edited on 06-29-2006 at 05:22 PM by matty.
|
|
06-29-2006 05:14 PM |
|
|
|