How to Create a. exe or. dll file using JScript

Source: Internet
Author: User

What is JScript?
JScript is an active scripting language developed by Microsoft and implemented based on ECMAScript specifications. JavaScript in Internet Explorer actually refers to JScript. JScript is supported by Windows Script Host (WSH) (JavaScript shell scripting: C: \> cscript jslint. js in WSH ). The latest JScript version (JScript. NET) is based on ECMAScript 4.0 and can be compiled in the. Net environment.
. NET Framwork contains the JScript Compiler: JScriptCompiler (C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ jsc.exe), which can compile a JScript file into. exe or. dll file.
For ease of use, we can add the JScriptCompiler Path to the environment variable (Environment Variable-> system variable-> Path. In the CMD program running window, call the command "jsc" to view the compiler-related help options.
Copy codeThe Code is as follows:
Jsc [Option] <Source File> [Option] <Source File>…]

JScript compiler options

-Output file-
/Out: <file> specifies the name of the binary output file.
/T [arget]: exe creates a console application (default)
/T [arget]: winexe creates a Windows Application
/T [arget]: library creates a library program set.
/Platform: <platform> indicates the platform on which this code can run. It must be x86, Itanium, x64, or anycpu. The default value is anycpu.

-Input file-
/Autoref [+ |-] automatically reference the Assembly based on the imported namespace and fully qualified name (default
On)
/Lib: <path> specifies the additional directory to be referenced in the search.
/R [eference]: <file list> reference metadata from the specified assembly file <file list >:< assembly name> [; <assembly name>...]

-Resource-
/Win32res: <file> specify the Win32 resource file (. res)
/Res [ource]: <info> embed the specified resource <info >:< filename> [, <name> [, public | private]
/Linkres [ource]: <info> link the specified resource to this Assembly <info >:< filename> [, <name> [, public | private]

-Code Generation-
/Debug [+ |-] sends debugging information
/Fast [+ |-] disable the language function to make code better generated
/Warnaserror [+ |-] treats the warning as an error
/W [arn]: <level> set the warning level (0-4)

-Miscellaneous-
@ <Filename> for more options, read the response file.
/? Show Help
/Help show help
/D [efine]: <symbols> define Conditional compilation symbols
/Nologo does not display the compiler copyright mark
/Print [+ |-] provides the print () function

-Advanced-
/Codepage: <id> use the specified code page ID to open the source file.
/Lcid: <id> use the specified LCID for messages and the default code page.
/Nostdlib [+ |-] do not import the standard library (mscorlib. dll) and change the default autoref value to off.
/Utf8output [+ |-] sends the compiler output in the form of UTF-8 character encoding
/Versionsafe [+ |-] specifies the default value for a member not marked as "override" or "hide"

Create a. exe file

First create a JS file (C: \ test \ helloWorld. js) with the following content:

Var date = new Date (); print ('Hello World! \ NToday is '+ date );

Then we compile:

C: \ test> jsc helloWorld. jsMicrosoft (R) JScript Compiler version 8.00.50727for Microsoft (R). NET Framework version 2.0.50727Copyright (C) Microsoft Corporation 1996-2005. All rights reserved.
You will be surprised to find that the C: \ test directory contains a helloWorld.exe file, which is very simple.

Finally, we can directly execute the helloWorld.exe file:

C: \ test> helloWorldHello World! Today is Fri Jun 3 23:13:20 UTC + 8 2011
Success !!

Create a. dll file

. Dll file creation is also very simple:
Copy codeThe Code is as follows:
Package LibHW {
Class HelloWorld {
Function run (){
Var date = new Date ();
Return 'Hello World! \ NToday is '+ date;
}
}
}

Compile statement:

C: \ test> jsc/t: library LibHW. js
For the generated LibHW. dll file, we can call it by creating a new. exe file (consumer. js-> consumer.exe) as an import module (similar to Python ).

Create the consumer. js file first:

Import LibHW; var hw = new LibHW. HelloWorld (); print (hw. run ());
Then compile the consumer. js file and execute consumer.exe:

C: \ test> jsc consumer. jsMicrosoft (R) JScript Compiler version 8.00.50727for Microsoft (R). NET Framework version 2.0.50727Copyright (C) Microsoft Corporation 1996-2005. All rights reserved. C: \ test> consumerHello World! Today is Sat Jun 4 00:42:35 UTC + 8 2011
Of course, you can also create a windows application. The consumer. js file in the previous example is modified as follows:
Copy codeThe Code is as follows:
Import System. Windows. Forms; // this has a MessageBox class
Import LibHW;

Var hw = new LibHW. HelloWorld ();
MessageBox. Show (
Hw. run (),
"Dude! ",
MessageBoxButtons. OK,
MessageBoxIcon. Exclamation
);

Compile statement:

C: \ test> jsc/t: winexe consumer. js
Double-click the newly generated consumer.exe file. Haha, is it a sense of accomplishment!

From the above example, we can see that the potential of JScript is unlimited, and there is no way to do it.

Additional reading:

  • Make your javascript a Windows. exe
  • JavaScript shell scripting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.