Create the native part of the extension

Using the memory sample extension, you'll learn how to:
  • Set up a native extension project in Eclipse
  • Implement a JNEXT extension on the native side 
  • Build your native extension project

Create an extension project with the BlackBerry 10 Native SDK

  1. In the QNX Momentics IDE, choose your workspace and then click the Workbench icon.
  2. On the File menu, select New > BlackBerry C/C++ Project.
  3. In the Project Name field, type a name for your project (for example, MemoryExt). Click Next.
  4. In the Basic Settings window, select the following options:
    1. For the Language, select C++.
    2. For the Build Style, select Managed Build.
    3. For the Project Type, select Empty Shared Library.
  5. Click Next.
  6. In the Active Configuration field, select the configuration you want to use for the project. Click Next.
  7. If you want to use a different version of the BlackBerry 10 Native SDK the the workspace uses, clear the Use Workspace SDK check box, and then select the BlackBerry 10 Native SDK version you want to use.
  8. Click Finish.

After you finish, a new project appears in the Project Explorer view with the name that you specified.

Next, you need to import the extension resources into your new project.

Import extension resources into your project

  1. In the QNX Momentics IDE, right-click your project and click Import.
  2. Expand the General folder and select File System. Click Next.
  3. Navigate to the native_src folder, and select the check boxes for public and src so that both folders and their contents are imported. Click Finish.

Your project now contains the public and src folders, which contain the source and header files. The file structure of your project should match the file structure of the extension sample.

Implement a JNEXT extension

The native and JavaScript parts of a BlackBerry WebWorks extension communicate with each other through the use of an extension framework provided by JNEXT. You can view the native interface for the JNEXT extension in the plug-in header file, which is located in the public folder of your project. The header file also contains constants and utility functions that you can use in your native code. Your native extension must be derived from JSExt, which is defined in the plugin.h file. You must include this file in your extension.

The sample code for the memory extension implements the native extension interface to JNEXT.

Each native extension must implement the following callback functions:

extern char* onGetObjList( void );
extern JSExt* onCreateObject( const string& strClassName, const string& strObjId );

The onGetObjList function returns a comma-separated list of classes supported by this JNEXT extension, which is used to determine the set of classes that can be instantiated by this JNEXT extension.

The onCreateObject function is the other callback that must be implemented by the native JNEXT extension. The onCreateObject function takes two parameters:
  • The name of the class that's created from the JavaScript portion. Valid names are returned by the onGetObjList function.
  • The unique object ID for the class.

The onCreateObject function returns a pointer to the created extension object.

The native extension must also implement the following class:

class JSExt
    virtual ~JSExt() {};
    virtual string InvokeMethod( const string& strCommand ) = 0;
    virtual bool CanDelete( void ) = 0;
    std::string m_id;

The m_id is an attribute that contains the JNEXT ID for this object. The ID is passed to the class as an argument to the constructor. The ID triggers events on the JavaScript side from native. The CanDelete method is used by JNEXT to determine whether the native object can be deleted. The InvokeMethod function is called based on a request from JavaScript to invoke a method of this particular object. The only argument to InvokeMethod is a string that's passed from JavaScript. The InvokeMethod should parse the passed string to determine which method of the native object should be executed.

If you want the native code to trigger an event on the JavaScript side, you'll need to call the SendPluginEvent function, which has the following signature:

void SendPluginEvent( const char* szEvent, void* pContext );

The first parameter is a space-delimited string consisting of the m_id (inherited attribute from JSExt), which is followed by the arguments you want to pass to the JavaScript onevent function. The second parameter is the m_pContext (inherited attribute from JSExt).

Build your native extension

  1. In the QNX Momentics IDE, right-click your project and click Clean Project.
  2. Right-click your project and click Build Configurations > Build Selected.

    The Clean and Rebuild Configurations window appears, showing all the available build configurations for the project. Depending on the profile you want to use, select the device and corresponding simulator build profiles. Click OK.

You should see the shared libraries generated in the folders for each build configuration that you selected.

Changing the build artifact settings

In the QNX Momentics IDE, you can change the build artifacts for your project, including the artifact type, name, extension, and output prefix. By default, the IDE adds the output prefix "lib" to the shared library file (for example,

Each modification of the build artifact corresponds to a single configuration. If you want to modify the build artifacts for all the build configurations, you must modify each build configuration.

For the memory extension sample, the name of the shared library file should appear as We can change the build artifact settings to reflect this name by completing the following steps:

  1. Right-click your project and click Properties.
  2. Expand C/C++ Build and select the Settings option.
  3. From the Configuration field, select the build configuration that you want to configure.
  4. Click the Build Artifact tab.
  5. In the Artifact Type field, select Shared Library.
  6. In the Artifact name field, type memoryJnext.
  7. In the Artifact extension field, select so.
  8. In the Output prefix field, clear the value.
  9. Click Apply and then OK.

Last modified: 2014-03-10

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus