可能您的错误甚至是一组错误是在 ODBC 标头的错误翻译中,然后可能用于非 Unicode 或 Unicode Delphi 版本。例如:
- 对于 Unicode Delphi,您宁愿使用
XxxW
(UTF16) 函数ODBCCP32.DLL
,而不是Xxx
(ANSI) 函数;
- 对于非 Unicode Delphi 而不是
Xxx
函数。然后wideChars
应定义为array[..] of Char
;
SqlConfigDataSource
可以XxxW
用 PAnsiChar 定义;
- 等等
我想向您展示这个想法,因为没有完整的来源,我只能推测。然后你有可疑的电话StringToWideChar(strAttributes, wideChars, 12);
。strAttributes 值比 12 个字符长得多。
以下代码在 Delphi XE2 中运行良好:
type
SQLHWnd = LongWord;
SQLChar = Char;
PSQLChar = ^SQLChar;
SQLBOOL = WordBool;
UDword = LongInt;
PUDword = ^UDword;
SQLSmallint = Smallint;
SQLReturn = SQLSmallint;
const
SQL_MAX_MESSAGE_LENGTH = 4096;
ODBC_ADD_DSN = 1; // Add data source
ODBC_CONFIG_DSN = 2; // Configure (edit) data source
ODBC_REMOVE_DSN = 3; // Remove data source
ODBC_ADD_SYS_DSN = 4; // add a system DSN
ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN
ODBC_REMOVE_SYS_DSN = 6; // remove a system DSN
ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN
function SQLConfigDataSource (
hwndParent: SQLHWnd;
fRequest: WORD;
lpszDriver: PChar;
lpszAttributes: PChar
): SQLBOOL; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl {$ENDIF};
external 'odbccp32.dll' name 'SQLConfigDataSourceW';
function SQLInstallerError (
iError: WORD;
pfErrorCode: PUDword;
lpszErrorMsg: PChar;
cbErrorMsgMax: WORD;
pcbErrorMsg: PWORD
): SQLReturn; {$IFDEF MSWINDOWS} stdcall {$ELSE} cdecl {$ENDIF};
external 'odbccp32.dll' name 'SQLInstallerErrorW';
procedure TForm616.Button1Click(Sender: TObject);
var
strAttributes: string;
pfErrorCode: UDword;
errMsg: PChar;
begin
strAttributes := 'DSN=' + 'example_DSN' + Chr(0);
strAttributes := strAttributes + 'DESCRIPTION=' + 'description' + Chr(0);
strAttributes := strAttributes + 'SERVER=' + 'testserver' + Chr(0);
strAttributes := strAttributes + 'DATABASE=' + 'somedatabase' + Chr(0);
if not SqlConfigDataSource(0, ODBC_ADD_DSN, 'SQL Server', PChar(strAttributes)) then begin
errMsg := AllocMem(SQL_MAX_MESSAGE_LENGTH);
SQLInstallerError(1, @pfErrorCode, errMsg, SQL_MAX_MESSAGE_LENGTH, nil);
MessageBox(0, errMsg, PChar('Add System DSN Error #' + IntToStr(pfErrorCode)), 0);
FreeMem(errMsg);
end;
end;