NPAPI插件框架错误 [英] NPAPI plugin framework Error

查看:161
本文介绍了NPAPI插件框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下答案尝试使用Yury Sidorov的NPAPI框架:

I am trying to use NPAPI Framework from Yury Sidorov by following this answer:

如何使用NPAPI将Delphi VCL表单嵌入HTML页面

,但我收到NPPlugin.pas错误。我正在使用delphi XE7,在这里我按照Krom Stern的说明进行操作

but I get an error with NPPlugin.pas. I am using delphi XE7 and here what i did by following Krom Stern instructions

procedure DefDebugOut(const Msg: string);
begin
  OutputDebugStringA(PAnsiChar(Msg + #13#10)); // Changed From Pchar To PAnsiChar
end;

但此消息仍然使编译器出错

but still getting error with compiler with this message

[dcc32错误] NPPlugin.pas(2215):E2010不兼容的类型: PWideChar和 PAnsiChar

在引发错误

procedure TPlugin.SetException(const msg: string);
var
  s: String;
begin
  try
    {$ifopt D+} NPP_DebugOut('Exception: ' + msg); {$endif}
    s:=UTF8Encode(msg);
    NPN_SetException(m_pScriptableObject, PAnsiChar(s));
  except
    { prevent any exception from leaking out of DLL }
  end;
end;

这是过程NPN_SetException

here is the procedure NPN_SetException

procedure NPN_SetException(npobj: PNPObject; msg: PNPUTF8);
begin
  NavigatorFuncs.SetException(npobj, msg);
end;


推荐答案

此NPAPI代码显然是为较早版本的Delphi设计的在Delphi 2009中切换为Unicode之前。默认的 String / (P)Char 类型不再是 AnsiString / (P)AnsiChar ,它们现在是 UnicodeString / (P)WideChar UnicodeString 不能强制转换为 PAnsiChar ,就像 AnsiString 永远都不能转换为 PWideChar

This NPAPI code was clearly designed for older versions of Delphi before the switch to Unicode in Delphi 2009. The default String/(P)Char types are no longer aliases for AnsiString/(P)AnsiChar, they are now aliases for UnicodeString/(P)WideChar. A UnicodeString cannot be casted to a PAnsiChar, just like an AnsiString could never be casted to a PWideChar.

In DefDebugOut(),最简单的解决方法是将 PAnsiChar 更改为 PChar 并更改 OutputDebugStringA () OutputDebugString()

In DefDebugOut(), the simplest fix is to change PAnsiChar to PChar and change OutputDebugStringA() to OutputDebugString():

procedure DefDebugOut(const Msg: string);
begin
  OutputDebugString(PChar(Msg + #13#10));
end;

这与所有Delphi版本兼容(代码从一开始就应该这样做。没有理由直接调用 OutputDebugStringA())。 PChar OutputDebugString()映射到 PAnsiChar OutputDebugStringA()在Delphi 2007及更早版本中,以及 PWideChar OutputDebugStringW()在Delphi 2009及更高版本中。所以一切都匹配。

This is compatible with all Delphi versions (the code should have been doing this from the beginning - there was no reason to call OutputDebugStringA() directly). PChar and OutputDebugString() map to PAnsiChar and OutputDebugStringA() in Delphi 2007 and earlier, and to PWideChar and OutputDebugStringW() in Delphi 2009 and later. So everything matches.

TPlugin.SetException() UTF8Encode()在所有版本的Delphi中都返回 UTF8String 。但是,在Delphi 2009之前, UTF8String 只是 AnsiString 本身的别名,但在Delphi 2009中已更改为具有完全RTL支持的真正UTF-8字符串类型(它仍然具有 AnsiString 基数,因此仍可以将其强制转换为 PAnsiChar )。将 UTF8String 分配给 UnicodeString 时,编译器将执行从UTF-8到UTF-16的隐式数据转换。 。并且如上所述, UnicodeString 无法强制转换为 PAnsiChar 。因此,您需要将 s 变量从 String 更改为 UTF8String 对于所有Delphi版本:

In TPlugin.SetException(), UTF8Encode() returns a UTF8String in all versions of Delphi. However, prior to Delphi 2009, UTF8String was just an alias for AnsiString itself, but in Delphi 2009 it was changed to a true UTF-8 string type with full RTL support (it still has an AnsiString base, so it can still be casted to PAnsiChar). When a UTF8String is assigned to a UnicodeString, the compiler performs an implicit data conversion from UTF-8 to UTF-16. And as stated above, UnicodeString cannot be casted to PAnsiChar. So you need to change the s variable from String to UTF8String for all Delphi versions:

procedure TPlugin.SetException(const msg: string);
var
  s: UTF8String;
begin
  try
    {$ifopt D+} NPP_DebugOut('Exception: ' + msg); {$endif}

    s:=UTF8Encode(msg);
    {
    UTF8Encode() is deprecated in Delphi 2009+.
    In those versions, you can use this instead:
    s := UTF8String(msg);
    }

    NPN_SetException(m_pScriptableObject, PAnsiChar(s));
  except
    { prevent any exception from leaking out of DLL }
  end;
end;

这样,如果您仍然在 NPN_SetException上遇到相同的错误()调用,则表示 NPN_SetException()的第二个参数声明为 PChar 。它需要声明为 PAnsiChar

With that said, if you are still getting the same error on the NPN_SetException() call, then it means the second parameter of NPN_SetException() is declared as PChar. It needs to be declared as PAnsiChar instead.

这篇关于NPAPI插件框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆