Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env raku
- # PopUpTest4.pl6
- # read the computer's name from Kernel32.dll as a pointer
- # and use the pointer to test memcpy_s
- use lib 'C:/NtUtil', 'K:/Windows/NtUtil', '/home/CDs/Windows/NtUtil';
- use NativeCall;
- use NativeConvert :c-utf8-to-raku-str;
- constant BYTE := uint8;
- constant LPDWORD := uint64; # long pointer to a DWORD
- constant LPSTR = CArray[BYTE]; # long pointer to a string
- constant DWORD := uint32;
- constant HANDLE = Pointer[void];
- constant WTS_CURRENT_SERVER_HANDLE = 0; # Current (local) server
- sub memcpy_s(
- #`{
- errno_t memcpy_s(
- void *dest,
- size_t destSize,
- const void *src,
- size_t count
- );
- Returns zero if successful; an error code on failure.
- $RtnCode = memcpy_s $SessionArray, $Length , $SessionPtr, $Length ;
- }
- CArray[BYTE] $dest, # [in,out] New buffer (target)
- DWORD $numberOfElements, # [in] Maximum size of $dest
- Pointer $src is rw, # [in] Pointer to the source to be copied
- DWORD $count # [out] number of bytes to copy from the source
- )
- is native("ucrtbase.dll")
- is symbol("memcpy_s")
- returns DWORD # [out] Zero if successful; an error code on failure
- { * };
- sub GetComputerName(
- #`{
- https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcomputernamea
- C++
- BOOL GetComputerNameA(
- [out] LPSTR lpBuffer,
- [in, out] LPDWORD nSize
- );
- [out] lpBuffer: A pointer to a buffer that receives the computer name or
- the cluster virtual server name. The buffer size should be large
- enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
- The maximum number of characters for a Netbios computer name is 15.
- The buffer passed to GetComputerName should be at least 16 characters
- to account for the nul at the end.
- [in, out] nSize On input, specifies the size of the buffer, in TCHARs. On output,
- the number of TCHARs copied to the destination buffer, not including
- the terminating null character.
- }
- Pointer $lpBuffer is rw,
- LPDWORD $nSize is rw
- )
- is native("Kernel32.dll")
- is symbol("GetComputerNameA")
- returns DWORD # If no error occurs, gethostname returns zero
- { * };
- # NetBios names are 15 characters plus 1 nul long maximum
- my LPDWORD $nSize = 16;
- my $NamePrt = NativeCall::Types::Pointer.new();
- my $RtnCode = GetComputerName $NamePrt, $nSize;
- my $SessionArray = CArray[BYTE].new( 0xFF xx $nSize );
- $RtnCode = memcpy_s $SessionArray, $nSize , $NamePrt, $nSize ;
- print "Computer Name = <";
- for $SessionArray -> $i { print Buf.new( $i ).decode("utf8-c8"); }
- print ">\n\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement