转 http://blog.csdn.net/z6482/article/details/8301823
获取插件路径
该功能不复杂,不过使用了windows提供的API故只适用于windows平台。代码如下:
- LPTSTR moduleName = new TCHAR[100];
- GetModuleFileName(GetModuleHandle(_T("name")),moduleName,100);
- 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。其中提到了三种方式,但我感觉比较靠谱的方式是第一种,下面是简单的实现代码:
- 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);
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。获取资源路径的代码如下:
- 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);
- }
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.