此代码直接从commitcrm.com网站复制--它演示了如何使用他们的API (像Hello一样,将连接到数据库,创建帐户,更新该帐户)
在Windows +Delphi2007上编译了完美和可执行完美。
如果在Windows 7+DelphiXE2/XE2 5/XE2 7上编译了完全相同的代码,那么可执行文件就会产生错误:(
我发现在某种程度上,它是与相关的,与Char / Ansichar,pChar / PAnsiChar。
因为,当我为CmtInitDbEngDll过程将char改为ansichar时,它实际上连接到数据库。太棒了。
但是我无法(我还不够好)找出我必须要做的是char、ansichar、pchar、pansichar等等,因为我不知道在涉及DLL时如何处理它。
有什么聪明的德尔菲人可以帮忙吗?
-源代码
program Demo;
{$APPTYPE CONSOLE}
uses
SysUtils,
Classes;
const
C_DataBuffSize = 1024;
C_MapBufSize = 1024;
C_ErrMsgBuffSize = 1024;
C_DescSize = 1024;
C_ErrCodeBuffSize = 64;
C_RecIDBuffSize = 64;
C_Flag = 1;
C_Ok = 1;
C_AccountsTable = 10;
C_AppName = 'Demo';
CmtDbEngDll = 'C:\CommitCRM\ThirdParty\UserDev\CmtDbEng.DLL';
var
Status: integer;
DataBuff: array [0..C_DataBuffSize] of Char;
MapBuff: array [0..C_MapBufSize] of Char;
RecIdBuff: array [0..C_RecIDBuffSize] of Char;
ErrCodesBuff: array [0..C_ErrCodeBuffSize] of Char;
ErrMsgBuff: array [0..C_ErrMsgBuffSize] of Char;
s: string;
//** Establishing connection with CommitCRM, Should be called only once for the entire session *
Procedure CmtInitDbEngDll (
xSoftWareName : PChar; // Your application name. Once selected this string
// will be used for all
// functions of the package. Specify a meaningful value.
xDbPath : PChar; // Path to the DB folder under where CommitCRM server is
// installed <server>\CommitCRM\Db
var xvStatus : integer // Returned connection status
); stdcall; external CmtDbEngDll;
//**** Insert/Update record
Procedure CmtInsUpdRec(
xSoftWareName : pChar; // See above
xDataKind : integer; // Desired Table Code
xDataBuff : pChar; // String containing the values, which we want
// to add into the Database
xMapBuff : pChar; // List of the database fields into
//which we want to add data
xContWhenInvalidData : Integer; //Flag - stop(0)/continue(1) the input process
// is some data value(s) is invalid
xFlags : Integer; // Not used
xRecIDBuffLen : Integer; // Length of REC ID Buffer
xLogErrCodesBuffLen : Integer; // Length of Error Code Buffer
xLogErrMsgBuffLen : Integer; // Length of Error Message Buffer
xvRecIDBuff : pChar; // Buffer for returned REC ID
xvErrCodesLogBuff : pChar; // Buffer for returned Error Codes
xvErrMsgLogBuff : pChar; // Buffer for returned Error Messages
var xvStatus : Integer // Returned status
); stdcall; external CmtDbEngDll;
//**** Terminate connection with CommitCRM ****
procedure CmtTerminateDbEngDll; stdcall; external CmtDbEngDll;
procedure CmtGetDescriptionByCode(
xCode : Integer;
xDescLen : Integer;
xvDesc : pChar); stdcall; external CmtDbEngDll;
procedure CmtGetDescriptionByStatus(
xCode : Integer;
xDescLen : Integer;
xvDesc : pChar); stdcall; external CmtDbEngDll;
procedure ErrCodesParsing (ErrCodeBuff: string);
var
lList: TStringList;
i: integer;
aDescErrCode : Pchar;
begin
try
lList := TStringList.Create;
lList.Text := ErrCodeBuff;
GetMem(aDescErrCode,C_DescSize);
for i := 0 to lList.Count - 1 do
begin
CmtGetDescriptionByCode(StrToInt(lList[i]), C_DescSize, aDescErrCode);
writeln('Error Code: '+lList[i]+' Desc: '+string(aDescErrCode));
end;
finally
FreeMem(aDescErrCode);
lList.Destroy;
end;
end;
procedure DisplayErrStatusCode(xCode : Integer);
var
aStatusErrCode : Pchar;
begin
try
GetMem(aStatusErrCode,C_DescSize);
CmtGetDescriptionByStatus(xCode,C_DescSize, aStatusErrCode);
writeln('Commit Init failed. Error code: '+Inttostr(xCode)+' Desc: '+string(aStatusErrCode));
finally
FreeMem(aStatusErrCode);
end;
end;
begin
//**** Establishing connection with CommitCRM, Should be called only once for the entire session
CmtInitDbEngDll(C_AppName, // Your application name. Once selected this string will be used
// for all functions of the package. Specify a meaningful value.
'C:\CommitCRM\Db\', // Path to the DB folder under where CommitCRM server is
// installed <server>\CommitCRM\Db
Status // Returned connection status
);
if Status = C_Ok then
begin
//**** Insert a new Account into the Accounts table ****
s := '"Bart De Hantsetters","De Hantsetters","Hantsetters"';
StrPCopy(DataBuff, s);
s := '"'+#13','+#13+'FLDCRDFULLNAME'+#13+'FLDCRDDEAR'+#13+'FLDCRDCONTACT'+#0;
StrPCopy(MapBuff, s);
CmtInsUpdRec(C_AppName, // Your application name
C_AccountsTable, // Desired Table Code
DataBuff, // String containing the values, which we want to add into
// the Database
MapBuff, // List of the Database Fields in which we want to add data
C_Flag, // Flag - stop(0)/continue(1) the input process is some data
// value(s) is invalid
0, // Not used
C_RecIDBuffSize, // Llength of REC ID Buffer
C_ErrCodeBuffSize, // Length of Error Code Buffer
C_ErrMsgBuffSize, // Length of Error Message Buffer
RecIdBuff, // Buffer for returned REC ID
ErrCodesBuff, // Buffer for returned Error Codes
ErrMsgBuff, // Buffer for returned Error Messages
Status // Returned status
);
if (ErrMsgBuff[0] <> #0) then
writeln('Error Message: '+ ErrMsgBuff);
ErrCodesParsing(ErrCodesBuff);
if Status = C_Ok then
begin
//**** Updating the Account record we've just created *****
// Map file for the update transaction - the Dear field and the record id
s := '"'+#13+','+#13+'FLDCRDDEAR'+#13'FLDCRDRECID';
StrPCopy(MapBuff, s);
s := '"Doctor","'+RecIdBuff+'"';
StrPCopy(DataBuff, s);
CmtInsUpdRec(C_AppName, // Your application name
C_AccountsTable, // Desired Table Code
DataBuff, // String containing the values, which we want
// to add into the Database
MapBuff, // List of the database fields into which we want to add
//data
C_Flag, // Flag - stop(0)/continue(1) the input process is some
// data value(s) is invalid
0, // Not used
C_RecIDBuffSize, // Length of REC ID Buffer
C_ErrCodeBuffSize, // Length of Error Code Buffer
C_ErrMsgBuffSize, // Length of Error Message Buffer
RecIdBuff, // Buffer for returned RECID
ErrCodesBuff, // Buffer for returned Error Codes
ErrMsgBuff, // Buffer for returned Error Messages
Status // Returned status
);
if ((ErrMsgBuff[0] <> #0)) then
writeln('Error Message: '+ ErrMsgBuff);
ErrCodesParsing(ErrCodesBuff);
if Status = C_Ok then
Writeln('Completed Successfully');
end
else
begin
try
s := IntToStr(Status);
except
s := 'ill-defined';
end;
writeln('Insert new Account. Error code: '+ s);
end;
//**** Terminate connection with CommitCRM****
CmtTerminateDbEngDll();
end
else
begin
DisplayErrStatusCode(Status);
end;
writeln(#13#10+'press Enter to quit');
readln;
end.发布于 2015-03-30 18:20:53
德尔福到并包括德尔福2007基本上是8位ANSI.Delphi 2009及更高版本是Unicode。
以下是两个可能有帮助的链接:
Embarcadero在2008年8月首次在RAD Studio中引入了完全的Unicode支持。在这样做的过程中,他们确保Delphi C++Builder将在很长一段时间内处于风ows平台上本地应用程序开发的前沿。 然而,与多年来在Delphi中引入的许多其他主要增强不同,例如变体和接口(Delphi 3)、框架(Delphi 5)、函数内联和嵌套类(Delphi 2005)和泛型(RAD Studio 2009),启用Unicode并不仅仅是向Delphi中已经支持的功能添加新特性。相反,它涉及对几乎每个Delphi应用程序中出现的几个基本数据类型的根本更改。具体来说,字符串、Char和PChar类型的定义发生了变化。 这些修改不是轻易通过的。相反,它们是在广泛考虑了这些变化对现有应用程序的影响以及它们将如何影响未来发展之后才引入的。此外,Embarcadero还征求了许多技术合作伙伴的意见和建议,他们支持和推广Delphi。 实际上,没有一些不便就无法实现Unicode支持。..。 字符串类型现在由UnicodeString类型定义,它是一个UTF-16字符串。类似地,Char类型现在是WideChar,一个双字节字符类型,而PChar是一个PWideChar,一个指向两个字节字符的指针。 对这些基本数据类型的更改的重要一点是,至少每个字符由至少一个代码单元(两个字节)表示,有时更多。
https://stackoverflow.com/questions/29353264
复制相似问题