Shoutbox

RegWrite Causing Errors? - 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: RegWrite Causing Errors? (/showthread.php?tid=66233)

RegWrite Causing Errors? by Spunky on 09-14-2006 at 04:28 PM

quote:
Originally posted by The Debug Window
Error: .
       Line: 135. Code: -2147418113.
Function OnEvent_Initialize returned an error. Code: -2147352567

line 135 is
code:
WriteRegistry("OldPSM",Messenger.MyPersonalMessage);

and the WriteRegistry function is
code:
function WriteRegistry(key, value){
Debug.Trace("WRITE REGISTRY - "+key + ": "+value);
return new ActiveXObject("WScript.Shell").RegWrite(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key, value, "REG_SZ");
}


Any ideas why I'm getting the error? Am I missing something really stupid?

PS: This scripts used to work, so I don't know why it's gone funny :s

EDIT: Also, this only happens the first time the script is run once WLM opens... If you disbale it and then enable it again it works properly
RE: RegWrite Causing Errors? by Matti on 09-14-2006 at 04:39 PM

This is caused since Messenger.MyEmail is undefined. It works when you re-enable it since you're logged in and the email is set. Therefore, try this:

code:
function OnEvent_Initialize(MessengerStart) {
   if(MessengerStart && Messenger.MyStatus > 1) {
       OnEvent_SigninReady(Messenger.MyEmail); //Call the event like a normal function
   }
}
function OnEvent_SigninReady(Email) {
   //Original code for OnEvent_Initialize goes here
}
This will make the function do what it has to do when the signin is ready, and when it's initialized if you're signed in. ;)
RE: RegWrite Causing Errors? by R4000 on 09-14-2006 at 05:04 PM

I suggest reg functions like:

code:
function WriteRegistry(key, value)
{
    var Shell = new ActiveXObject("WScript.Shell");
    try {
        return Shell.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key, value);
    } catch(e) {
        return false;
    }
}


function ReadRegistry(key)
{
    var Shell = new ActiveXObject("WScript.Shell");
    try {
        return Shell.RegRead(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key);
    } catch(e) {
        return false;
    }
}

function RemoveRegistry(key)
{
     var Shell = new ActiveXObject("Wscript.Shell");
    try {     
        return Shell.RegDelete(MsgPlus.ScriptRegPath + Messenger.MyEmail + "\\" + key);
    } catch(e) {
        return false;
    }
}

then you have error catching if the key doesnt exist and you try to read it or w/e
(its wierd but i try to read b4 i write :P)
RE: RegWrite Causing Errors? by Spunky on 09-14-2006 at 05:12 PM

Thankyou both. Just changed the code and I'm testing it out and I've sent it to a few others to try, but it seems to work (apart from still causing an OnEvent_Initialize error in the debug window)

EDIT: Thought it was working, but it still throws an error at the same line, meaning I can't read any of the settings for the script... It only starts working when it's re-renabled... Any ideas?


RE: RegWrite Causing Errors? by phalanxii on 09-15-2006 at 12:02 AM

Mattike has got the right idea, but if I'm not mistaken, shouldn't it be:

code:
function OnEvent_Initialize(MessengerStart) {
   if(!MessengerStart && Messenger.MyStatus > 1) {
       OnEvent_SigninReady(Messenger.MyEmail); //Call the event like a normal function
   }
}
function OnEvent_SigninReady(Email) {
   //Original code for OnEvent_Initialize goes here
}
That way, OnEvent_SigninReady executes when a) the script is enabled when Messenger is signed in and b) when you sign in to Messenger. Also, if you don't need any details from contacts, you can use OnEvent_Signin instead, as this will give the same result, but a lot earlier.
RE: RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:16 AM

quote:
Originally posted by Mattike
code:
if(MessengerStart && Messenger.MyStatus > 1)

As phalanxii pointed out, this is wrong and can never become true since if MessengerStart is true, Messenger.MyStatus will always be 0.

However...
quote:
Originally posted by phalanxii
but if I'm not mistaken, shouldn't it be:
code:
function OnEvent_Initialize(MessengerStart) {
   if(!MessengerStart && Messenger.MyStatus > 1) {
       OnEvent_SigninReady(Messenger.MyEmail); //Call the event like a normal function
   }
}
function OnEvent_SigninReady(Email) {
   //Original code for OnEvent_Initialize goes here
}
That way, OnEvent_SigninReady executes when a) the script is enabled when Messenger is signed in and b) when you sign in to Messenger. Also, if you don't need any details from contacts, you can use OnEvent_Signin instead, as this will give the same result, but a lot earlier.

That's not correct either though. The stuff in SigninReady will not be executed when Messenger is started the first time...

And if you check on Messenger.MyStatus you do not need to check on MessengerStart too.

Because if Messenger is started the first time, your status (and email) can not be something as you still need to sign in.

If the script is restarted (and thus MessengerStart is false) the important thing is again your status. If that is 0 (not signed in), Messenger.MyEmail will not be defined. If you are signed in, Messenger.MyEmail will be defined.

So, the proper code you want is:
code:
function OnEvent_Initialize(MessengerStart) {
   if(Messenger.MyStatus > 0) {
       OnEvent_SigninReady(Messenger.MyEmail);
   }
}
function OnEvent_SigninReady(Email) {
   //Original code for OnEvent_Initialize goes here
}
1) you start messenger thus script is started for the first time:
  OnEvent_Initialize is called
    MessengerStart is true
    Messenger.MyStatus is 0
    Messenger.MyEmail is undefined

2) you restart the script:
  OnEvent_Initialize is called
    MessengerStart is false
    Messenger.MyStatus is 0
    Messenger.MyEmail is undefined

3) you sign in:
  OnEvent_Signin(Ready) is called
    Messenger.MyStatus is >0
    Messenger.MyEmail is defined

4) you restart the script:
  OnEvent_Initialize is called
    MessengerStart is false
    Messenger.MyStatus is >0
    Messenger.MyEmail is defined

5) you sign out and then you restart the script
  see 2
RE: RegWrite Causing Errors? by phalanxii on 09-15-2006 at 01:14 PM

Ah yeah, sorry about that. I forgot about the status being 0 when you're not signed (so the code isn't executed). What I actually meant was:

code:
function OnEvent_Initialize(MessengerStart) {
   if(!MessengerStart) {
       OnEvent_SigninReady(Messenger.MyEmail); //Call the event like a normal function
   }
}
function OnEvent_SigninReady(Email) {
   //Original code for OnEvent_Initialize goes here
}
Cookie's code works exactly the same, so it doesn't really matter. :)
RE: RegWrite Causing Errors? by Matti on 09-15-2006 at 01:17 PM

quote:
Originally posted by phalanxii
Mattike has got the right idea, but if I'm not mistaken, shouldn't it be:
code:
...

Woops! You're right. :$

quote:
Originally posted by phalanxii
I don't think my code is wrong either *-)... although your code works just as well.
But what you forget is:

4. You have imported the script while you are signed out:
   OnEvent_Initialize is called.
   MessengerStart is false, so !MessengerStart is true.
   The if statement in OnEvent_Initialize is executed.
   OnEvent_Signin is called.
   Messenger.MyEmail is undefined (you are signed out).
     TUUT! Error!

So, the only way it should work is by using both !MessengerStart and Messenger.MyStatus > 0. ;)

quote:
Originally posted by phalanxii
I would also use OnEvent_Signin instead of OnEvent_SigninReady because it is called a lot earlier. Though OnEvent_Signin is called when not all information is available, for SpunkyLoveMuff's purpose of writing settings into the registry, contact information is not required (if it is, then use OnEvent_SigninReady ;)).
You can, it just depends on how urgent the keys should be imported. :)
RE: RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:03 PM

quote:
Originally posted by phalanxii
Ah yeah, sorry about that. I forgot about the status being 0 when you're not signed (so the code isn't executed). What I actually meant was:
code:
function OnEvent_Initialize(MessengerStart) {
   if(!MessengerStart) {
       OnEvent_SigninReady(Messenger.MyEmail); //Call the event like a normal function
   }
}
function OnEvent_SigninReady(Email) {
   //Original code for OnEvent_Initialize goes here
}
Cookie's code works exactly the same, so it doesn't really matter. :)
no it isn't the same and doesn't work the same....

Open Messenger, before signing in, restart the script. You'll see that with your method (if(!MessengerStart)) your script will end in an error, while mine (if(Messenger.MyStatus > 0)) doesn't.

quote:
Originally posted by Mattike
(...)
So, the only way it should work is by using both !MessengerStart and Messenger.MyStatus > 0. ;)
!MessengerStart isn't needed at all... see my previous post which explains all the possible events in a pseudo boolean algebra truth table. Look at the sequence of events and look at the states of things, you'll see that to have a defined email, the only thing to check is the status being >0.

many script make this error (in fact I think I have made some scripts myself with that very same error... :/... me goes to check)...

RE: RegWrite Causing Errors? by Spunky on 09-15-2006 at 10:18 PM

Thanks for the help guys. Cookie's solution appears to have worked :D

I'm reading a comma seperated list from the registry and reading it into an array and then placing it into a List View box thingy :p  This needs to be done ASAP as the window is opened when the script starts... (it then gets hidden until it's needed)

Like I said, if(Messenger.MyStatus > 0) seems to work for what I want at the moment... Thanks :)


RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:20 PM

no problem...

PS: do not use "Messenger.MyEmail" to store info in the registry, use "Messenger.MyUserId" instead... security reasons!

PS2: anyways

quote:
Originally posted by SpunkyLoveMuff
I'm reading a comma seperated list from the registry and reading it into an array and then placing it into a List View box thingy :p  This needs to be done ASAP as the window is opened when the script starts... (it then gets hidden until it's needed)
Why don't you create the window and fill in the listview when it is actually needed for the first time? In that way you don't slow other things down...

Unless you constantly need to add to the listview also... but then again, you also could simply store everything in an array and only create the listview (from that array) when it actually needs to be shown to the user. This will be way faster...not only in loading but in general executing too (as updating a listview takes a lot of processing).
RE: RegWrite Causing Errors? by Spunky on 09-15-2006 at 10:22 PM

I should go back and edit the Read/Write functions for that then as I used ones I found on the forums :p

I did see you mention that in another thread, but totally forgot about editing scripts I've made already...


RE: RE: RE: RegWrite Causing Errors? by phalanxii on 09-15-2006 at 10:25 PM

quote:
Originally posted by Mattike
4. You have imported the script while you are signed out:
   OnEvent_Initialize is called.
   MessengerStart is false, so !MessengerStart is true.
   The if statement in OnEvent_Initialize is executed.
   OnEvent_Signin is called.
   Messenger.MyEmail is undefined (you are signed out).
     TUUT! Error!
quote:
Originally posted by CookieRevised
Open Messenger, before signing in, restart the script. You'll see that with your method (if(!MessengerStart)) your script will end in an error, while mine (if(Messenger.MyStatus > 0)) doesn't.

Yes, true, that theoretically the script is different if !MessengerStart is used, but practically, there is no difference. Why? Because you can only import/restart/enable a script when a) WLM is open and b) you are signed in. Therefore, Cookie's scenarios 2 and 5 (and Mattike's scenario 4) don't exist and there is no error.

Try it for yourself: open WLM (don't sign in) and try to import a script. For me, Messenger Plus! Live sends an error box saying:
code:
---------------------------
Messenger Plus! Live
---------------------------
You must be signed in before you can import new scripts.
---------------------------
OK   
---------------------------
As for enabling when you are signed out: open WLM (don't sign in) and try to access the MPL preferences. For me, I get this error:
code:
---------------------------
Messenger Plus! Live
---------------------------
You must be signed-in to get access to your preferences.
---------------------------
OK   
---------------------------
Please tell me if I'm wrong about this, because I'm one of those many people who use that method in their scripts. :$
RE: RegWrite Causing Errors? by Spunky on 09-15-2006 at 10:28 PM

scripts can start before you are signed in from what I've noticed, it doesnt matter if you can't import them when not signed in...


RE: RE: RE: RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:29 PM

quote:
Originally posted by phalanxii
quote:
Originally posted by CookieRevised
Open Messenger, before signing in, restart the script. You'll see that with your method (if(!MessengerStart)) your script will end in an error, while mine (if(Messenger.MyStatus > 0)) doesn't.

Yes, true, that theoretically the script is different if !MessengerStart is used, but practically, there is no difference. Why? Because you can only import/restart/enable a script when a) WLM is open and b) you are signed in. Therefore, Cookie's scenarios 2 and 5 (and Mattike's scenario 4) don't exist and there is no error.
(...)
Please tell me if I'm wrong about this, because I'm one of those many people who use that method in their scripts. :$
You're wrong :p... They do exist in practice too... As I said, open messenger, sign in, sign back out and you can restart any script you want!

If it was like you said, then a script wouldn't continue to function either after the OnEvent_SignOut event... Or couldn't detect OnEvent Initialize(true) when Messenger is first started (as you aren't signed in yet), etc...

Forget about importing scripts, that doesn't matter as that is only a subset of cases which are possible (in particular you can only import a script when you are in case 3 in my list, and when the script is imported, case 4 will happen).
RE: RegWrite Causing Errors? by phalanxii on 09-15-2006 at 10:33 PM

Oh crap, that's devious...

I just figured it out:

1. Sign in.
2. Open your preferences.
3. Sign out.
4. Change your script preferences...

Is that a bug or what? I guess I'll need to fix all my scripts! :S (Sorry everybody, you were all right! :))

EDIT: And what I meant by "you can only import/restart/enable a script when a) WLM is open and b) you are signed in" was that the script can only be MANUALLY started when you are signed in. The script automatically starts when WLM opens if it has already been manually enabled (by import/restart/enable) and continues to run until WLM closes (unless it is disabled). The thing that caught me was that I thought scripts could not be manually started when signed out...


RE: RegWrite Causing Errors? by Spunky on 09-15-2006 at 10:34 PM

quote:
Originally posted by CookieRevised
Why don't you create the window and fill in the listview when it is actually needed for the first time? In that way you don't slow other things down...

Unless you constantly need to add to the listview also... but then again, you also could simply store everything in an array and only create the listview (from that array) when it actually needs to be shown to the user. This will be way faster...not only in loading but in general executing too (as updating a listview takes a lot of processing).

Basically whats happening is, the array is loaded in the list box and then, when a certain function is called, a loop searches to see if a variable is equal to the current value in the 1st column... if it is, the variable is replaced by the current value in the second column and passed back...

it's basically replacing email addresses with a user defined nickname so that my script can show who the user is talking to, without giving away they're email address...

As the function that performs this is called quite frequently, it's not a very good idea to read from the registry at that point. So it's easier to read the registry at startup and then it updates as it goes along...
RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:37 PM

quote:
Originally posted by SpunkyLoveMuff
quote:
Originally posted by CookieRevised
Why don't you create the window and fill in the listview when it is actually needed for the first time? In that way you don't slow other things down...

Unless you constantly need to add to the listview also... but then again, you also could simply store everything in an array and only create the listview (from that array) when it actually needs to be shown to the user. This will be way faster...not only in loading but in general executing too (as updating a listview takes a lot of processing).

Basically whats happening is, the array is loaded in the list box and then, when a certain function is called, a loop searches to see if a variable is equal to the current value in the 1st column... if it is, the variable is replaced by the current value in the second column and passed back...

it's basically replacing email addresses with a user defined nickname so that my script can show who the user is talking to, without giving away they're email address...

As the function that performs this is called quite frequently, it's not a very good idea to read from the registry at that point. So it's easier to read the registry at startup and then it updates as it goes along...
It is even a worse idea to use a listview for that. Reading from the registry would actually be faster than updating a listview....

A way faster method is reading the stuff you need from the registry, and from that point on using an array (or a multiple of arrays) to do what you need to do. Forget about the listview totally as that is not needed (except for maybe showing the user something; but certainly not for the updating/computing/replacing/etc)...

Reading from the registry is 1 api call, updating a listview is a multitude of api-calls from memory calculations to graphic updates and rendering and what not.
RE: RegWrite Causing Errors? by Spunky on 09-15-2006 at 10:40 PM

The list view does serve a purpose and is not just there for my benefit... It allows the user to see the nicknames they've entered as well as remove entries and hopefully, soon I'll be able to put a "modify" button as well.

One thing that does concern me is about a character limit in the registry value... Will I need to eventually just switch to a text file?


RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:43 PM

quote:
Originally posted by SpunkyLoveMuff
The list view does serve a purpose and is not just there for my benefit... It allows the user to see the nicknames they've entered as well as remove entries and hopefully, soon I'll be able to put a "modify" button as well.
yep, but 'updating' the listview should be done only when the user asks for it, aka: when he opens the window which contains that listview and want to see the listview.

When he closes the window again, your internal arrays should update with the info taken from the listview and the script should continue to use those arrays until the next time the user opens the window. Only then you update the listview again, etc...

And when the user signs out, that's the time to write back all the stuff to the registry...

All the updates you do in the background while the window is hidden aren't needed and will slow things majorly down.

quote:
Originally posted by SpunkyLoveMuff
One thing that does concern me is about a character limit in the registry value... Will I need to eventually just switch to a text file?
no worries I'd say... The character limit of a REG_SZ registry value is way bigger than what you'll ever need for a nickname or something though.

;)
RE: RegWrite Causing Errors? by Spunky on 09-15-2006 at 10:52 PM

quote:
Originally posted by CookieRevised
quote:

Originally posted by SpunkyLoveMuff
One thing that does concern me is about a character limit in the registry value... Will I need to eventually just switch to a text file?


no worries I'd say... The character limit of a REG_SZ registry value is way bigger than what you'll ever need for a nickname or something though.

Well, I'm using one REG_SZ for EVERY email and nickname, which I'm guessing is gonna be a problem at some stage :p

RE: RegWrite Causing Errors? by CookieRevised on 09-15-2006 at 10:57 PM

quote:
Originally posted by SpunkyLoveMuff

Well, I'm using one REG_SZ for EVERY email and nickname, which I'm guessing is gonna be a problem at some stage :p
No it isn't...

Size limit of a registry value name

In Windows 95/98/ME, the size limit for a value name is 255 characters
In Windows XP this can be up to 16383 characters long.
In Windows 2000, a maximum of 260 Ansi or 16383 Unicode characters may be used in the name of a value.

Size limit of value data

In Windows 95/98/ME, the limit for all types is either 16300 bytes or 64Kb (depends on what source... and I haven't tested this).
In Windows NT/2000/XP, there is no limit as long as your memory can handle it. Except for REG_BINARY and REG_MULTI_SZ values which can 'only' hold 64Kb of data. However, the bigger the size the slower it is to read of course.

Microsoft recommends that data which is more than 2048 bytes big should be stored in a file and only be called from the registry when it is absolutely needed.

Note that these sizes are internal limits. Programs are often way more restricted. eg: Windows regeditor forces smaller sizes. eg: in Windows XP, the name of a registry subkey can only be 209 characters long, the name of a value can only be 259 characters long  (the value itself can still be almost 64000 bytes though). A common size limit which is often (wrongfully!) forced by programs is 1024 bytes.


Anyways, you really shouldn't be worried about registry size limits in your case. However you should be worried about the slowness of updating that listview at some stage with such sizes (I know I keep getting back on this, but it is kind-of important if you want a fast working script and not slowing other things (other scripts) down).

;)

note: figures provided by different sources (which I can't remember). So there is a slight chance they may not be absolute. Though if they aren't totally correct, they are certainly extremely close.


-----------

PSS: don't store the emails of the contacts in the registry either for the same reason why you shouldn't use the user's email, but rather his userid.

For other emails than the user's email, you can calculate the userid by using the msnID function as described here.

(btw, this is also the same as what Plus! does, and for the same reason. It doesn't store the emails of contacts in the registry either, it uses a hash to store them)