Chrome 开发 plugin 崩溃?
(创建时间:2009年06月22日 21:57:00)
Jangogo : 
Chrome 插件在处理输出的时候,如果是字符串,必须NPN_MemAlloc来分配一个内存
 
  1. //字符串必须转成UTF-8,不然中文会乱码   
  2.            DWORD dwNum = WideCharToMultiByte(CP_UTF8,NULL,rlt.bstrVal,-1,NULL,0,NULL,FALSE);   
  3.            char *psText;   
  4.            psText = new char[dwNum];   
  5.            if(!psText)   
  6.            {   
  7.             delete []psText;   
  8.            }   
  9.            WideCharToMultiByte(CP_UTF8,NULL,rlt.bstrVal,-1,psText,dwNum,NULL,FALSE);   
  10.              
  11.            //必须使用NPN_MemAlloc来分配一个内存,否则程序会崩溃   
  12.            NPUTF8 *utf8_chars = static_cast<NPUTF8*>(NPN_MemAlloc(dwNum));   
  13.            memcpy(utf8_chars, psText, dwNum);   
  14.            STRINGZ_TO_NPVARIANT(utf8_chars, *result);   
  15.                
  16.            //回收资源   
  17.            delete []psText;  

 

文档中心