extern "C" int __declspec(dllexport) add(int x, int y);

Delphi中例子: function add(i:Integer; j:Integer):Integer; cdecl; External 'NonMfcDll.dll';

  • 声明中加上__stdcall C++中例子: extern "C" int __declspec(dllexport) __stdcall stdadd(int x, int y); 因为加上__stdcall关键字, 会导致函数名分裂. 此时函数名变成_stdadd@8. 其中, 8是参数的总字节数
  • Delphi引用的方法1: 在delphi定义中加上"name'_stdadd@8'".

    function stdadd(i:Integer; j:Integer):Integer; stdcall; External 'NonMfcDll.dll' name'_stdadd@8';

    方式一 var procedure test8(var a: TRect); cdecl; external Windll name 'test8';

    方式二 const procedure test8(const a: TRect); cdecl; external Windll name 'test8';

    在C#用Struct非常方便,但Delphi竟然没有Struct。最后发现了Record,转载此文章以备使用。

     Delphi             转换         C/C++
    SysUtils;      转换          #include "sysutils.h"

    const CPU_TYPE_INTEL = 1; 转换 extern const int CPU_TYPE_INTEL;

    type TVendor = array [0..11] of char; 转换 typedef char TVendor [ 12 ];/* range 0..11*/

    type TCPUID = array[1..4] of longint; 转换 typedef int TCPUID [ 5 ];

    type struct _SYSTEM_PERFORMANCE_INFORMATION { _SYSTEM_PERFORMANCE_INFORMATION = record LARGE_INTEGER IdleTime; IdleTime: LARGE_INTEGER; DWORD Reserved [ 76 ];/* range 0..75*/
    Reserved: array[0..75] of DWORD; }; end; 转换

    CPUType: Byte; 转换 unsigned char CPUType;

    DTS: Boolean; 转换 bool DTS;

    function Get_CPUSpeed(var CpuSpeed: Double): Boolean; 转换 bool __fastcall Get_CPUSpeed ( double & CpuSpeed );

    function GetCPUSpeed: Cardinal; 转换 unsignedint __fastcall GetCPUSpeed ( );

    function GetCPU_Speed : string; 转换 String __fastcall GetCPU_Speed ( );

    procedure CollectCPUData; 转换 void __fastcall CollectCPUData ( );

    function GetCPUCount: Integer; 转换 int __fastcall GetCPUCount ( );

    function GetCPUUsage(Index: Integer): Double; 转换 double __fastcall GetCPUUsage ( int Index );

    const CPU_TYPE_INTEL = 1; 转换 const int CPU_TYPE_INTEL = 1;

    function HasCPUIDInstruction: Boolean; 转换 bool __fastcall HasCPUIDInstruction ( )

    Delphi 代码 PUSHFD POP EAX MOV ECX, EAX XOR EAX, ID_FLAG AND ECX, ID_FLAG PUSH EAX POPFD PUSHFD POP EAX AND EAX, ID_FLAG XOR EAX, ECX SETNZ Result

    转换 成 C++

    PUSHFD POP EAX MOV ECX, EAX XOR EAX, ID_FLAG AND ECX, ID_FLAG PUSH EAX POPFD PUSHFD POP EAX AND EAX, ID_FLAG XOR EAX, ECX SETNZ Result

    const ID_FLAG = $200000; 转换 const int ID_FLAG = 0x200000;

    CPUInfo.DTS := (EAX AND 1) = 1; 转换 CPUInfo.DTS = ( EAX && 1 ) == 1;

    $80000000 转换 0x80000000 AND 转换 &&

    CpuInfo.PhysicalCore := ((EAX AND $FC000000) shr 26) + 1; 转换 CpuInfo.PhysicalCore = ( ( EAX && 0xFC000000 ) >> 26 ) + 1;

    const PCRAddress = $0cf8; 转换 const int PCRAddress = 0x0cf8;

    function ExtractRes(FileName: KolString): Boolean; 转换 bool __fastcall ExtractRes ( KolString FileName )

    Delphi 代码 Procedure GetPCIRDWord( dwBus, dwDev, dwFunc, offs : byte; var pdata:DWord); Data: Cardinal; begin Data := 80000000or(longint(dwBus)shl16)or((longint(dwDev)and80000000 or (longint(dwBus) shl 16) or ((longint(dwDev) and 1f) shl 11) or ((longint(dwFunc) and 07)shl8)or(offsand07 ) shl 8) or (offs and fc); PortDWordOutNT(PCRAddress, Data); pData := PortDWOrdInNT(PCRData);

    转换 成 C++

    void __fastcall GetPCIRDWord ( unsignedchar dwBus, unsignedchar dwDev, unsignedchar dwFunc, unsignedchar offs, DWORD & pdata ) unsigned int Data; Data = 0x80000000 || ( longint ( dwBus ) << 16 ) || ( ( longint ( dwDev ) && 0x1F ) << 11 ) || ( ( longint ( dwFunc ) && 0x07 ) << 8 ) || ( offs && 0xfc ); PortDWordOutNT ( PCRAddress, Data ); pdata = PortDWordInNT ( PCRData );

    if ((Modrm and $c0 = 0) and (Rm = 5)) then 转换 if ( ( Modrm && 0xc0 == 0 ) && ( Rm == 5 ) )

    V1:=(ver and $FFFF0000) shr 16 ; 转换 V1 = ( ver && 0xFFFF0000 ) >> 16;

    for j:=0 to 5 do 转换 for ( J = 0; J != 5; J ++ )

    n1,n2,n3,n4,n5 : Integer; 转换 int n1, n2, n3, n4, n5;

    Delphi 代码 n1 := byte(RegCode[1]);

    n2 := (n1*n1)*32 ;

    n3 := n2+(n1*n1);

    n4 := n3 mod $19 ;

    n5 := n4+$41;

    RegCode[2] := char(n5);

    //计算生成 RegCode[3]

    n1 := byte(RegCode[1]) * byte(RegCode[2]);

    n2 := n1*$15;

    n3 := n2 mod $19;

    n4 := n3+$41;

    RegCode[3] := char(n4);

    转换 成 C++

    //计算生成 RegCode[2] n1 = byte ( RegCode [ 1 ] ); n2 = ( n1 * n1 ) * 32; n3 = n2 + ( n1 * n1 ); n4 = n3 % 0x19; n5 = n4 + 0x41; RegCode [ 2 ] = char ( n5 );

    //计算生成 RegCode[3] n1 = byte ( RegCode [ 1 ] ) * byte ( RegCode [ 2 ] ); n2 = n1 * 0x15; n3 = n2 % 0x19; n4 = n3 + 0x41; RegCode [ 3 ] = char ( n4 );

    Delphi 代码 function UpperCase(sString: string): string; stdcall; Ch : char; L : Integer; Source: PChar; Dest : PChar; begin L := Length(sString); SetLength(Result, L); Source := Pointer(sString); Dest := Pointer(Result); while L <> 0 do begin Ch := Source^; if (Ch >= 'a') AND (Ch <= 'z') then Dec(Ch, 32); Dest^ := Ch; Inc(Source); Inc(Dest); Dec(L);

    转换 成 C++

    String __stdcall UpperCase ( String sString ) String result; char Ch; int L; PChar Source; PChar Dest; L = sString.Length ( ); result.SetLength ( L ); Source = Pointer ( sString ); Dest = Pointer ( result ); while ( L != 0 ) Ch = ( * Source ); if ( ( Ch >= 'a' ) && ( Ch <= 'z' ) ) Ch -= 32; ( * Dest ) = Ch; Source ++; Dest ++; L ++; return result;

    Delphi 代码 PPointer(DWord(pMap)+3)^ := ZW; //Pointer(DWord(ZW)-DWord(pMap)+ntoskrnl); PDWord(DWord(pMap)+8)^ := PDWord(DWord(ZW)+0)^; PDWord(DWord(pMap)+18)^ := PDWord(DWord(ZW)+4)^;

    pByte(DWord(ZW)+0)^ := $68;
    PPointer(DWord(ZW)+1)^ := Pointer(ntoskrnl);
    pByte(DWord(ZW)+5)^ := $C3;
    Result := OpenProcess($1337, False, $1337) = $1337;
    

    转换 成 C++ ( * PPointer ( DWord ( pMap ) + 3 ) ) = ZW; //Pointer(DWord(ZW)-DWord(pMap)+ntoskrnl); ( * PDWord ( DWord ( pMap ) + 8 ) ) = ( * PDWord ( DWord ( ZW ) + 0 ) ); ( * PDWord ( DWord ( pMap ) + 18 ) ) = ( * PDWord ( DWord ( ZW ) + 4 ) ); ( * pByte ( DWord ( ZW ) + 0 ) ) = 0x68; ( * PPointer ( DWord ( ZW ) + 1 ) ) = Pointer ( ntoskrnl ); ( * pByte ( DWord ( ZW ) + 5 ) ) = 0xC3; result = OpenProcess ( 0x1337, false, 0x1337 ) == 0x1337;

    Delphi 代码 function GetUptime32: string; Tiempo, Dias, Horas, Minutos: Cardinal; begin Result :=''; Tiempo := GetTickCount(); Dias := Tiempo DIV (1000 * 60 * 60 * 24); Tiempo := Tiempo - Dias * (1000 * 60 * 60 * 24); Horas := Tiempo DIV (1000 * 60 * 60); Tiempo := Tiempo - Horas * (1000 * 60 * 60); Minutos:= Tiempo DIV (1000 *60); Result := IntToStr(Dias) + '天' + IntToStr(Horas) + '小时' + IntToStr(Minutos) + '分';

    转换 成 C++ String __fastcall GetUptime32 ( ) String result; unsigned int Tiempo, Dias, Horas, Minutos; result = ""; Tiempo = GetTickCount ( ); Dias = Tiempo / ( 1000 * 60 * 60 * 24 ); Tiempo = Tiempo - Dias * ( 1000 * 60 * 60 * 24 ); Horas = Tiempo / ( 1000 * 60 * 60 ); Tiempo = Tiempo - Horas * ( 1000 * 60 * 60 ); Minutos = Tiempo / ( 1000 * 60 ); result = IntToStr ( Dias ) + "天" + IntToStr ( Horas ) + "小时" + IntToStr ( Minutos ) + "分"; return result;

    Delphi 代码 const BlockCode: array[1..2] of Byte = ( 59,POPECX59, { POP ECX } E9); { JMP StdWndProc } 转换 成 C++ unsigned char BlockCode [ 3/* range 1..2*/ ] /* POP ECX / = { 0x59 , 0xE9 }; / JMP StdWndProc */

    Delphi 代码 Block^.Next := InstBlockList; 转换 成 C++ ( * Block ).Next = InstBlockList;

    Delphi 代码 Move(BlockCode, Block^.Code, sizeof(BlockCode)); 转换 成 C++ Move ( BlockCode, ( * Block ).Code, sizeof ( BlockCode ) );

    Delphi 代码 Instance := @Block^.Instances; Delphi=@ /// C++=& 转换 成 C++ Instance = & ( * Block ).Instances;

    Delphi 代码 if P^ = #0 then 转换 成 C++

    if ( ( * P ) == '\x00' )

    Delphi 代码 TRampArray = array[0..2] of array[Byte] of Word; 转换 成 C++ typedef unsigned short [ unsigned char ] TRampArray [ 3 ];

    Delphi 代码 s[i] := char(ord(s[i]) xor 444); 转换 成 C++ s [ i ] = char ( int ( s [ i ] ) ^ 444 );

    Delphi 代码 for i := 1 to length(s) do 转换 成 C++ for ( i = 1; i != s.Length ( ); i ++ )

    Delphi 代码 Inc(iCount); 转换 成 C++ iCount ++;

    Delphi 代码 User : array[0..255] of char; 转换 成 C++ char User [ 256/* range 0..255*/ ];

    Delphi 代码 while pptr^[i] <> nil do 转换 成 C++ while ( ( * pptr ) [ i ] != NULL )

    Delphi 代码 else if (OSINFO.dwMajorVersion = 5) and (OSINFO.dwMinorVersion = 1) then result := 2//'Windows XP' 转换 成 C++ if ( ( OSINFO.dwMajorVersion == 5 ) && ( OSINFO.dwMinorVersion == 1 ) ) result = 2//'Windows XP'

    Delphi 代码 PKeyboardState = ^TKeyboardState; TKeyboardState = array[0..255] of Byte; 转换 成 C++ BYTE keyState[256] GetKeyboardState((LPBYTE)&keyState);

    在windef.h头文件中有如下定义 #define WINAPI __stdcall #define APIENTRY WINAPI WINAPI 和 APIENTRY 所以实际上 WINAPI和APIENTRY是一样的

    !true=false;

    !1=0;

    !0=1;

    // GetMem(ptr, sizeof(integer) * 20); //这句等价于C的 ptr = (int*) malloc(sizeof(int) * 20);

    转换 成 C++ pPhysicalMonitors = (LPPHYSICAL_MONITOR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cPhysicalMonitors * sizeof(PHYSICAL_MONITOR)); Delphi 代码 pPhysicalMonitorArray:=HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,cPhysicalMonitors DIV sizeof(PHYSICAL_MONITOR));//GetMem(cPhysicalMonitors,sizeof(PHYSICAL_MONITOR));//SizeOf(cPhysicalMonitors);

    Delphi 代码 const ENUM_CURRENT_SETTINGS = DWORD(-1);

    转换 成 C++ const int ENUM_CURRENT_SETTINGS = DWORD ( - 1 );

    Delphi 代码 GUID_DEVCLASS_MONITOR: TGuid = '{4D36E96E-E325-11CE-BFC1-08002BE10318}';

    转换 成 C++ TGuid GUID_DEVCLASS_MONITOR = "{4D36E96E-E325-11CE-BFC1-08002BE10318}";

    Delphi 代码 TDevModMonitor = record dmPosition: TPoint; dmDisplayOrientation: DWORD; dmDisplayFixedOutput: DWORD; PDevModMonitor = ^TDevModMonitor;

    转换 成 C++ struct TDevModMonitor { TPoint dmPosition; DWORD dmDisplayOrientation; DWORD dmDisplayFixedOutput; typedef TDevModMonitor *PDevModMonitor;

    Delphi 代码 SP_DEVINFO_DATA = packed record cbSize: DWORD; ClassGuid: TGuid; DevInst: DWORD; // DEVINST handle Reserved: ULONG_PTR;

    转换 成 C++ struct SP_DEVINFO_DATA { DWORD cbSize; TGuid ClassGuid; DWORD DevInst; // DEVINST handle ULONG_PTR Reserved; typedef SP_DEVINFO_DATA TSPDevInfoData; typedef TSPDevInfoData *PSPDevInfoData;

    Delphi 代码 AMonitor.FSN := (S[12] shl 24) or (S[13] shl 16) or (S[14] shl 8) or S[15];

    转换 成 C++ AMonitor->FSN = ( S [ 12 ] << 24 ) || ( S [ 13 ] << 16 ) || ( S [ 14 ] << 8 ) || S [ 15 ];

    Delphi 代码 case DN of Master : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS; Microphone : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; WaveOut : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; Synth : mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER;

    转换 成 C++ switch ( DN ) case Master: mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS; break; case Microphone: mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE; break; case WaveOut: mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT; break; case Synth: mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_SYNTHESIZER; break;

    Delphi 代码 FillChar(mxlc, SizeOf(mxlc),0); //@mxc

    转换 成 C++ setmem ( (void*) mxlc, sizeof ( mxlc ), 0 ) /* todo #include <mem.h> */; //& mxc

    Delphi 数据类型 C/C++

    ShorInt 8位有符号整数 char Byte 8位无符号整数 BYTE,unsigned short SmallInt 16位有符号整数 short Word 16位无符号整数 unsigned short Integer,LongInt 32位有符号整数 int,long Cardinal,LongWord/DWORD 32位无符号整数 unsigned long Int64 64位有符号整数 _int64 Single 4字节浮点数 float *Real48 6字节浮点数   Double 8字节浮点数 double Extended 10字节浮点数 long double Currency 64位货币类型   TDate/TDateTime 8字节日期/时间   Variant,OleVariant 16字节可变类型 VARIANT,^Variant,^OleVariant Char,AnsiChar 1字节字符 char WideChar 2字节字符 WCHAR ShortString 短字符串   AnsiString/String 长字符串 ^AnsiString WideString 宽字符串 ^WideString PChar,PAnsiChar NULL结束的字符串 char PWideChar NULL结束的宽字符串 LPCWSTR Boolean,ByteBool 1字节布尔类型 任何1字节 WordBool 2字节布尔类型 任何2字节 BOOL,LongBool 4字节布尔类型 BOOL 注:有前缀的是向前兼容类型;有^前缀的是C++Builder特有类型。

    C/C++ Delphi ABC TABC ACCEL TAccel ATOM TAtom BITMAP TBitMap BITMAPCOREHEADER TBitmapCoreHeader BITMAPCOREINFO TBitmapCoreInfo BITMAPFILEHEADER TBitmapFileHeader BITMAPINFO TBitmapInfo BITMAPINFOHEADER TBitmapInfoHeader BOOL Bool CBT_CREATEWND TCBT_CreateWnd CBTACTIVATESTRUCT TCBTActivateStruct CHAR Char CHAR* PChar CLIENTCREATESTRUCT TClientCreateStruct COLORREF TColorRef COMPAREITEMSTRUCT TCompareItemStruct COMSTAT TComStat CREATESTRUCT TCreateStruct CTLINFO TCtlInfo CTLSTYLE TCtlStyle CTLtype TCtltype DCB TDCB DDEAACK TDDEAck DDEADVISE TDDEAdvise DDEDATA TDDEData DDEPOKE TDDEPoke DEBUGHOOKINFO TDebugHookInfo DELETEITEMSTRUCT TDeleteItemStruct DEVMODE TDevMode DOUBLE Double DRAWITEMSTRUCT TDrawItemStruct DWORD LongInt ENUMLOGFONT TEnumLogFont EVENTMSG TEventMsg FARPROC TFarProc FIXED TFixed FLOAT Single GLYPHMETRICS TGlyphMetrics HANDLE THandle HANDLETABLE THandleTable HARDWAREHOOKSTRUCT THardwareHookStruct HELPWININFO THelpWinInfo INT Integer KERNINGPAIR TKerningPair LOGBRUSH TLogBrush LOGFONT TLogFont LOGPALETTE TLogPalette LOGPEN TLogPen LONG LongInt LONG DOUBLE Extended LONG INT LongInt LPSTR PChar LPWSTR PWideChar MAT2 TMat2 MDICREATESTRUCT TMDICreateStruct MEASUREITEMSTRUCT TMeasureItemStruct MENUITEMTEMPLATE TMenuItemTemplate MENUITEMTEMPLATEHEADER TMenuItemTemplateHeader METAFILEPICT TMetaFilePict METAHEADER TMetaHeader METARECORD TMetaRecord MINMAXINFO TMinMaxInfo MOUSEHOOKSTRUCT TMouseHookStruct MSG TMsg MULTIKEYHELP TMultiKeyHelp NCCALCSIZE_PARAMS TNCCalcSize_Params NEWTEXTMETRIC TNewTextMetric OFSTRUCT TOFStruct OUTLINETEXTMETRIC TOutlineTextMetric PAINTSTRUCT TPaintStruct PALETTEENTRY TPaletteEntry PANOSE TPanose PATTERN TPattern POINTFX TPointFX PSTR PChar PWSTR PWideChar RASTERIZER_STATUS TRasterizer_Status RGBQUAD TRGBQuad RGBTRIPLE TRGBTriple SEGINFO TSegInfo SHORT SmallInt SHORT INT SmallInt SIZE TSize TEXTMETRIC TTextMetric TPOINT TPoint TRECT TRect TTPOLYCURVE TTTPolyCurve TTPOLYGONHEADER TPolygonHeader UINT Word UNSIGNED Word UNSIGNED CHAR Byte UNSIGNED INT Word UNSIGNED LONG LongInt(DWORD) UNSIGNED LONG INT LongInt UNSIGNED SHORT Word UNSIGNED SHORT INT Word VOID* Pointer WINDOWPLACEMENT TWindowPlacement WINDOWPOS TWindowPos WNDCLASS TWndClass WORD Word

    /////////////////////////动态调用DLL

    TSetKey = function (KeyState: TKeyboardState):BOOL; stdcall;//定义一个函数类型,注意一点过程类型种的参数应该与DLL中用到的方法的参数一致。

    procedure TForm1.Button3Click(Sender: TObject); KeyState: TKeyBoardState;

    myhandle:THandle; FPointer:Pointer; Myfun :TSetKey;

    begin myhandle:=LoadLibrary('user32.dll') ;//加载这个DLL

    GetKeyBoardState(KeyState); //设置capsLock有效 if KeyState[VK_NUMLOCK] = 0 then //VK_NUMLOCK VK_CAPITAL begin KeyState[VK_NUMLOCK] := 0; // SetCapsLockOn; // SetKeyBoardState(KeyState); Label1.Caption:='0';

    begin KeyState[VK_NUMLOCK] := 0; //0 Label1.Caption:='0'; // SetCapsLockOFF; // SetKeyBoardState(KeyState); // CallFunc('user32.dll', 'SetKeyboardState', ['KeyState']); FPointer :=GetProcAddress(myhandle,PChar('SetKeyboardState')); //取函数的地址。 if FPointer <>nil then //如果函数存在就调用 begin Myfun := TSetKey(FPointer); Myfun(KeyState); Sleep(5); //showmessage(IntToStr(Myfun(StrToInt(edt1.Text),StrToInt(edt2.Text))));//弹出算出的结果。 except FreeLibrary(myhandle);

    PVIOD 一个普通指针类型等价于(viod *) 还有一些对你有用 win api 编程中的数据类型很多,有没有人能 具体讲讲啊? CALLBACK 在应用程序的回调例程中取代FAR PASCAL HANDLE 一个32位的无符号整数,用作句柄 HDC 设备描述句柄 HWND 一个32位的无符号整数用作窗口句柄 LONG 一个32位的带符号整数 LPARAM 用于声明lParam的类型 LPCSTR 与LPSTR类似,但用于只读字符串指针 LPSTR 一个32位的指针 LPVIOD 一个普通指针类型等价于(viod *) LRESULT 子窗口过程的返回值 NULL 一个整型的0值,常常用于激活函数的缺省动作和参数 UINT 一种无符号的整数类型,其大小取决于主机环境;在NT下是32位 WCHAR 一种16位的UNICODE字符,用于表示世界上所有语言的符号。 WINAPI 在API的定义中取代FAR PASCAL WPARAM 关于wParam的声明

    function GetHzPy(const AHzStr: string): string; const ChinaCode: array[0..25, 0..1] of Integer = ((1601, 1636), (1637, 1832), (1833, 2077), (2078, 2273), (2274, 2301), (2302, 2432), (2433, 2593), (2594, 2786), (9999, 0000), (2787, 3105), (3106, 3211), (3212, 3471), (3472, 3634), (3635, 3722), (3723, 3729), (3730, 3857), (3858, 4026), (4027, 4085), (4086, 4389), (4390, 4557), (9999, 0000), (9999, 0000), (4558, 4683), (4684, 4924), (4925, 5248), (5249, 5589));

    分类:
    后端
  •