In the Delphi programming language used in the C code

Author:Anonymous    Updated:2008-2-26 22:09:52
Under Windows programming tools are many, such as VB, Delphi, VC, and so on. Here I do not want to discuss "their specific Which is better," this naive question. Played DOS program design people are aware, under DOS Many language is the real core of the system call the compilation interrupt functions. to Windows, it has become a We often say that the API. written in Windows and procedures are often called API, language is only one expression more than a tool.
I now have to work about six months, our company is using Borland's Delphi as the main development tool. Not partial to any in the position of a tool, I be fair: Delphi is currently under Win32 the most rapid development process, the most efficient tool.

Delphi suitable for the development of applications, but sometimes some things can be directly bottom of the use of C language development, in which I have always developed with hardware-related projects, and many hardware SDK package is written in C to write. This time I General them into Delphi (PASCAL) syntax of the code. Below talk about my personal experience shallow because the school is the Pascal language, so I was not too Confirmed C language. below views or if errors and omissions in the code experts hope to release the younger brother of a horse:)

1: C language compiler as the procedures for Delphi DLL calls this approach is too simple, and the need to bring an additional DLL files, so this is not within the scope of the discussion.

2: The direct conversion C language code to code DELPHI

C language function and Delphi different format, they are returned to the function of the former type, in a statement after the function. No return to the types of functions are defined as VOID type.

For example: Delphi function in the function MyFunction: (intIN: integer): Bool; corresponding C language code into Bool MyFunction (int intIN); another example procedure MyProcedure ;====> void MyProcedure applying this method, the general request more familiar with the C language. me is the use of this method. collected Below is my own common between Delphi and C types of correspondence table. type C which is the left, the right is the corresponding Delphi type:

The following is quoted fragment:
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



3: direct links in Delphi C language OBJ files.
The advantages of this is not the end without any external EXE file. Also not too familiar with the C language.

We all know that the code was compiled into an executable file (or DLL, OCX document, the same below), to have Mr. into OBJ files (generally DCU DELPHI documents, but also can edit compiler options generated OBJ files), then OBJ file and resource files (*. RES) links into the final executable. use of this method, we can directly link to the OBJ file our procedures inside.

However, it should be noted that, the compiler different generation of OBJ files are no longer the same. Microsoft compiler generated OBJ is COFF file format, and Borland C + + Builder is generated OMF format because we need in Delphi link CBC must be used, or Borland official site with the free compiler tools. Below we have adopted a simple example to illustrate specific steps:

This example is the provision of a simple function, a document used to determine whether Dat VCD format files. Header files the following statement:

The following is quoted fragment:
/ *
File name: DatFormat.h
* /
# Ifndef DatFormat_H
# Define DatFormat_H
# Include
# Pragma pack (push, 1) / / this and the following match, the general types of records used when needed definition, not practical here
# Ifdef __cplusplus
Extern "C" (
# Endif
Extern BOOL CheckIsDatFile (const char * FileName, BOOL IsDatFile *);
# Ifdef __cplusplus
)
# Endif
# Pragma pack (pop)
# Endif / / DatFormat_H
DatFormat.c concrete realization of the code as follows:
# Include "DatFormat.h"
BOOL CheckIsDatFile (const char * FileName, BOOL IsDatFile *)
/ *
Function Description: This function used to determine whether a document Dat document (VCD) format.
Parameters:
IN:
FileName: To determine the file name
IN, OUT:
IsDatfile: whether Dat format
OUT:
Reading the paper failed to return FALSE, or else return TRUE.
------------------------------------
Author: The Chen-tao .2004,01,17. Http://www.138soft.com, lovejingtao@21cn.com
* /
(
HANDLE hFile;
DWORD dwBytesRead;
BOOL re;
Char MyBuf [4];
* IsDatFile = FALSE;
/ / Read documents establishing handle
HFile = CreateFile (FileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
0);
If (hFile == INVALID_HANDLE_VALUE) return FALSE;
/ / Reading the paper
Re = ReadFile (hFile,
& MyBuf,
4.
& DwBytesRead,
NULL);
If (dwBytesRead! = 4)
(
CloseHandle (hFile);
Return FALSE;
)
/ / Read the document when failure
If (re! = TRUE)
(
CloseHandle (hFile);
Return FALSE;
)
CloseHandle (hFile);
* IsDatFile = (MyBuf [0] == 'R' & MyBuf [1] == 'I' & MyBuf [2] == 'F' & MyBuf [3] == 'F');
Return (TRUE);
)


Operation CBC, a new project, and then added to the project DatFormat.c inside, the compiler entire project, we will be in need of OBJ files: DatFormat.OBJ. CBC and then we can close, because Below no longer need it.

Operation Delphi, a new project and keep. DatFormat.OBJ then copy it to the directory under. Unit in the implementation Below add the following code:

($ LINK 'DatFormat.obj') / / link to external documents OBJ

Function _CheckIsDatFile (const FileName: Pchar; IsDatfile: PBool): Bool; cdecl; external; / / function definition. Cdecl which way the stack into the C-language format by passing a parameter. External that is a function of external statement.

Pay attention to the statement function prototype and the definition of C not the same. Must add an underscore in front. Reason is that the compiler links symbols. C and C + + is different, because this is not focused on this, so here is not for discussion. Please refer to themselves friends interested in the information.

Then we write the following code calls this function:

The following is quoted fragment:
Procedure TFrmMain.Button1Click (Sender: TObject);
Var
IsDatfile: Bool;
Begin
If OpenDialog1.Execute then
If _CheckIsDatFile (Pchar (OpenDialog1.FileName), @ IsDatFile) then
If IsDatFile then ShowMessage ( 'Congratulations! The document is a Dat format video files!')
Else ShowMessage ( 'I am sorry, the document is not a Dat format video files!')
Else ShowMessage ( 'Reading the paper wrong!');
End;


Compiler of this process, will be a clean executable EXE files.

4: C + + Builder modules used in Delphi

This is actually the way, but there is still talk about: Suppose we have a BIOS password access to the Delphi unit

Unit AwardBiosPas;

{================================================= ====

Projects: Delphi programming code used in the C language - Demonstration Program

Modules: BIOS password access modules

Description:

Version:

Date: 2004-01-17

Author: The Chen-tao. Lovejingtao@21cn.com, http://www.138soft.com

Update: 2004-01-17

================================================== =====}

The following is quoted fragment:
Interface
Uses
Windows, SysUtils;
Function My_GetBiosPassword: string;
Implementation
Function CalcPossiblePassword (PasswordValue: WORD): string;
Var
I: BYTE;
C: CHAR;
S: string [8];
Begin
I: = 0;
While PasswordValue do <> 0
Begin
Inc (I);
If $ 263> PasswordValue then
Begin
If $ 80> PasswordValue then
S [I]: = CHAR (PasswordValue)
Else if $ B0> PasswordValue then
S [I]: = CHAR (PasswordValue and $ 77)
Else if $ 11D> PasswordValue then
S [I]: = CHAR ($ 30 or (PasswordValue and $ 0F))
Else if $ 114> PasswordValue then
Begin
S [I]: = CHAR ($ 64 or (PasswordValue and $ 0F));
If'0 '> S [I] then
S [I]: = CHAR (BYTE (S [I]) + 8);
End
Else if $ 1C2> PasswordValue then
S [I]: = CHAR ($ 70 or (PasswordValue and $ 03))
Else if $ 1E4> PasswordValue then
S [I]: = CHAR ($ 30 or (PasswordValue and $ 03))
Else
Begin
S [I]: = CHAR ($ 70 or (PasswordValue and $ 0F));
If 'z' <then S [I]
S [I]: = CHAR (BYTE (S [I]) - 8);
End;
End
Else
S [I]: = CHAR ($ 30 or (PasswordValue and $ 3));
PasswordValue: = (PasswordValue - BYTE (S [I])) shr 2;
End;
S [0]: = CHAR (I);
PasswordValue: = I shr 1;
While PasswordValue <I do
(Begin this is to do because award starts calculating with the last letter)
C: S = [BYTE (S [0]) - I + 1];
S [BYTE (S [0]) - I + 1]: = S [I];
S [I]: = C;
Dec (I);
End;
CalcPossiblePassword: = S;
End;
Function readcmos (off: byte): byte;
Var
Value: byte;
Begin
Asm
Xor ax, the ax
Mov al, off
Out 70h, al
In al, 71h
Mov value, al
End;
Readcmos: = value;
End;
Function My_GetBiosPassword: string;
Var
Superpw, userpw: word;
Begin
If Win32Platform <> VER_PLATFORM_WIN32_NT then / / NT is not
Begin
Pchar (@ superpw) [0]: = char (readcmos ($ 1C));
Pchar (@ superpw) [1]: = char (readcmos ($ 1D));
Pchar (@ userpw) [0]: = char (readcmos ($ 64));
Pchar (@ userpw) [1]: = char (readcmos ($ 65));
Result: = ('************ BIOS passwords **********************')+# 13 + 'super users The password is: '+ CalcPossiblePassword (superpw) + + # 13' user password is: '+ CalcPossiblePassword (userpw);
End
Else
Result: = 'system for NT users, lack of access to BIOS password!';
End;
End.


CBC how directly in the use of it? A new CBC project, and then inside the unit added to the project. Specific operations: Add to Project ---> file types: pascal unit (*. pas), then Build Demo1. this time in the same directory AwardBiosPas.pas generate a AwardBiosPas.hpp document. use it to our needs call units. then can be called directly:

The following is quoted fragment:
Void __fastcall TFrmMain:: Button1Click (TObject * Sender)
(
ShowMessage (My_GetBiosPassword ());
)


5: other methods. We can of course use the C language RES generated binary files, but this method and the first approach almost. Advantage is not lost documents. Shortcomings of others is very easy to direct resources to amend tools open amended. This time can be The author made use of written procedures for tools PasAnywhere. But this is another topic.
Previous:Delphi embedded compilation case
Next:Using Delphi interesting animation of the mouse
User Reviews
Related Articles
Recommended article
AD