Преобразование функции C ++ в Delphi: что делать с параметром void *?

StackOverflow https://stackoverflow.com/questions/1420234

Вопрос

Я пишу DLL в Delphi, используя приведенный ниже пример C ++:

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 ++ только для того, чтобы преобразовать пару строк, если кто-то достаточно любезен, чтобы сделать это для меня: -)

Это было полезно?

Решение

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;

Не проверено, но должно помочь вам начать работу.

Другие советы

Электронная документация RAD Studio включает в себя отображение типов Delphi на C ++ таблица, которая может помочь вам перевести код 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++)
Тип данных

Pointer является точным эквивалентом C void * .

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top