Shoutbox

Help - accessing the registry - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Help - accessing the registry (/showthread.php?tid=66570)

Help - accessing the registry by Choli on 09-23-2006 at 06:53 PM

I don't understand why the following code fails when I execute it in a script (of Plus, of course) and the same code works perfectly in Visual Basic (typing it using VB syntax, of course).

I'm trying to access the registry, so first of all, I have to open it, using RegOpenKeyEx, but it fails....

code:
var HKEY_CURRENT_USER = 0x80000001;
var KEY_ALL_ACCESS = 0xF003F;

var ret;
var key = Interop.Allocate(4);

var x = Interop.Allocate(10000);

ret = Interop.Call("advapi32.dll", "RegOpenKeyExW", HKEY_CURRENT_USER, "Software\\Patchou\\Messenger Plus! Live", 0, KEY_ALL_ACCESS, key);

if (ret != 0) {
    Debug.Trace("" + ret + " " + Interop.GetLastError());
    Interop.Call("kernel32.dll", "FormatMessageW", 0x00001000, 0, ret, 0, x, 1000, 0);
    Debug.Trace(x.ReadString(0));
}

The output in the debug window is
quote:
2 0
El sistema no puede hallar el archivo especificado.

That means, in english: "The system can't find the specified file".

Seeing the "2 0", I understand that the function RegOpenKeyEx fails, but GetLastError returns 0, ie: no error.

So it seems that it can't find the path "Software\Patchou\Messenger Plus! Live" under the HKCU branch of the registry. However, if I put only "Software", it opens it correctly. That, together with the fact that I can open it using VB, makes me think that Plus Live executes its scripts under another Windows account, therefore accessing another HKCU branch where there is no "Patchou" "folder". I know that's weird and stupid, but what else can it be?

Any ideas?

Thanks.
RE: Help - accessing the registry by CookieRevised on 09-23-2006 at 07:12 PM

You just experienced a (known) Plus! bug ;) :

1) Plus! can't handle numbers larger than 0x80000000. (this should be fixed in next update). In fact, every interop function of Plus! requires SIGNED numbers instead of UNSIGNED numbers.

eg: ReadWord(), ReadDWord(), WriteDWord(), etc all require signed numbers.

so, instead of:

code:
var HKEY_CURRENT_USER = 0x80000001;
which will return the unsigned number 2147483649, use this:
code:
var HKEY_CURRENT_USER = 0x80000000 | 1;
which will return the equivalent, but signed, number -2147483647...

[edit]
or:
code:
var HKEY_CURRENT_USER = 0x80000001 & 0xFFFFFFFF;
[/edit]


The reason why certain keys do work is because Plus! interprets that provided unsigned number as a signed one (thus wrongly) and thus you actually open the key under a different main hive, namely HKEY_CLASSES_ROOT in this case (which does has a "Software" subkey).




2)
quote:
Return Values
If the function succeeds, the return value is ERROR_SUCCESS.

If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
Thus those registry APIs don't use GetLastError, they return the error directly as the return value...

However, I must say Interop.GetLastError always returns 0 with me too with certain APIs (APIs which do provide the error in GetLastError).
RE: Help - accessing the registry by Choli on 09-23-2006 at 07:30 PM

EDIT:
It seems that my Plus still doesn't handle numbers bigger than 0x80000000 correctly. What Cookie sugggested me, works correctly, as well as defining the HKCU constant as -2147483647. Let's hope Patchou definitelly fixes for the next release :)

EDIT END.

Original post:


quote:
Originally posted by CookieRevised
You just experienced a (known) Plus! bug  :

1) Plus! can't handle numbers larger than 0x80000000. (this should be fixed in next update). so, use this instead:
err... well :P I forgot to say that I'm using a beta version which doesn't have that bug ;)

Also, if I open "HKCU\Software", it works correctly... the problem is while trying to open "HKCU\Software\Patchou\Messenger Plus! Live".
quote:
Originally posted by CookieRevised
Thus those registry APIs don't use GetLastError, they return the error directly as the return value
yeah, well, maybe i shouldn't have used GetLastError. Anyway, ERROR_SUCCESS is 0 and my RegOpenKeyEx returns 2, which passed through FormatMessage gives me the error: "The system can't find the specified file".

Maybe I should have reported this in the beta forum... but i don't think it's a problem with the beta