Click here to Skip to main content
13,895,605 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 20 Oct 2016
Licenced CPOL

NPAPI – A Plugin to Retrieve Device Information

, 20 Oct 2016
Rate this:
Please Sign up or sign in to vote.
NPAPI – A Plugin to Retrieve Device Information

Some time ago, I wrote a barcode scanner NPAPI plugin for the HTML5 browser by zetakey. It is based on code supplied as MySensor in the motorola knowledge base. The plugin is for Windows Mobile 6.x based devices by Intermec and another one for M3 devices.

The MySensor plugin based on npruntime was the only sample I could get to compile and work correctly. The barcode scanner fork I wrote does also work very well. The new MyDevinfo plugin retrieves model code, battery level and WLAN RSSI value of the Intermec device and allows to present this information to an HTML5 browser user when needed.


The above shows the web page of MyDevinfo_simple.htm.

A NPAPI plugin for Windows Mobile HTML5 browsers like Zetakey Browser, Intermec Browser, Honeywell Enterprise Browser, Honeywell HTML5 Browser or Motorola/Zebra RhoMobile Browser is a DLL. The DLL has to be placed in the browser’s plugin directory and then can be used from HTML5 JavaScript code. The code defines which properties, methods and callbacks can be used from JavaScript. When the browser loads the plugin, it queries all known properties of the plugin object. These properties are accessed via NPIdentifier types:

static NPIdentifier sCurrentRSSIValue_id;
static NPIdentifier sCurrentBattery_id;
static NPIdentifier sModelCode_id;
//  Constructor for the plugin, called when the embedded mime type is found on a web page (see npp_new).
//  <embed id="embed1" type="application/x-itc-devinfo" hidden=true> </embed> 
CMyDevinfoPlugin::CMyDevinfoPlugin(NPP pNPInstance) :
	DEBUGMSG(1, (L"CMyDevinfoPlugin()...\n"));
  	// Must initialise this before getting NPNVPluginElementNPObject, as it'll
	// call back into our GetValue method and require a valid plugin.
	pNPInstance->pdata = this;

    // Say that we're a windowless plugin.
    NPN_SetValue(m_pNPInstance, NPPVpluginWindowBool, false);

	//  Instantiate the values of the methods / properties we possess
	sMonitor_id = NPN_GetStringIdentifier("monitor");
	sPollInterval_id = NPN_GetStringIdentifier("pollInterval");
	sCurrentValue_id = NPN_GetStringIdentifier("currentValue");
	sCurrentRSSIValue_id = NPN_GetStringIdentifier("currentRSSIValue");
	sCurrentBattery_id = NPN_GetStringIdentifier("currentBatteryLevel");
	sModelCode_id = NPN_GetStringIdentifier("modelCode");

	//  Export onto the webpage the JS object 'MyDevinfo'.  This enables us
	//  to say var myObj = new MyDevinfo();
	NPObject *sWindowObj;
	NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj);
	NPObject *mySensorObject =NPN_CreateObject
	NPVariant v;
	OBJECT_TO_NPVARIANT(mySensorObject, v);
	NPIdentifier n = NPN_GetStringIdentifier("MyDevinfo");
	NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v);

In the above, you can see the global initialization of the property and method names for the Devinfo object. When a property of the JavaScript object is queried, the following code is executed:

bool MyDevinfoPluginObject::GetProperty(NPIdentifier name, NPVariant *result)
	//  Retrieve the value of a property.  *result is an out parameter
	//  into which we should store the value
	bool bReturnVal = false;

        if (name == sCurrentRSSIValue_id)
		//  Called by: var sensorVal = myDevinfo.currentRSSIValue;
		//  Return the current value to the web page.
		INT32_TO_NPVARIANT(this->m_iCurrentRSSIValue , 
		*result);// (((float)this->m_iCurrentValue) / 100.0, *result);
		bReturnVal = true;
	else if (name == sCurrentBattery_id)
		//  Called by: var sensorVal = myDevinfo.currentBatteryLevel;
		//  Return the current value to the web page.
		INT32_TO_NPVARIANT( this->m_iCurrentBatteryLevel, 
		*result);// (((float)this->m_iCurrentValue) / 100.0, *result);
		bReturnVal = true;
	else if (name == sModelCode_id)
		//  Called by: var sensorVal = myDevinfo.modelCode;
		//  Return the value to the web page.
		char* npOutString = (char *)NPN_MemAlloc(MAX_BUFF);
		sprintf(npOutString, "%s", getModelCode());
		*result);// (((float)this->m_iCurrentValue) / 100.0, *result);
		bReturnVal = true;
	if (!bReturnVal)
	return bReturnVal;

The return types have to be put in a NPVARIANT structure which is then returned to the browser. There are different macros to convert int, float or null terminated strings to a NPVARIANT.

In the code, I used separate header and code files to retrieve the information like the battery or RSSI level. This is easy to manage.

If you want to extend the number of properties, you need to add a new global NPIdentifier variable, add code to register the new property and code to return the new property value.

The code for an object method is similar. There is also a global variable of type NPIdentifier. Then, when the object method is called from JavaScript, the following code is executed:


bool MyDevinfoPluginObject::Invoke(NPIdentifier name, const NPVariant *args,
                               uint32_t argCount, NPVariant *result)
	//  Called when a method is called on an object
	bool bReturnVal = false;
	//  Convert to lower case to make our methods case insensitive
	char* szNameCmp = _strlwr(NPN_UTF8FromIdentifier(name));
	NPIdentifier methodName =  NPN_GetStringIdentifier(szNameCmp);
	//  mySensor.monitor(bool)
	if (methodName == sMonitor_id)
		//  Expect one argument which is a boolean (start / stop)
		if (argCount == 1 && NPVARIANT_IS_BOOLEAN(args[0]))
			if (NPVARIANT_TO_BOOLEAN(args[0]))
				//  Create a thread to monitor the sensor			
				CloseHandle(CreateThread(NULL, 0,
				(LPTHREAD_START_ROUTINE)SensorMonitorThread, this, 0, NULL));
				//  Stop monitoring the sensor
			//  Monitor has no return value
			bReturnVal = true;
	if (!bReturnVal)
	return bReturnVal;

As the monitor method is to be called with a boolean to start or stop the monitor, the code will start or stop a background thread. First, the argument passed to the method is tested to be a boolean, then it is converted from the provided NPVARIANT to a C boolean.

The background thread uses a window handle to the browser to send update messages. These WM_USER messages are received by an invisible window and its window procedure. That will then invoke a callback method inside the browser context, if available.

The source code and description can be found at github.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

-- No messages could be retrieved (timeout) --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 21 Oct 2016
Article Copyright 2016 by hjgode
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid