[REBOL] Re: Problems with accessing external lib in /Command
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"