Advertisement
Guest User

Untitled

a guest
Dec 1st, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/usr/bin/env raku
  2. # PopUpTest4.pl6
  3.  
  4. # read the computer's name from Kernel32.dll as a pointer
  5. # and use the pointer to test memcpy_s
  6.  
  7.  
  8. use lib 'C:/NtUtil', 'K:/Windows/NtUtil', '/home/CDs/Windows/NtUtil';
  9. use NativeCall;
  10. use NativeConvert :c-utf8-to-raku-str;
  11.  
  12.  
  13. constant BYTE := uint8;
  14. constant LPDWORD := uint64; # long pointer to a DWORD
  15. constant LPSTR = CArray[BYTE]; # long pointer to a string
  16. constant DWORD := uint32;
  17. constant HANDLE = Pointer[void];
  18. constant WTS_CURRENT_SERVER_HANDLE = 0; # Current (local) server
  19.  
  20.  
  21. sub memcpy_s(
  22. #`{
  23. errno_t memcpy_s(
  24. void *dest,
  25. size_t destSize,
  26. const void *src,
  27. size_t count
  28. );
  29. Returns zero if successful; an error code on failure.
  30.  
  31. $RtnCode = memcpy_s $SessionArray, $Length , $SessionPtr, $Length ;
  32. }
  33. CArray[BYTE] $dest, # [in,out] New buffer (target)
  34. DWORD $numberOfElements, # [in] Maximum size of $dest
  35. Pointer $src is rw, # [in] Pointer to the source to be copied
  36. DWORD $count # [out] number of bytes to copy from the source
  37. )
  38. is native("ucrtbase.dll")
  39. is symbol("memcpy_s")
  40. returns DWORD # [out] Zero if successful; an error code on failure
  41. { * };
  42.  
  43.  
  44. sub GetComputerName(
  45. #`{
  46. https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcomputernamea
  47. C++
  48.  
  49. BOOL GetComputerNameA(
  50. [out] LPSTR lpBuffer,
  51. [in, out] LPDWORD nSize
  52. );
  53. [out] lpBuffer: A pointer to a buffer that receives the computer name or
  54. the cluster virtual server name. The buffer size should be large
  55. enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
  56.  
  57. The maximum number of characters for a Netbios computer name is 15.
  58. The buffer passed to GetComputerName should be at least 16 characters
  59. to account for the nul at the end.
  60.  
  61. [in, out] nSize On input, specifies the size of the buffer, in TCHARs. On output,
  62. the number of TCHARs copied to the destination buffer, not including
  63. the terminating null character.
  64. }
  65. Pointer $lpBuffer is rw,
  66. LPDWORD $nSize is rw
  67. )
  68. is native("Kernel32.dll")
  69. is symbol("GetComputerNameA")
  70. returns DWORD # If no error occurs, gethostname returns zero
  71. { * };
  72.  
  73. # NetBios names are 15 characters plus 1 nul long maximum
  74. my LPDWORD $nSize = 16;
  75.  
  76. my $NamePrt = NativeCall::Types::Pointer.new();
  77. my $RtnCode = GetComputerName $NamePrt, $nSize;
  78.  
  79. my $SessionArray = CArray[BYTE].new( 0xFF xx $nSize );
  80.  
  81. $RtnCode = memcpy_s $SessionArray, $nSize , $NamePrt, $nSize ;
  82.  
  83. print "Computer Name = <";
  84. for $SessionArray -> $i { print Buf.new( $i ).decode("utf8-c8"); }
  85. print ">\n\n";
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement