JS version key Wizard-elf dot simple tutorial 5--writing extensions

Source: Internet
Author: User
Tags parse string

Absrtact: Although the kernel functions of dots are still increasing, it is not perfect after all, so it is necessary for you to write your own extension to achieve the desired function when encountering the functions that cannot be realized.

Even so, don't be afraid, because writing an extension is really a simple thing to do, just follow the steps below, but it requires some C and C + + skills.

Catalogue

Elf Point Point Simple Tutorial 1--Download and install

Elf Dot Simple tutorial 2--Basic operation

Elf Dot Simple Tutorial 3--recording script

Elf Dot Simple Tutorial 4--editing and debugging scripts

Elf Dot Simple Tutorial 5--writing extensions

Writing extensions

Although the point of the kernel function is still increasing, but after all can not be perfected, so in the face of the inability to implement the function, you still need to write your own extension to achieve the desired function.

Even so, don't be afraid, because writing an extension is really a simple thing to do, just follow the steps below, but requires some C and C + + skills.

1. First we open the installation root directory of the Genie point.

2. Enter the Extensionsources directory, there will be a directory named Acextenddemo, click into the ACExtendDemo.sln, which is a VS2010 sample solution.

3. After opening the solution, open dllmain.cpp, and the extension's source file is here.

Below, we will paste the main content of dllmain.cpp below, carefully look inside the note.

Because the characters passed from the V8 engine are UTF8, and our project chooses the Unicode character set, we need each other's conversion function char* UnicodeToUTF8 (const wchar_t* WP) {//specific content see source code};// UNICODETOUTF8 Reverse wchar* utf8tounicode (const char* c) {//specific content see source code};//parse string into JSON object Json::value Parsejson (wchar* json) {/ /specific content See source code};/************************************************************************** Method Description: True export function, Function arguments that are called in our script: The params point extension function has only one parameter, which can be any string if the arguments that need to be passed are complex data structures that can be passed in as a JSON string. The params in this extension function is similar to the struct "{a:3,b:6}" return value: The return value of the dot extension function is a string, and if you need to return a complex structure, you can convert the struct to a JSON string to return ************************* /extern "C" __declspec (dllexport) wchar* Sum (WCHAR *params) {//IF  The params is the string "{A:3,b:6}"//parsing the Json string Json::value p = Parsejson (params);    Gets the value of a, at which time 3 int a = p["a"].asint ();  Gets the value of B, at which time 6 int b = p["B"].asint ();  Add two numbers, c=9 int c = a + B;  Convert the result of the calculation to a string return WCHAR *r = new WCHAR[12];  wsprintf (R, L "%d", c); Return r;};/ /return JSON string case extern "C" __declspec (dllexport) wchar* getpoint (WCHAR *params) { return L "{\" x\ ": 12,\" y\ ": 8}";};/  /directly perform the operation of the case extern "C" __declspec (dllexport) wchar* OpenUrl (WCHAR *params) {WCHAR * url = params;  :: ShellExecute (NULL, L "open", L "explorer", URL, null, sw_show);  return L ""; };

Then we compile the project, and a file named ACExtensionDemo.dll is generated, and the file is copied to the extensions in the installation directory.

So we can invoke the extension method we just wrote in the script, and here we use the method called extern "C" __declspec (dllexport) wchar* Sum (WCHAR *params) as an example to illustrate this.

The script should write like this

The object that constructs the extension method, where the function is the name of the DLL var AED = ex ("Acextensiondemo");//Call the Sum method, where the first parameter is the method name, the second is the parameter, and this parameter is passed as the params to the sum method in the DLL. Recall the comment on the sum method that you just expanded. After this method executes, the value of RET is 9.  var ret = aed.call (' Sum ', ' {a:3,b:6} ');//Although the parameter params of the method in the DLL must be a string, in the script we can still call the following way, In this case, the JSON object is automatically converted to a string that is passed to the DLL extension method var Ret1 = Aed.call (' Sum ', {a:3,b:6});

Writing an extension and calling it is not very simple. :)

JS version key Wizard-elf dot simple tutorial 5--writing extensions

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.