获取插件路径
该功能不复杂,不过使用了windows提供的API故只适用于windows平台。代码如下:
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,pl