Re: null and native call question

2019-12-27 Thread Brad Gilbert
A Null pointer is just a pointer that points to the address 0.

So if you are dealing with it as an integer it will be 0.

On Fri, Dec 27, 2019 at 6:06 AM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> Hi All,
>
> https://docs.perl6.org/language/nativecall
>
>   "As you may have predicted by now, a NULL pointer
>   is represented by the type object of the struct type."
>
>
> https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexw
>
>   C++
>   LSTATUS RegQueryValueExW(
> HKEYhKey,
> LPCWSTR lpValueName,
> LPDWORD lpReserved,
> LPDWORD lpType,
> LPBYTE  lpData,
> LPDWORD lpcbData
>   );
>
>   lpReserved
>   This parameter is reserved and must be NULL.
>
> With "native", how do I satisfy the "NULL" requirement?
>
> constant WCHAR   := uint16;
>
> constant DWORD   := int32;
>
>
> sub RegQueryValueExW( DWORD, WCHARS, DWORD, DWORD, DWORD is rw, DWORD is
> rw ) is native("Kernel32.dll") returns DWORD { * };
>
> $RtnCode = RegQueryValueExW( $Handle, $lpValueName, int32, REG_DWORD,
> $lpData, $lpcbData );
>
> "int32" returns:
>
>  Cannot unbox a type object (int32) to int in method
>  CALL-ME at C:\rakudo\share\perl6\sources \947BDAB9F96E0E5FCCB383124F9
>  23A6BF6F8D76B (NativeCall) line 587
>
>
> Many thanks,
> -T
>


Re: My pop ups for windows module

2019-12-27 Thread ToddAndMargo via perl6-users

On 2019-12-26 23:15, WFB wrote:

Hi Todd,
I refactored your code a bit to make it a bit more readable IMHO. 
Thinking on publish it on modules.raku.org  if 
you are not interested to do so.


Hi Tom,

I am interested, but not quite yet.  There are three dependent modules I 
need to tweak to my liking first.


This one (WinPopUps)
WinMount (gives Linux style mount and dismount
  and label and UUID searches)
WinReg   (read and set registry values)

I will post them here when I am ready for everyone to
scrutinize before putting on modules.

In your mods, what is the syntax to export the "enum" calls?

-T


null and native call question

2019-12-27 Thread ToddAndMargo via perl6-users

Hi All,

https://docs.perl6.org/language/nativecall

 "As you may have predicted by now, a NULL pointer
 is represented by the type object of the struct type."

https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexw

 C++
 LSTATUS RegQueryValueExW(
   HKEYhKey,
   LPCWSTR lpValueName,
   LPDWORD lpReserved,
   LPDWORD lpType,
   LPBYTE  lpData,
   LPDWORD lpcbData
 );

 lpReserved
 This parameter is reserved and must be NULL.

With "native", how do I satisfy the "NULL" requirement?

constant WCHAR   := uint16;

constant DWORD   := int32;


sub RegQueryValueExW( DWORD, WCHARS, DWORD, DWORD, DWORD is rw, DWORD is 
rw ) is native("Kernel32.dll") returns DWORD { * };


$RtnCode = RegQueryValueExW( $Handle, $lpValueName, int32, REG_DWORD, 
$lpData, $lpcbData );


"int32" returns:

Cannot unbox a type object (int32) to int in method
CALL-ME at C:\rakudo\share\perl6\sources \947BDAB9F96E0E5FCCB383124F9
23A6BF6F8D76B (NativeCall) line 587


Many thanks,
-T


Re: how do I read registry key?

2019-12-27 Thread ToddAndMargo via perl6-users

My Latest with Wolf's recommendations.  Now I
can open and close the key, but still am getting
an invalid parameter trying to read the key



K:\Windows\NtUtil>perl6 -I. -e "use WinMount :GetLUA; say GetLUA();"
RegOpenKeyExW
RegOpenKeyExW   RtnCode 0

RegQueryValueExW
1
2
RegQueryValueExW   RtnCode 87  (87 = ERROR_INVALID_PARAMETER)
lpData pointer 0
lpcbData data length 0

RegCloseKey
RegCloseKey   RtnCode 0

True

# unit module WinMount;
# WinMount.pm6

#`{

   Utilities to mount and dismound drive partitions
   Note: LUA must be unset (0x) for mount to function 
prpoperly


   raku -I. -c WinMount.pm6

}

use NativeCall;
use WinPopUps :WinMsg;


# Reference to types and values: 
http://dsource.org/projects/tango/ticket/820


constant BYTE:= uint8;
constant WCHAR   := uint16;
constant DWORD   := int32;
constant REGSAM  := int32;
constant WCHARS  := CArray[WCHAR];
constant BYTES   := CArray[BYTE];

constant HKEY_CURRENT_USER  = 0x8001;
constant HKEY_LOCAL_MACHINE = 0x8002;
constant KEY_QUERY_VALUE   = 1;
constant ERROR_SUCCESS = 0; # Yeah, I know. The Win-Api uses 0 
for success and other values to indicate errors

constant REG_SZ = 1;

constant KEY_READ  = 0x20019;
constant KEY_SET_VALUE = 0x0002;
constant REG_DWORD = 0x0004;



sub to-c-str( Str $str ) returns CArray[WCHAR]  is export( 
:to-c-str ) {

   my @str := CArray[WCHAR].new;
   for ( $str.comb ).kv -> $i, $char { @str[$i] = $char.ord; }
   @str[ $str.chars ] = 0;
   @str;
}


sub wstr( Str $str ) returns WCHARS  is export( :wstr ) {
CArray[WCHAR].new( $str.comb.map: *.ord )
}


sub GetLUA() is export( :GetLUA ) {

#`{

Returns the LUA value in the registry to True (0x0001) or False 
(0x)



[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
  "EnableLUA"=dword:

https://docs.perl6.org/language/nativecall

Win32 return codes:

https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

}

   my Str $SubName = &?ROUTINE.name;
   my Str $OS  = $*KERNEL.name;
   if not $OS eq "win32" { say "Sorry, $SubName only work in 
Windows."; exit; }


   my Bool  $LUA = True;
   my   $RtnCode;

   my Str   $SubKey = 
Q[SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\];

   my Str   $Key = Q[EnableLUA];

   my   $lpSubKey= wstr( $SubKey );
   my   $lpValueName = wstr( $Key );
   # my $lpSubKey= CArray[uint8].new($Key.encode.list);
   # my $lpValueName = CArray[uint8].new($SubKey.encode.list);


   my int32 $Handle;
   my int32 $ulOptions = 0;
   my int32 $lpData;
   my int32 $lpcbData;
   my int32 $lpReserved = 1;


#`{
Open the key:

https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regopenkeyexw

https://docs.microsoft.com/en-us/windows/win32/sysinfo/registry-key-security-and-access-rights
C++
LSTATUS RegOpenKeyExW(
   HKEYhKey,  # Hive name (HKEY_LOCAL_MACHINE)
   LPCWSTR lpSubKey,  # path to the 
key(/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System/EnableLUA)

   DWORD   ulOptions, # 0
   REGSAM  samDesired,# KEY_READ (0x20019), KEY_SET_VALUE 
(0x0002)
   PHKEY   phkResult  # A pointer to a variable that 
receives a handle to the opened key

);
}
   say "RegOpenKeyExW";
   sub RegOpenKeyExW( DWORD, WCHARS, DWORD, DWORD, DWORD is rw) is 
native("Kernel32.dll") returns DWORD { * };
   $RtnCode = RegOpenKeyExW( HKEY_LOCAL_MACHINE, $lpSubKey, 
$ulOptions, KEY_READ, $Handle );

   say "RegOpenKeyExW   RtnCode $RtnCode\n";



#`{
Read the key:
use RegQueryValueExW if you know key and value name

https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexw
C++
LSTATUS RegQueryValueExW(
   HKEYhKey,  # Hive name (HKEY_LOCAL_MACHINE)
   LPCWSTR lpValueName,   # path to the 
key(\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA)
   LPDWORD lpReserved,# give it "int32" without the quotes 
to give it a NULL
   LPDWORD lpType,# Registry Value Type (REG_DWORD 
which is 32 bit)

   LPBYTE  lpData,# Pointer to the return value
   LPDWORD lpcbData   # number of bytes in the return value
);
}
   say "RegQueryValueExW";
   sub RegQueryValueExW( DWORD, WCHARS, DWORD, DWORD, DWORD is rw, 
DWORD is rw ) is native("Kernel32.dll") returns DWORD { * };

say "1";
   $RtnCode = RegQueryValueExW( $Handle, $lpValueName, $lpReserved, 
REG_DWORD, $lpData, $lpcbData );

say "2";
   say "RegQueryValueExW   RtnCode 

Re: how do I read registry key?

2019-12-27 Thread ToddAndMargo via perl6-users

On 2019-12-27 00:45, WFB wrote:

Hi Todd,

According to this: 
https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-

Return code 161 means: ERROR_BAD_PATHNAME.
Changing your code fixed that:
  my Str   $SubKey = 
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System';

  my       $lpValueName = wstr('EnableLUA');

Then the return code of the RegQueryValueExW is 87. According to the 
page above, that means ERROR_INVALID_PARAMETER.
Because of that I looked at the RegQueryValueExW help page you linked 
and noticed that it says:


|lpReserved|

This parameter is reserved and must be *NULL*.

In your code there is a 1 given. Unfortunately, just a 0 does not 
helped. Here, I have no idea how a NULL is given.



Regards,

Wolf


Hi Wolf,

The guys over on comp.lang.c++ gave me the same
link to the error codes.

I will try your fixes tomorrow when I get some free time.

Thank you!

-T


Re: how do I read registry key?

2019-12-27 Thread WFB
Hi Todd,

According to this:
https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
Return code 161 means: ERROR_BAD_PATHNAME.
Changing your code fixed that:
 my Str   $SubKey =
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System';
 my   $lpValueName = wstr('EnableLUA');

Then the return code of the RegQueryValueExW is 87. According to the page
above, that means ERROR_INVALID_PARAMETER.
Because of that I looked at the RegQueryValueExW help page you linked and
noticed that it says:

lpReserved

This parameter is reserved and must be *NULL*.

In your code there is a 1 given. Unfortunately, just a 0 does not helped.
Here, I have no idea how a NULL is given.


Regards,

Wolf


On Fri, 27 Dec 2019 at 08:22, WFB  wrote:

> I would love to look into that, however, my son needs all my free time and
> I do have very little knowledge about NativeCall.
> I hope I will find time in the next days to learn more of this stuff, but
> I will not much help :-(
>
>
> On Fri, 27 Dec 2019 at 07:31, ToddAndMargo via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> This is how far I have gotten:
>>
>> Note that is I use a "0" in
>>
>> $RtnCode = RegQueryValueExW( HKEY_LOCAL_MACHINE, $lpValueName, 1,
>> REG_DWORD, $lpData, $lpcbData );
>>
>> The program dies with no return code.
>>
>> -T
>>
>>
>>
>>  K:\Windows\NtUtil>perl6 -I. -e "use WinMount :GetLUA; say
>> GetLUA();"
>>  RegOpenKeyExW
>>  RegOpenKeyExW   RtnCode 161
>>
>>  RegQueryValueExW
>>  1
>>  2
>>  RegQueryValueExW   RtnCode 87
>>  lpData pointer 0
>>  lpcbData data length 0
>>
>>  RegCloseKey
>>  RegCloseKey   RtnCode 6
>>
>>  True
>>
>>
>>
>>
>>
>>  # unit module WinMount;
>>  # WinMount.pm6
>>
>>  #`{
>>
>> Utilities to mount and dismound drive partitions
>> Note: LUA must be unset (0x) for mount to function
>> prpoperly
>>
>> raku -I. -c WinMount.pm6
>>
>>  }
>>
>>  use NativeCall;
>>  use WinPopUps :WinMsg;
>>
>>
>>  # Reference to types and values:
>> http://dsource.org/projects/tango/ticket/820
>>
>>  constant BYTE:= uint8;
>>  constant WCHAR   := uint16;
>>  constant DWORD   := int32;
>>  constant REGSAM  := int32;
>>  constant WCHARS  := CArray[WCHAR];
>>  constant BYTES   := CArray[BYTE];
>>
>>  constant HKEY_CURRENT_USER  = 0x8001;
>>  constant HKEY_LOCAL_MACHINE = 0x8002;
>>  constant KEY_QUERY_VALUE   = 1;
>>  constant ERROR_SUCCESS = 0; # Yeah, I know. The Win-Api
>> uses 0 for success and other values to indicate errors
>>  constant REG_SZ = 1;
>>
>>  constant KEY_READ  = 0x20019;
>>  constant KEY_SET_VALUE = 0x0002;
>>  constant REG_DWORD = 0x0004;
>>
>>
>>
>>  sub to-c-str( Str $str ) returns CArray[WCHAR]  is export(
>> :to-c-str ) {
>> my @str := CArray[WCHAR].new;
>> for ( $str.comb ).kv -> $i, $char { @str[$i] = $char.ord; }
>> @str[ $str.chars ] = 0;
>> @str;
>>  }
>>
>>
>>  sub wstr( Str $str ) returns WCHARS  is export( :wstr ) {
>>  CArray[WCHAR].new( $str.comb.map: *.ord )
>>  }
>>
>>
>>  sub GetLUA() is export( :GetLUA ) {
>>
>>  #`{
>>
>>  Returns the LUA value in the registry to True (0x0001) or
>> False (0x)
>>
>>
>>
>> [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
>>"EnableLUA"=dword:
>>
>>  https://docs.perl6.org/language/nativecall
>>
>>  }
>>
>> my Str $SubName = &?ROUTINE.name;
>> my Str $OS  = $*KERNEL.name;
>> if not $OS eq "win32" { say "Sorry, $SubName only work in
>> Windows."; exit; }
>>
>> my Bool  $LUA = True;
>> my   $RtnCode;
>>
>> my Str   $SubKey =
>> '\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System';
>> my Str   $Key = $SubKey ~ '\EnableLUA';
>>
>> my   $lpSubKey= wstr( $SubKey );
>> my   $lpValueName = wstr( $Key );
>> # my $lpSubKey= CArray[uint8].new($Key.encode.list);
>> # my $lpValueName = CArray[uint8].new($SubKey.encode.list);
>>
>>
>> my int32 $Handle;
>> my int32 $ulOptions = 0;
>> my int32 $lpData;
>> my int32 $lpcbData;
>>
>>
>>  #`{
>>  Open the key:
>>
>>
>> https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regopenkeyexw
>>
>>
>> https://docs.microsoft.com/en-us/windows/win32/sysinfo/registry-key-security-and-access-rights
>>  C++
>>  LSTATUS RegOpenKeyExW(
>> HKEYhKey,  # Hive name (HKEY_LOCAL_MACHINE)
>> LPCWSTR