Getting the UserName (an example of calling a Windows API function)
GetUser() ; Return the name of the logged-on user
N rslt,handle
N lpBuffer,nsize
; Get memory to receive the result
S lpBuffer=$ZMSM(1,256)
; Get memory to pass and return the buffer length
S nsize=$ZMSM(1,4)
; Initialize it with the buffer length
V nsize::256
; Load the DLL that contains the API call we want
S handle=$&MSM.LOADLIB("advapi32.dll")
; Call the ANSI version of GetUser, because that should work on Win9x as well as NT
; However, this example doesn't handle usernames that contain characters
; which are multi-byte encoded (e.g. "foreign" characters)
S rslt=$&MSM.CALLENTRY(handle,"GetUserNameA",$$spar(lpBuffer)_$$spar(nsize))
; Unload the DLL
D &MSM.FREELIB(handle)
; If the call succeeded, fetch the result from the buffer
I rslt>0 S rslt=$V(lpBuffer,-3,$V(nsize)-1,1)
E S rslt=""
; Release the two memory areas
I $ZMSM(2,lpBuffer,256)
I $ZMSM(2,nsize,4)
; return the result
Q rslt
;
spar(p) ; Encode a 32-bit value (typically a pointer to a memory buffer)
; ready for use in the third argument of &MSM.CALLENTRY
N out,i
S out=""
I p<0 S p=4294967296+p
F i=1:1:4 S out=$C(p#256)_out,p=p\256
Q out