Calling C Code from JavaScript


Calling C++ Code from JavaScript

In general, calling C++ code from within JavaScript is just like calling a method on any other JavaScript object. You simply use standard JavaScript object.method() notation to call a given method. For example, to create an object of type Date and then call a method on the object, in regular JavaScript code, you would write something like this:

 var theDate = new Date();  var theDay = theDate.getDay(); 

To do the same thing with a C++ object in Dreamweaver, you would simply call the method on a prenamed object. You don't need to create the C++ objects the way you do with built-in classes; you simply give the name of the class and method you want to call. For example, if a JSExtension named MyExt.dll exists in the JSExtensions folder and one of the methods it exposes to the JavaScript namespace is myMethod , then the syntax for calling the method would be as follows :

 MyExt.myMethod(); 

Making Sure a JSExtension Is Present Before Using It

Because JSExtensions are not built into the Dreamweaver application and not all Dreamweaver users might have a particular JSExtension installed, it is necessary to first make sure that a given JSExtension is actually present and loaded into memory before attempting to use it. This is accomplished by using the typeof() JavaScript function to retrieve the type of a JSExtension object. For example, to make sure that a JSExtension named MyExt is installed properly, you would use the following code:

 if (typeof(MyExt) != "undefined")     // go ahead and call object's methods -- it is installed  else     alert("The MyExt Extension is not installed."); 

The expression typeof(MyExt) attempts to retrieve the type of the given argument. If the extension is not present, or is present but fails to load properly, then the typeof function will return the string "undefined" . At this point, you might show an error message to the user or avoid calling methods on the JSExtension object.

The Standard JavaScript Extensions

Dreamweaver MX ships with a standard set of JSExtensions that developers can take advantage of right out of the box. The extensions include these:

  • DWfile ” Provides access to platform-native files. You can create, delete, read data from, write data to, and otherwise manipulate files and directories on the user's system.

  • FWLaunch ” Provides access to Fireworks scripting from within Dreamweaver.

  • MMNotes ” Exposes Dreamweaver's Design Notes functionality to extensions. Using MMNotes, you can manipulate Design Note information for files directly.

  • SWFFile ” Allows developers to build Flash objects that create simple Flash content.

These JavaScript extensions are stored inside the JSExtensions folder, which is inside Dreamweaver's Configuration folder. Several other extensions are also stored in this directory that are for the internal use of the Dreamweaver application, such as MM.dll and MMSiteSettings.dll.

When Dreamweaver starts up, it scans the JSExtensions folder looking for these C++ extension DLLs and adds each one of them to the built-in JavaScript namespace, as illustrated in Figure 10.1. Each JSExtension tells Dreamweaver what methods it makes available to JavaScript developers and what arguments are necessary for each method. JavaScript developers can then call those methods just like they do any other JavaScript method.

Figure 10.1. During the bootup process, Dreamweaver scans the Configuration/JSExtensions folder to include any DLLs found there.

graphics/10fig01.gif

TIP

Dreamweaver maintains a link to these DLLs while running, so you'll need to quit the program if you ever need to uninstall or remove one of theDLLs.


Example: Using the DWfile JSExtension

One of the more useful JSExtensions that ship with Dreamweaver is the DWfile extension, which provides methods for creating and manipulating files on the user's local machine, a capability not supported by standard JavaScript. This functionality can be used to create and store preference files, read data files that are stored in a format that Dreamweaver does not understand, create or list the contents of directories, and generally perform many other types of file access that a JavaScript extension needs to do.

The following code uses the DWfile extension to create a new text file:

 function testDWfile()  {     var theFile = "file:///D/temp/myTestFile.txt"  if (typeof(DWfile) != "undefined")     DWfile.write(theFile, "This is some text", false);  } 

When the preceding function is executed in Dreamweaver, a new file named myTestFile.txt will be created with "This is some text" as its contents. The first argument specifies a file to write information to, written as a standard file:// URL string. The second argument is the string to write to the file. The third argument is a Boolean value indicating whether the data should be appended to the existing file (true) or should replace the existing content (false).

TIP

If the file:// portion of the file URL specifier string is left out, then the file is created in the same directory as the Dreamweaver application.




Joseph Lowery's Beyond Dreamweaver
Joseph Lowerys Beyond Dreamweaver
ISBN: B000H2MWYS
EAN: N/A
Year: 2001
Pages: 87
Authors: Joseph Lowery

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net