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

本文介绍如何在Windows平台上获取插件的路径和页面资源的URL,包括使用API获取插件目录路径、通过NPAPI插件接口获取页面路径及资源路径的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


### 浏览器插件支持情况 现代浏览器如Microsoft Edge基于Chromium内核,在2015年后逐步停止了对NPAPI插件的支持[^2]。这意味着传统的ActiveX控件或类似的插件无法直接在新版Edge中正常工作。 ### 安装海康威视插件面临的挑战 由于上述原因,尝试在最新版本的Microsoft Edge浏览器中安装并使用依赖于NPAPI接口的传统海康威视插件会遇到困难。这类插件原本设计用于较早版本的Internet Explorer或其他仍保留NPAPI支持的老化平台环境[^3]。 ### 替代解决方案建议 #### 使用WebRTC技术 对于希望实现在Edge浏览器中查看由海康设备提供的视频流的需求,推荐考虑采用更现代化的技术路径——即利用WebRTC协议来传输媒体数据。这种方式不需要额外安装任何本地插件,并且能够提供较低延迟能力下的高质量音视频通信服务。 #### 尝试第三方中间件产品 部分厂商提供了专门针对此类需求开发的产品,它们可以在无需改动原有硬件设施的前提下,帮助用户轻松地将传统监控系统的信号接入到最新的HTML5标准兼容环境中。例如某些方案允许通过服务器端转码的方式把RTSP/ONVIF等格式转换成适合跨平台展示的形式。 #### 考虑专用客户端应用程序 如果以上两种途径都不能满足具体应用场景的要求,则不妨评估是否有条件部署一套专门为Windows操作系统定制化的海康系列产品的官方桌面级监视软件。这通常是最稳定可靠的选择之一,因为可以直接访问底层资源而不受限于特定类型的web浏览工具链约束[^1]。 ```python # 示例Python代码片段演示如何创建简单的HTTP请求获取在线文档内容 import requests url = 'http://example.com' response = requests.get(url) if response.status_code == 200: print('Request successful') else: print(f'Request failed with status code {response.status_code}') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值