VC calls the DLL compiled by DELPHI

Source: Internet
Author: User

Delphi DLL code:

Library InspectWSDll;

Uses
SysUtils;

{$ R *. res}

Exports
Validate,
RetrieveDevicePage,
RetrieveDevicePage1,
RetrieveInspectionPersonPage,
RetrieveInspectionPersonPage1;

 

The following is the implementation file:

Unit Unt_UserLogin;

Interface

Uses DBAccessService, GlobeUtils, Dialogs, SysUtils, CnDebug;

Type
PTUser = ^ TUser _;

TUser _ = record
Id: Int64;
Name: WideString;
Pass: WideString;
Region: WideString;
Username: WideString;
Usertype: Int64;
End;

Function Validate (UserName, Password: PChar; P: PTUser): Integer; stdcall;

Function TUser_ToTUser (obj: TUser _): TUser;
Function TUserToTUser _ (Tobj: TUser): TUser _;

Implementation

Function Validate (UserName, Password: PChar; P: PTUser): Integer;
Var
A: PTUser;
FUser: TUser;
Begin
Validate: =-1;

FUser: = TUser. Create;
Try

FUser: = FService. validate (UserName, Password );

A: = PTUser (P );
With A ^ do
Begin
Id: = fUser. id;
Name: = fUser. name;
Pass: = fUser. pass;
Region: = fUser. region;
Username: = fUser. username;
Usertype: = fUser. usertype;
End;
Validate: = 0;
Except
Validate: = predictioncode;
End;

End;

Function TUserToTUser _ (Tobj: TUser): TUser _;
Var
R: TUser _;
Begin
With R do
Begin
Id: = Tobj. id;
Name: = Tobj. name;
Pass: = Tobj. pass;
Region: = tobj. region;
Username: = tobj. Name;
Usertype: = tobj. usertype;
End;

Tusertotuser _: = R;
End;

Function tuser_totuser (OBJ: Tuser _): Tuser;
VaR
R: Tuser;
Begin
R: = Tuser. Create;

With r do
Begin
ID: = obj. ID;
Name: = obj. Name;
Pass: = obj. pass;
Region: = obj. region;
Username: = obj. Name;
Usertype: = obj. usertype;
End;

TUser_ToTUser: = R;
End;

End.

========================================================== ==================================

VC call code:

Struct TUser
{
_ Int64 id;
BSTR name;
BSTR pass;
BSTR region;
BSTR username;
_ Int64 usertype;
};

Typedef int (CALLBACK * Validate) (char * Username, char * password, TUser * tu );

Validate validate;

HDLL = LoadLibrary ("G: // workspace // Delphi_WorkSpace // InspectWSDll // Bin // InspectWSDll. dll ");
 
If (hDLL! = NULL ){
Validate = (Validate) GetProcAddress (hDLL, "Validate ");

Struct TUser user;
ZeroMemory (& user, sizeof (TUser ));

Int a = validate (LPSTR) (LPCTSTR) "test", (LPSTR) (LPCTSTR) "860821", & user );
If (! = 0)
{
AfxMessageBox ("Failed! ");
}
Else
{
AfxMessageBox ("OK! ");
}

}

========================================================== ============

Note that if you want to pass a pointer to a DLL, you must allocate the memory in the main program.

The Unt_TDevice code is attached:

Unit Unt_TDevice;

Interface

Uses
Dialogs, DBAccessService, XSBuiltIns, CnDebug, SysUtils, Activex, Classes, GlobeUtils;

Type
PTTDevice = ^ TTDevice;

TTDevice = record
Company: WideString;
Flag: Int64;
ID: int64;
Region: widestring;
Seriesnumber: widestring;
Stamp: widestring;
End;

Ptdevicepage = ^ tdevicepage;

Tdevicepage = record
Flist: pointer; // This is a pointer to ttdevice.
Fpageamount: integer;
Fpagenum: integer;
End;

Function retrievedevicepage1 (const pageindex: integer; const pagesize: integer; paramstr _: pchar; datestr: pchar; P: pointer): integer; stdcall;
Function retrievedevicepage (const pageindex: integer; const pagesize: integer; paramstr _: pchar; P: pointer): integer; stdcall;

Function tdevicetottdevice (OBJ: tdevice): ttdevice;
Function ttdevicetotdevice (tobj: ttdevice): tdevice;

Implementation

 

Function TDeviceToTTDevice (Obj: TDevice): TTDevice;
Var
R: TTDevice;
Begin
With R do
Begin
Company: = Obj. company;
Flag: = Obj. flag;
Id: = Obj. id;
Region: = Obj. region;
Seriesnumber: = Obj. seriesnumber;
Try
Stamp: = DateTimeToStr (Obj. stamp. AsDateTime );
Except
Stamp: = DateTimeToStr (now );
End;
End;
TDeviceToTTDevice: = R;
End;

Function TTDeviceToTDevice (Tobj: TTDevice): TDevice;
Var
W: TDevice;
Dtm: TXSDateTime;
Begin
W: = TDevice. Create;

With w do
Begin
Company: = Tobj. company;
Flag: = Tobj. flag;
Id: = Tobj. id;
Region: = Tobj. region;
Seriesnumber: = Tobj. seriesnumber;
Try
Dtm: = DateTimeToXSDateTime (StrToDateTime (Tobj. stamp ));
Except
Dtm: = DateTimeToXSDateTime (now );
End;
Stamp: = dtm;
End;
TTDeviceToTDevice: = w;
End;

Function RetrieveDevicePage (const PageIndex: Integer; const PageSize: Integer; ParamStr _: PChar; P: Pointer): Integer;
Var
LstIndex: Integer;
A: PTDevicePage;
RstPage: DevicePage;
Pttdevice _: pttdevice;
Begin

Retrievedevicepage: =-1;
Try
Rstpage: = devicepage. Create;
Rstpage: = fservice. retrievedevicepage (pageindex, pagesize, paramstr _);

A: = ptdevicepage (P );
A ^. fpageamount: = rstpage. pageamount;
A ^. fpagenum: = rstpage. pagenum;
Pttdevice _: = A. flist;

For lstindex: = low (rstpage. List) to high (rstpage. List) Do
Begin
Pttdevice _ ^. Company: = trim _ (rstpage. list [lstindex]. Company );
Pttdevice _ ^. Flag: = rstpage. list [lstindex]. flag;
Pttdevice _ ^. ID: = rstpage. list [lstindex]. ID;
Pttdevice _ ^. region: = trim _ (rstpage. list [lstindex]. region );
PtTDevice _ ^. seriesnumber: = Trim _ (RstPage. list [LstIndex]. seriesnumber );
PtTDevice _ ^. stamp: = DateTimeToStr _ (RstPage. list [LstIndex]. stamp );

Inc (PtTDevice _);
End;

RetrieveDevicePage: = High (RstPage. list) + 1;
Except
RetrieveDevicePage: =-100;
End;

End;

Function RetrieveDevicePage1 (const PageIndex: Integer; const PageSize: Integer; ParamStr _: PChar; DateStr: PChar; P: Pointer): Integer;
Var
LstIndex: Integer;
A: PTDevicePage;
RstPage: DevicePage;
PtTDevice _: PTTDevice;
Dt: TDateTime;
Begin

RetrieveDevicePage1: =-1;
Rstpage: = devicepage. Create;
Try
DT: = strtodatetime (datestr );
DT: = DT-ftimezone/24;
Rstpage: = fservice. retrievedevicepage1 (pageindex, pagesize, paramstr _, datetimetoxsdatetime (DT ));
Except
Rstpage: = nil;
Retrievedevicepage1: =-100;
Exit;
End;

Try

A: = ptdevicepage (P );
A ^. fpageamount: = rstpage. pageamount;
A ^. fpagenum: = rstpage. pagenum;
Pttdevice _: = A. flist; // multiple tdevices are included.

For lstindex: = low (rstpage. List) to high (rstpage. List) Do
Begin
Pttdevice _ ^. Company: = trim _ (rstpage. list [lstindex]. Company );
PtTDevice _ ^. flag: = RstPage. list [LstIndex]. flag;
PtTDevice _ ^. id: = RstPage. list [LstIndex]. id;
PtTDevice _ ^. region: = Trim _ (RstPage. list [LstIndex]. region );
PtTDevice _ ^. seriesnumber: = Trim _ (RstPage. list [LstIndex]. seriesnumber );
PtTDevice _ ^. stamp: = DateTimeToStr _ (RstPage. list [LstIndex]. stamp );

Inc (PtTDevice _); pointer moves forward
End;

RetrieveDevicePage1: = High (RstPage. list) + 1;
Except
RetrieveDevicePage1: =-100;
End;

End;

Initialization

Finalization

End.

Defined in VC:
Struct TDevice
{
BSTR company;
_ Int64 flag;
_ Int64 id;
BSTR region;
BSTR seriesnumber;
BSTR stamp;
};

Struct DevicePage
{
TDevice * list;
Int pageAmount;
Int pageNum;
};

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.