Subj : Re: Problem with getProperty hook To : netscape.public.mozilla.jseng From : Jamie Nordmeyer Date : Thu Mar 24 2005 08:53 am This is a multi-part message in MIME format. ------=_NextPart_000_000F_01C5304F.0035C280 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Oh. Never mind. Figured out on my own. On the call to = JS_DefineObject, I should be passing proto to argument number 5, not = NULL. Works like a charm now. "Jamie Nordmeyer" wrote in message = news:d1uq82$5kg1@ripley.netscape.com... Hi all. I have the following code in a class file: /////////////////////////////////////////////////////////// // File: Directories.cpp=20 // Description: Implements the Directories class and jsDirectories = class.=20 ///////////////////////////////////////////////////////////=20 #include =20 #include =20 #include "Directories.h"=20 // Directories implementation=20 void Directories::get_CommonAppData(char* dir)=20 { HKEY hKey =3D NULL; DWORD type, size =3D 512; char temp[512]; RegOpenKeyEx(HKEY_LOCAL_MACHINE, = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", = 0, KEY_QUERY_VALUE, &hKey); RegQueryValueEx(hKey, "Common AppData", 0, &type, (LPBYTE)temp, = &size); strcpy(dir, temp); RegCloseKey(hKey); } // jsDirectories implementation=20 static JSClass Directories_class =3D { "Directories", JSCLASS_HAS_PRIVATE, JS_PropertyStub, JS_PropertyStub, jsDirectories::GetProperty, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, jsDirectories::Finalize, JSCLASS_NO_OPTIONAL_MEMBERS }; JSObject* jsDirectories::proto =3D NULL; JSPropertySpec jsDirectories::properties [] =3D { {"CommonAppData", PROP_COMMONAPPDATA, JSPROP_READONLY}, {0} }; JSBool jsDirectories::Constructor(JSContext* cx, JSObject* obj, uintN = argC, jsval* argv, jsval* rval) { return JS_SetPrivate(cx, obj, (void*)new Directories()); } void jsDirectories::Finalize(JSContext* cx, JSObject* obj)=20 {=20 assert(JS_GET_CLASS(cx, obj) =3D=3D &Directories_class);=20 Directories* dir =3D static_cast(JS_GetPrivate(cx, = obj));=20 if (dir) delete dir;=20 } JSBool jsDirectories::GetProperty(JSContext* cx, JSObject* obj, jsval = id, jsval* vp)=20 {=20 if (JSVAL_IS_INT(id))=20 {=20 Directories* dir =3D = static_cast(JS_GetPrivate(cx, obj));=20 char buffer[512];=20 JSString* jsbuffer;=20 switch(JSVAL_TO_INT(id))=20 {=20 case PROP_COMMONAPPDATA:=20 dir->get_CommonAppData(buffer);=20 jsbuffer =3D JS_NewStringCopyZ(cx, buffer);=20 *vp =3D STRING_TO_JSVAL(jsbuffer); break;=20 }=20 }=20 return JS_TRUE;=20 } JSBool jsDirectories::Init(JSContext* cx, JSObject* obj)=20 {=20 if (!(proto =3D JS_InitClass(cx, obj, NULL, &Directories_class, = Constructor, 0, properties, NULL, NULL, NULL)))=20 return JS_FALSE;=20 JS_DefineObject(cx, obj, "Directories", &Directories_class, NULL, = JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT); return JS_TRUE;=20 } I also have a similar class called Environment. In my main.cpp file, = I'm using the following code: jsEnvironment::Init(context, global); jsDirectories::Init(context, jsEnvironment::proto); = //jsEnvironment::proto is a global object of the jsEnvironment class My question is this: When I try to execute the following script, var = x =3D Environment.Directories.CommonAppData;, x is set to undefined. = Debugging the code, the id parameter of the GetProperty hook is not the = right number for PROP_COMMONAPPDATA. It should be 21, and is coming = back as something like 310445. Any ideas? Thanks in advance. ------=_NextPart_000_000F_01C5304F.0035C280 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Oh.  Never mind.  Figured out = on my=20 own.  On the call to JS_DefineObject, I should be passing proto to = argument=20 number 5, not NULL.  Works like a charm now.
"Jamie Nordmeyer" <nordyj2001@yahoo.com> = wrote in=20 message news:d1uq82$5kg1@ripley.net= scape.com...
Hi all.  I have the following code in a class file:
 
///////////////////////////////////////////////////////////
// File: = Directories.cpp=20
// Description: = Implements the=20 Directories class and jsDirectories class.
///////////////////////////////////////////////////////////
 
#include <windows.h>=20
#include <assert.h>=20
#include "Directories.h"=20
 
// Directories implementation
 
void = Directories::get_CommonAppData(char* dir)
{
    HKEY hKey =3D NULL;
    DWORD type, size =3D 512;
    char = temp[512];
 
    RegOpenKeyEx(HKEY_LOCAL_MACHINE,=20 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell = Folders", 0,=20 KEY_QUERY_VALUE, &hKey);
    RegQueryValueEx(hKey, "Common AppData", 0, = &type,=20 (LPBYTE)temp, &size);
    strcpy(dir, temp);
    RegCloseKey(hKey);
}
 
// jsDirectories implementation
 
static JSClass Directories_class =3D = {
    "Directories", JSCLASS_HAS_PRIVATE,
    JS_PropertyStub, JS_PropertyStub,
    jsDirectories::GetProperty, = JS_PropertyStub,
    JS_EnumerateStub, JS_ResolveStub,
    JS_ConvertStub, jsDirectories::Finalize,
    JSCLASS_NO_OPTIONAL_MEMBERS
};
 
JSObject* jsDirectories::proto =3D NULL;
 
JSPropertySpec jsDirectories::properties [] =3D {
    {"CommonAppData", PROP_COMMONAPPDATA,=20 JSPROP_READONLY},
    {0}
};
 
JSBool jsDirectories::Constructor(JSContext* cx, JSObject* obj, = uintN=20 argC, jsval* argv, jsval* rval)
{
    return = JS_SetPrivate(cx,=20 obj, (void*)new=20 Directories());
}
 
void = jsDirectories::Finalize(JSContext* cx,=20 JSObject* obj)
{
    assert(JS_GET_CLASS(cx, obj) =3D=3D=20 &Directories_class);
    Directories* dir =3D static_cast<Directories*>(JS_GetPrivate(cx, = obj));=20
    if (dir) delete dir;
}
 
JSBool jsDirectories::GetProperty(JSContext* cx, JSObject* obj, = jsval id,=20 jsval* vp)
{
    if = (JSVAL_IS_INT(id))=20
    {
        Directories* dir =3D static_cast<Directories*>(JS_GetPrivate(cx, = obj));=20
        = char=20 buffer[512];
        JSString* jsbuffer;
 
       =20 switch(JSVAL_TO_INT(id))
        {
       =20     case PROP_COMMONAPPDATA:
           =20     dir->get_CommonAppData(buffer);
           =20     jsbuffer =3D JS_NewStringCopyZ(cx, buffer);
           =20     *vp =3D STRING_TO_JSVAL(jsbuffer); break;
        }
    }
    return JS_TRUE; =
}
 
JSBool jsDirectories::Init(JSContext* cx, JSObject* obj)
{
    if (!(proto =3D=20 JS_InitClass(cx, obj, NULL, &Directories_class, Constructor, 0,=20 properties, NULL, NULL, NULL)))
        = return=20 JS_FALSE;
    JS_DefineObject(cx, obj, "Directories",=20 &Directories_class, NULL,=20 JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT);
    return JS_TRUE;
}
I also have a similar class called=20 Environment.  In my main.cpp file, I'm using the following=20 code:
 
jsEnvironment::Init(context, global);
jsDirectories::Init(context, jsEnvironment::proto); =20 //jsEnvironment::proto is a global object of the jsEnvironment = class
 
My question is this:  When I try to execute the following = script,=20 var x =3D Environment.Directories.CommonAppData;, x is = set to=20 undefined.  Debugging the code, the id parameter of the = GetProperty hook=20 is not the right number for PROP_COMMONAPPDATA.  It should be 21, = and is=20 coming back as something like 310445.
 
Any=20 ideas?
 
Thanks in=20 advance.
------=_NextPart_000_000F_01C5304F.0035C280-- .