Jump to content

Structures in JScript


Kip
 Share

Recommended Posts

Hi, I've found a VBS script somewhere that can create structures, and pass it with dll calls.

And it works, that's not the problem.

But when I try to convert that script to JScript (or Javascript, I don't think that matters in this case) I just can't get it working.

Original: (VBS)

Set Move=CreateObject("DynamicWrapper")
 Move.Register "User32.dll","GetCursorPos","f=s","i=l","r=l"
 
     Set POINT=New Struct
     
     With POINT
         .Add "X", 4, 0
         .Add "Y", 4, 0
     End With
 
 
 Move.GetCursorPos(POINT.Ptr)
 
 
 WScript.Echo(POINT.GetItem("X") & vbCrLf & POINT.GetItem("Y"))
 
 
 
 Class Struct ' v1.1 allow typedef with dynawrap calls
 
     Public Property Get Ptr '******************************* Property Ptr
     Ptr=GetBSTRPtr(sBuf)
     End Property
     
         Private oMM,oSCat,oAnWi 'objets wrapper API
     Private dBuf,sBuf,iOffset
     
     Public Sub Add(sItem,iSize,Data) '********************** Method Add
     
         Dim lVSize,iD
         iD="0"
         
         lVSize = iSize
         
         dBuf.Add sItem,lVSize
         sBuf=sBuf & String(lVSize/2+1,Chr(0))
         SetDataBSTR GetBSTRPtr(sBuf),lVSize,Data,iOffset
         
     End Sub
     
     
     Public Function GetItem(sItem)
         '********************************************** Méthode GetItem
         Dim lOf,lSi,aItems,aKeys,i
         
         If dBuf.Exists(sItem) then
         
             lSi=CLng(dBuf.Item(sItem))
             aKeys=dBuf.Keys
             aItems=dBuf.Items
             lOf=0
             
             For i=0 To dBuf.Count-1
                 If aKeys(i)=sItem Then Exit For
                 lOf=lOf+aItems(i)
             Next
             
             GetItem=GetDataBSTR(Ptr,lSi,lOf)
             
         Else
             GetItem=""
             err.raise 10000,"Method GetItem","The item " & sItem & "don't exist"
         End If
         
     End Function
     
     Public Function GetBSTRPtr(ByRef sData)
         'retun the TRUE address (variant long) of the sData string BSTR
         
         Dim pSource
         Dim pDest
         
         If VarType(sData)<>vbString Then 'little check
             GetBSTRPtr=0
             err.raise 10000, "GetBSTRPtr", "The variable is not a string"
             Exit Function
         End If
         
         pSource=oSCat.lstrcat(sData,"") 'trick to return sData pointer
         pDest=oSCat.lstrcat(GetBSTRPtr,"") 'idem
         GetBSTRPtr=CLng(0) 'cast function variable
         'l'adresse du contenu réel de sBuf (4octets) écrase le contenu de la variable GetBSTPtr
         'les valeurs sont incrémentées de 8 octets pour tenir compte du Type Descriptor
         oMM.RtlMovememory pDest+8,pSource+8,4
         
     End Function
     
     
     '************************************************* *************************** IMPLEMENTATION
     
     
     Private Sub Class_Initialize 'Constructeur
         
         Set oMM=CreateObject("DynamicWrapper")
         oMM.Register "kernel32.dll","RtlMoveMemory","f=s","i=lll","r=l"
         
         Set oSCat=CreateObject("DynamicWrapper")
         oSCat.Register "kernel32.dll","lstrcat","f=s","i=ws","r=l"
         
         Set oAnWi=CreateObject("DynamicWrapper")
         oAnWi.Register "kernel32.dll","MultiByteToWideChar","f=s","i=llllll","r=l"
         
         Set dBuf=CreateObject("Scripting.Dictionary")
         
         sBuf=""
         iOffset=0
         
     End Sub
     
     Private Sub SetDataBSTR(lpData,iSize,Data,ByRef iOfs)
         'Place une valeur Data de taille iSize à l'adresse lpData+iOfs
         
         Dim lW,hW,xBuf
         
         Select Case iSize 'on commence par formater les valeurs numériques
             Case 1
                 lW=Data mod 256 'formatage 8 bits
                 xBuf=ChrB(lW)
                 
             Case 2 'if any
                 lW=Data mod 65536 'formatage 16 bits
                 xBuf=ChrW(lW) 'formatage little-endian
                 
             Case 4
                 hW=Fix(Data/65536)'high word
                 lW=Data mod 65536 'low word
                 xBuf=ChrW(lW) & ChrW(hW) 'formatage little-endian
                 
             Case Else 'bytes array, size iSize
                 xBuf=Data
                 
         End Select
         
         oMM.RtlMovememory lpData+iOfs,GetBSTRPtr(xBuf),iSize
         iOfs=iOfs+iSize 'maj l'offset
         
     End Sub
     
     
     Private Function GetDataBSTR(lpData,iSize,iOffset)
         'Read an iSize data to lpData+iOffset address
         
         Const CP_ACP=0 'code ANSI
         Dim pDest,tdOffset
         
         'valeurs pour les données numériques
         pDest=oSCat.lstrcat(GetDataBSTR,"")
         tdOffset=8
         
         Select Case iSize ' cast de la variable fonction
             Case 1
                 GetDataBSTR=CByte(0)
             Case 2
                 GetDataBSTR=CInt(0)
             Case 4
                 GetDataBSTR=CLng(0)
             Case Else 'a little bit more complicated with string data...
                 GetDataBSTR=String(iSize/2,Chr(0))
                 'la chaine variant BSTR stocke ses données ailleurs
                 pDest=GetBSTRPtr(GetDataBSTR)
                 tdOffset=0
         End Select
         
         
         'le contenu de la structure à l'offset iOffset écrase le contenu de la variable GetDataBSTR (tenir compte du TD)
         oMM.RtlMovememory pDest+tdOffset,lpData+iOffset,iSize
         
         if tdOffset=0 Then
         
             oAnWi.MultiByteToWideChar CP_ACP,0,lpData+iOffset,-1,pDest,iSize 'don't forget conversion Ansi->Wide
             
             GetDataBSTR=Replace(GetDataBSTR,Chr(0),"")
             'clean the trailer
         End If
         
     End Function
     
     
 End Class

And what i've tried so far:

function print(sString) {
    return WScript.StdOut.Write(sString);
}



POINT = new struct();
POINT.init();
POINT.Add("X", 4, 0);

while (1) {

}

function struct() {

this.oMM;
this.oSCat;
this.oAnWi;
this.dBuf;
this.sBuf;
this.iOffset;


this.Ptr = Ptr;
function Ptr() {
return GetBSTRPtr(this.sBuf);
}

this.init = init;
function init() {

this.oMM = new ActiveXObject("DynamicWrapper");
this.oMM.Register( "kernel32.dll","RtlMoveMemory","f=s","i=lll","r=l" );

this.oSCat = new ActiveXObject("DynamicWrapper");
this.oSCat.Register( "kernel32.dll","lstrcat","f=s","i=ws","r=l");

this.oAnWi = new ActiveXObject("DynamicWrapper");
this.oAnWi.Register( "kernel32.dll","MultiByteToWideChar","f=s","i=llllll","r=l");

this.dBuf = new ActiveXObject("Scripting.Dictionary");

this.sBuf = "";
this.iOffset = 0;

}

this.Add = Add;
function Add(sItem, iSize, Data) {

iD = "0";
lVSize = iSize;
this.dBuf.Add(sItem, lVSize);

for (i=1; !(i==(lVSize/2+1)+1); i+=1) {
this.sBuf += String.fromCharCode(0);
}

SetDataBSTR( GetBSTRPtr(this.sBuf), lVSize, Data, this.iOffset );
this.iOffset += lVSize;

}

this.GetBSTRPtr = GetBSTRPtr;
function GetBSTRPtr(sData) {

vEmpty = 0; // ???

//pSource = this.oSCat.lstrcat(sData,"");
//pDest = this.oSCat.lstrcat(vEmpty, "");

//this.oMM.RtlMovememory( pDest+8, pSource+8,4  );

//return pDest;

}


this.SetDataBSTR = SetDataBSTR;
function SetDataBSTR(lpData, iSize, Data, iOfs) {

var lW;
var hW;
var xBuf;

//if (iSize == 1) {

//  lW = Data % 256; // Modulus
//  sBuf = String.fromCharCode(lW);
//} else {
xBuf = Data;
//}

//this.oMM.RtlMovememory( lpData+iOfs,GetBSTRPtr(xBuf),iSize );

}

}

The comments are french so those aren't helping either.

Does someone know what I'm doing wrong?

Edit: DynaWrapper is here: http://www.autoitscript.com/forum/index.ph...st&p=688165

Edited by Kip
Link to comment
Share on other sites

Heres what I get for js but you may have to tinker with it. I didn't test it at all, just ran it through a converter.

Set Move= new ActiveXObject("DynamicWrapper");
Move.Register "User32.dll","GetCursorPos","f=s","i=l","r=l"

    POINT=New Struct;

     With POINT;
         .Add "X", 4, 0;
         .Add "Y", 4, 0;
     End With


Move.GetCursorPos(POINT.Ptr)


WScript.Echo(POINT.GetItem("X") + '\r\n' + POINT.GetItem("Y"))



Class Struct // v1.1 allow typedef with dynawrap calls

     Public Property Get Ptr //******************************* Property Ptr;
     Ptr=GetBSTRPtr(sBuf);
     End Property;

         Private oMM,oSCat,oAnWi //objets wrapper API;
     Private dBuf,sBuf,iOffset;

     Public Sub Add(sItem,iSize,Data) //********************** Method Add;

         var lVSize,iD;
         iD="0";

         lVSize = iSize;

         dBuf.Add sItem,lVSize;
         sBuf=sBuf + String(lVSize/2+1,Chr(0));
         SetDataBSTR GetBSTRPtr(sBuf),lVSize,Data,iOffset;

     }


     Public Function GetItem(sItem);
         //********************************************** Méthode GetItem;
         var lOf,lSi,aItems,aKeys,i;

         if(dBuf.Exists(sItem)){

             lSi=CLng(dBuf.Item(sItem));
             aKeys=dBuf.Keys;
             aItems=dBuf.Items;
             lOf=0;

             For i=0 To dBuf.Count-1;
                 if(aKeys(i)==sItem){ Exit For;
                 lOf=lOf+aItems(i);
             Next;

             GetItem=GetDataBSTR(Ptr,lSi,lOf);

         }else{
             GetItem="";
             err.raise 10000,"Method GetItem","The item " + sItem + "don't exist";
         }

     }

     Public Function GetBSTRPtr(ByRef sData);
         //retun the TRUE address (variant long) of the sData string BSTR;

         var pSource;
         var pDest;

         if(VarType(sData)!=8){ //little check;
             GetBSTRPtr=0;
             err.raise 10000, "GetBSTRPtr", "The variable is ! a string";
             Exit Function;
         }

         pSource=oSCat.lstrcat(sData,"") 'trick to return sData pointer;
         pDest=oSCat.lstrcat(GetBSTRPtr,"") 'idem;
         GetBSTRPtr=CLng(0) //cast function variable;
         //l'adresse du contenu réel de sBuf (4octets) écrase le contenu de la variable GetBSTPtr;
         //les valeurs sont incrémentées de 8 octets pour tenir compte du Type Descriptor;
         oMM.RtlMovememory pDest+8,pSource+8,4;

     }


     //************************************************* *************************** IMPLEMENTATION;


     Private Sub Class_Initialize //Constructeur;

        oMM= new ActiveXObject("DynamicWrapper");
         oMM.Register "kernel32.dll","RtlMoveMemory","f=s","i=lll","r=l";

        oSCat= new ActiveXObject("DynamicWrapper");
         oSCat.Register "kernel32.dll","lstrcat","f=s","i=ws","r=l";

        oAnWi= new ActiveXObject("DynamicWrapper");
         oAnWi.Register "kernel32.dll","MultiByteToWideChar","f=s","i=llllll","r=l";

        dBuf= new ActiveXObject("Scripting.Dictionary");

         sBuf="";
         iOffset=0;

     }

     Private Sub SetDataBSTR(lpData,iSize,Data,ByRef iOfs);
         //Place une valeur Data de taille iSize à l'adresse lpData+iOfs;

         var lW,hW,xBuf;

         switch(iSize //on commence par formater les valeurs numériques){
             Case 1;
                 lW=Data mod 256 //formatage 8 bits;
                 xBuf=ChrB(lW);

             Case 2 //if(any;
                 lW=Data mod 65536 //formatage 16 bits;
                 xBuf=ChrW(lW) //formatage little-endian;

             Case 4;
                 hW=Fix(Data/65536)//high word;
                 lW=Data mod 65536 //low word;
                 xBuf=ChrW(lW) + ChrW(hW) //formatage little-endian;

             Case }else{ //bytes array, size iSize;
                 xBuf=Data;

         }

         oMM.RtlMovememory lpData+iOfs,GetBSTRPtr(xBuf),iSize;
         iOfs=iOfs+iSize //maj l'offset;

     }


     Private Function GetDataBSTR(lpData,iSize,iOffset);
         //Read an iSize data to lpData+iOffset address;

         var CP_ACP=0 //code ANSI;
         var pDest,tdOffset;

         //valeurs pour les données numériques;
         pDest=oSCat.lstrcat(GetDataBSTR,"");
         tdOffset=8;

         switch(iSize // cast de la variable fonction){
             Case 1;
                 GetDataBSTR=CByte(0);
             Case 2;
                 GetDataBSTR=CInt(0);
             Case 4;
                 GetDataBSTR=CLng(0);
             Case }else{ //a little bit more complicated with string data...;
                 GetDataBSTR=String(iSize/2,Chr(0));
                 //la chaine variant BSTR stocke ses données ailleurs;
                 pDest=GetBSTRPtr(GetDataBSTR);
                 tdOffset=0;
         }


         //le contenu de la structure à l'offset iOffset écrase le contenu de la variable GetDataBSTR (tenir compte du TD);
         oMM.RtlMovememory pDest+tdOffset,lpData+iOffset,iSize;

         if(tdOffset==0){

             oAnWi.MultiByteToWideChar CP_ACP,0,lpData+iOffset,-1,pDest,iSize //don't forget conversion Ansi->Wide;

             GetDataBSTR=Replace(GetDataBSTR,Chr(0),"");
             //clean the trailer;
         }

     }


End Class

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I'll run it again (piece by piece) when I get the chance.

EDIT:

Interestingly enough, this site returns the same results

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...