NPAPI插件开发详细记录:实用功能——获取插件路径、页面路径、资源路径

http://blog.csdn.net/z6482/article/details/8301823

获取插件路径

该功能不复杂,不过使用了windows提供的API故只适用于windows平台。代码如下:

  1. LPTSTR moduleName = new TCHAR[100];  
  2. GetModuleFileName(GetModuleHandle(_T("name")),moduleName,100);  
  3. std::string mPath = std::string(moduleName);  
	LPTSTR moduleName = new TCHAR[100];
	GetModuleFileName(GetModuleHandle(_T("name")),moduleName,100);
	std::string mPath = std::string(moduleName);

GetModuleHandle(_T("name"))的name即你的插件名称,如:npdemo.dll。TCHAR的长度视具体情况而定,用于保存得到的路径。注意需要include tchar.h和string(是string不是string.h)。当然路径其实已经保存在moduleName中了,如果不用string可以不要最后一句(鉴于字符串处理的繁琐,推荐使用string)。

获取页面路径、资源路径

要获取插件页面的路径,可以参考:https://developer.mozilla.org/en-US/docs/Getting_the_page_URL_in_NPAPI_plugin。其中提到了三种方式,但我感觉比较靠谱的方式是第一种,下面是简单的实现代码:

  1.        NPObject *pluginObj;  
  2. NPN_GetValue(m_pNPInstance,NPNVWindowNPObject,&pluginObj);  
  3. NPIdentifier  n=NPN_GetStringIdentifier("location");  
  4.         NPVariant rval;  
  5. NPN_GetProperty(m_pNPInstance,pluginObj,n,&rval);  
  6. NPObject* locationObj = rval.value.objectValue;  
  7. n=NPN_GetStringIdentifier("href");  
  8. NPN_GetProperty(m_pNPInstance,locationObj,n,&rval);  
  9. std::string  pageURL = std::string(rval.value.stringValue.UTF8Characters);  
        NPObject *pluginObj;
	NPN_GetValue(m_pNPInstance,NPNVWindowNPObject,&pluginObj);
	NPIdentifier  n=NPN_GetStringIdentifier("location");
         NPVariant rval;
	NPN_GetProperty(m_pNPInstance,pluginObj,n,&rval);
	NPObject* locationObj = rval.value.objectValue;
	n=NPN_GetStringIdentifier("href");
	NPN_GetProperty(m_pNPInstance,locationObj,n,&rval);
	std::string  pageURL = std::string(rval.value.stringValue.UTF8Characters);


object标签可以使用data属性设置资源的URL,embed标签使用src属性设置资源URL。获取资源路径的代码如下:

  1. NPObject *pluginObj;  
  2.        NPN_GetValue(m_pNPInstance,NPNVPluginElementNPObject,&pluginObj);  
  3.        NPIdentifier n=NPN_GetStringIdentifier("src");  
  4.        NPVariant rval;  
  5. NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);  
  6. if(NPVARIANT_IS_STRING(rval))  
  7.     m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);  
  8. else{  
  9.     n=NPN_GetStringIdentifier("data");  
  10.     NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);  
  11.     if(NPVARIANT_IS_STRING(rval))  
  12.         m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);  
  13. }  
	NPObject *pluginObj;
        NPN_GetValue(m_pNPInstance,NPNVPluginElementNPObject,&pluginObj);
        NPIdentifier n=NPN_GetStringIdentifier("src");
        NPVariant rval;
	NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);
	if(NPVARIANT_IS_STRING(rval))
		m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);
	else{
		n=NPN_GetStringIdentifier("data");
		NPN_GetProperty(m_pNPInstance, pluginObj, n, &rval);
		if(NPVARIANT_IS_STRING(rval))
			m_pSrc = std::string(NPVARIANT_TO_STRING(rval).UTF8Characters);
	}



这样,不管html中使用的是object标签的data还是使用的embed标签中的src设置资源URL,都可以将资源的完整URL保存到m_pSrc中。



Via window location

From Robert Platt:

// Get the Dom window object.
NPN_GetValue( m_pNPInstance, NPNVWindowNPObject, &sWindowObj );
// Create a "location" identifier.
NPIdentifier identifier = NPN_GetStringIdentifier( "location" );
// Declare a local variant value.
NPVariant variantValue;
// Get the location property from the window object (which is another object).
bool b1 = NPN_GetProperty( m_pNPInstance, sWindowObj, identifier, &variantValue );
// Get a pointer to the "location" object.
NPObject *locationObj = variantValue.value.objectValue;
// Create a "href" identifier.
identifier = NPN_GetStringIdentifier( "href" );
// Get the location property from the location object.
bool b2 = NPN_GetProperty( m_pNPInstance, locationObj, identifier, &variantValue );

This code is just a rough example. Remember to release any references after using them.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值