【问题标题】:Converting C++ function to Delphi: what to do with void* parameter?将 C++ 函数转换为 Delphi:如何处理 void* 参数?
【发布时间】:2009-09-14 08:01:48
【问题描述】:

我正在使用下面的 C++ 示例在 Delphi 中编写一个 DLL:

USERDLL_API double process_message (const char* pmessage, const void* param) 
{
    if (pmessage==NULL) { return 0; }
    if (param==NULL) { return 0; }

    if (strcmp(pmessage,"state")==0) 
    { 
        current_state *state = (current_state*) param;
        return process_state( (current_state*)param ); 
    }
}

不幸的是,我对 C++ 和指针几乎一无所知。我应该用什么来代替 char* (PChar?) 和 void*?

function process_message (const pmessage: PChar; const param: ???): Double; export;
begin
    ???
end;

exports process_message;

对于函数主体的任何帮助也将受到高度赞赏。我意识到这不是火箭科学,但如果有人愿意为我这样做,我不会仅仅为了转换几行而学习 C++ 的基础知识:-)

【问题讨论】:

  • 学习 C++ 的基础知识是您应该做的事情,无论是否需要翻译该代码。如果您使用 Delphi,那么您就在 Windows 上工作,如果您在 Windows 上工作,那么您应该知道如何阅读 C 和 C++,因为您将看到的几乎所有文档都是这些语言的。如果你不这样做,你只是把头埋在沙子里。
  • 好点,罗布。只是我不是专业的软件开发人员。我通常做逻辑,Delphi的内置组件就是我所需要的。当我不得不处理其他事情时,这是一个罕见的案例。赞美stackoverflow :-)

标签: c++ delphi pointers parameters void


【解决方案1】:

RAD Studio 在线文档包括一个Delphi to C++ types mapping 表,它可以帮助您将 C++ 代码转换为 Delphi。

Delphi type         Platform    Corresponding C++ type

Boolean (Delphi)                bool (C++)
ShortInt (Delphi)               ShortInt, signed char (C++)
SmallInt (Delphi)               short (C++)
Integer (Delphi)                int (C++)
Byte (Delphi)                   Byte (C++)
Word (Delphi)                   Word (C++)
Cardinal (Delphi)               unsigned (C++)
Int64 (Delphi)                  __int64 (C++)
UInt64 (Delphi)                 unsigned __int64 (C++)
NativeInt (Delphi)  32-bit Win   int (C++)
                    64-bit Win  __int64 (C++)
                    64-bit iOS  long (C++)
NativeUInt (Delphi) 32-bit      unsigned (C++)
                    64-bit Win  unsigned __int64 (C++)
                    64-bit iOS  unsigned long (C++)
Single (Delphi)                 float (C++)
Double (Delphi)                 double (C++)
Extended (Delphi)               Extended (C++)
Currency (Delphi)               Currency, CurrencyBase (C++)
Comp (Delphi)                   Comp, CompBase (C++)
Real (Delphi)                   double (C++)
ShortString (Delphi)            ShortString, ShortStringBase (C++)
OpenString (Delphi)             OpenString (C++)
File (Delphi)                   file (C++)
Text (Delphi)                   TextFile (C++)
ByteBool (Delphi)               ByteBool (C++)
WordBool (Delphi)               WordBool (C++)
LongBool (Delphi)               BOOL (C++)
Real48 (Delphi)                 not supported in C++
Pointer (Delphi)                void* (C++)
PWideChar (Delphi)              WideChar* (C++)
PAnsiChar (Delphi)              char* (C++)
Variant (Delphi)                defined in sysvari.h (C++)
OleVariant (Delphi)             defined in sysvari.h (C++)
LongInt (Delphi)                int (C++)
                     64-bit iOS long (C++)
LongWord (Delphi)               unsigned (C++)
                     64-bit iOS unsigned long (C++)
FixedInt (Delphi)               int (C++)
FixedUInt (Delphi)              unsigned int (C++)
TextFile (Delphi)               TextFile (C++)

【讨论】:

    【解决方案2】:
    function process_message (const pmessage: PChar; const param: Pointer): Double; export; stdcall;
    begin
        If (pmessage = nil) Or (param = nil) Then
            Result := 0;
        Else If StrComp(pmessage, 'state') = 0 Then
           Result := process_state(current_state^(param));
    
        // missing a return statement for cases where pmessage is not 'state' here!
    end;
    
    exports process_message;
    

    未经测试,但应该有助于您入门。

    【讨论】:

      【解决方案3】:

      Pointer 数据类型完全等同于 C void*

      【讨论】:

        猜你喜欢
        • 2014-06-20
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        • 2018-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多