Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: .Net Metaclass

  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    .Net Metaclass

    This allows JScript classic to get automation objects directly from managed code assemblies: woo hoo!


    JScript.Net

    Code:
    import System.Reflection;
    
    package Managed
    {
    	class Metaclass
    	{
    		function GetAssembly(pathname : String) : Assembly
    		{
    			return Assembly.LoadFrom(pathname);
    		}
    	}
    }
    Compile, register, and store that in the script host's directory.


    Then use it:

    Code:
    // create the meta class
    
    var objMeta = new ActiveXObject("Managed.Metaclass");
    
    
    // get the Assembly object from a file
    
    var objAssembly = objMeta.GetAssembly("C:\\myStuff\\myDotnet.dll");
    
    
    // use the Assembly object's CreateInstance method to retrieve a particular class
    
    var objClass = objAssembly.CreateInstance("myPackage.myClass");
    
    
    // call methods of objClass...
    That's right, you only have to register the one assembly, ever.

    Just a basic test, really...
    Last edited by enumerator; 08-22-2005 at 09:31 PM.
    Thanks in advance!

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't mean that to be a "drive by"; it actually took me a while to research an ultimately simple thing to implement, so...
    Thanks in advance!

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you provide a compile commandline for this?
    sounds very interesting.

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sure

    compile: %comspec% /k jsc /t:library "C:\ManagedMetaclass.js"
    (outputs to System32 directory)

    register: %comspec% /k regasm ManagedMetaclass.dll



    As a test, compile hello.js: %comspec% /k jsc /t:library /out:"C:\hello.dll" "C:\hello.js"

    Code:
    import System.Windows.Forms;
    
    package hello
    {
    	class world
    	{
    		function alert(message : String)
    		{
    			MessageBox.Show(message,"hello.world");
    		}
    	}
    }
    use it from an *.hta:
    Code:
    <html>
    <head>
    <title>My GUI</title>
    <HTA:Application id="HTA" />
    <script type="text/JScript">
    
    function window::onload()
    {
    	window.tester = new ActiveXObject("Managed.MetaClass").GetAssembly("C:\\hello.dll").CreateInstance("hello.world");
    }
    
    </script>
    </head>
    <body>
    <button onclick="tester.alert('from: ' + HTA.commandLine);">Test</button>
    </body>
    </html>
    So that's the basic idea: automating components written in a .Net language, without having to register each one.
    Thanks in advance!

  • #5
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great work, and appreciate the extra information, very descriptive.

    Thanks! Will try later this evenning.

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    313
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's cool, improve upon it if you like (if it works); as is, I'll be putting that little snippet to use...
    Thanks in advance!

  • #7
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Even works with windows 7 and 8.

    I know this thread is quite old, but as I just made a batch file that handles compilation and registration as well as a brief test, it might still be interesting for someone. Tested successfully on up-to-date XP (.net 2 only so far), Win7 32 and 64 and Win8 64 (.net 2 and 4, see remarks in code).

    In most cases you will have to right-click the batchfile and "Run As Admin", but else you will be reminded to do so.

    Note that the name of the batchfile (i.e. DLLoad.cmd) will determine the name of the namespace/package that must be used in new ActiveXObject("DLLoad.Acc"). Here is the code:

    @echo off
    cd %~dp0
    %~d0
    rem set net=%windir%\Microsoft.NET\Framework\v2.0.50727
    rem set net=%windir%\Microsoft.NET\Framework\v4.0.30319
    rem (chose from above OR use) NET auto-detect:

    for /d %%F in (%windir%\Microsoft.NET\Framework\v*) do if exist "%%F\jsc.exe" set net=%%F

    if not exist "%net%\regasm.exe" call :ERR 'No usable .NET'
    echo Installing ActiveXObject %~n0 using .net %net:~-9%
    set dll=%windir%\System32\%~n0.DLL

    rem UNcomment to unregister: (SAME filename AND net.version!)
    rem if exist %dll% call :TRY %net%\regasm /nologo %dll% /u
    rem del %dll% & pause & exit

    echo import System.Reflection; package %~n0 {class acc{function >tmp.js
    echo Load (s:String):Assembly {return Assembly.LoadFrom(s)}} } >>tmp.js

    call :TRY %net%\jsc /nologo /out:%dll% /t:library tmp.js
    call :TRY %net%\regasm /nologo %dll%
    :test
    echo package p {class c {function f(s) {return '%~n0 '+s } } } >tmp.js
    echo ^<script^> l=new ActiveXObject ('%~n0.acc').Load('tmp.dll') >tmp.hta
    echo alert(l.CreateInstance('p.c').f('OK.'));close()^</script^> >>tmp.hta
    %net%\jsc /t:library /nologo tmp.js
    tmp.hta

    :ERR
    if not "%*"=="" mshta "javascript:alert(%*);close()"
    del tmp.*
    exit

    :TRY
    %* || call :ERR 'Right-Click [%~nx0] and RUN AS ADMIN.'


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •