2009-09-14 133 views
2

我在Delphi使用下面的C++例子寫一個DLL:將C++函數轉換爲Delphi:如何處理void *參數?

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++只是轉換一個幾行的基礎知識,如果有人好心地爲我做的:-)

+3

學習C++的基礎知識是你應該做的事*無論你是否需要翻譯該代碼。如果你使用Delphi,那麼你在Windows上工作,如果你在Windows上工作,那麼你應該知道如何閱讀C和C++,因爲你看到的幾乎所有的文檔都是用這些語言編寫的。如果你不這樣做,你只是將你的頭伸向沙灘。 – 2009-09-14 13:43:47

+1

好點,Rob。只是我不是專業的軟件開發人員。我通常會做邏輯,Delphi的內置組件就是我所需要的。當我不得不處理一些事情時,這是一種罕見的情況。讚美stackoverflow :-) – Mikhail 2009-09-15 03:16:53

回答

7
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; 

未經檢驗,但應有助於讓你開始。

5

Pointer數據類型與C void*完全等價。

10

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++)