Problems with accessing external lib in /Command
[1/3] from: coussement:c:itc:mil:be at: 6-Mar-2001 14:01
Hi fellow Rebolians:
Because I need to use the user name in an NT-networking env, I thought to
use the "GetUserNameA" function from from ADVAPI32.dll ...
Based on the code found on MSDN at
http://support.microsoft.com/support/kb/articles/Q152/9/70.asp , I tried:
<snip>
name: make string! 255
len: make integer! 255
advapi32: load/library %advapi32.dll
get-user-name: make routine! [buffer [string!] length [integer!] return:
[integer!]] advapi32 "GetUserNameA"
get-user-name name len
print name ; because the returned value should be here
</snip>
But, although I tried several different combinations of passed parms,
/Command stays crashing.
Well, what did I do wrong ? Anybody as any experience regarding this matter
?
Thanks for answering and Best Regards,
chr==
[2/3] from: coussement::c::itc::mil::be at: 7-Mar-2001 9:01
Jeff:
Wow, this was a tricky one ! I tried your solution and it's OK.
Thanks you so much for helping me: I wouldn't have found it by myself ...
I will now try my new knowledge on other external libs.
Take care,
==christophe
[3/3] from: jeff:rebol at: 6-Mar-2001 7:52
Howdy, Coussement:
> Hi fellow Rebolians:
>
> Because I need to use the user name in an NT-networking env, I thought to
> use the "GetUserNameA" function from from ADVAPI32.dll ...
>
> Based on the code found on MSDN at
> http://support.microsoft.com/support/kb/articles/Q152/9/70.asp , I tried:
Always base your routines off the C version of these
functions, rather than the Visual Basic function that the
above describes. The addressing mode of the different
parameters isn't immediately clear when looking at the VB
declaration.
So we open MSVC and find:
BOOL GetUserName(
LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);
(ie, to decode the "Hungarian notation" above: long pointer to
string == char* and long pointer to DWORD == int* )
Okay, now this is probably one of the most important tricks
with REBOL/Command which is undocumented, though if you're a C
programmer, it should be fairly intuitive. This really needs
to be published into an addendum, probably, so here it goes:
In C, a pointer to a struct is a pointer to the struct's first
element. In REBOL/Commands, all structs are passed by
reference. What's this mean? This means struct! values in
REBOL/Command allow you to make references to values, for
example, we need this int*. REBOL/Command doesn't directly
provide support for pointer to int, but it's easy to make it
using a struct! value to wrap our integer, for example:
advapi32: load/library %advapi32.dll
get-user-name: make routine! [
name [string!]
l-ref [struct! [len [integer!]]]
return: [integer!]
] advapi32 "GetUserNameA"
insert/dup name: copy "" " " 255 ;-pad the buffer
l-ref: make struct! [len [integer!]] [255]
get-user-name name l-ref
trim name
== "jeff"