AI am trying to make a new entry in User DSN, in ODBC Data Source Administrator with the following code:
procedure TForm1.FormCreate(Sender: TObject);
var strAttributes: string;
wideChars : array[0..1000] of WideChar;
pfErrorCode: DWORD;
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);
StringToWideChar(strAttributes, wideChars, 12);
if not SqlConfigDataSource(0, ODBC_ADD_DSN, 'SQL Server', wideChars) 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;
but the SqlConfigDataSource part does not do the job, and also the error that is returned is not undarstandable at all. It is not a number, nor description for the error. Can anyone help me where i make the mistake? Thanks
Probably your error or even set of errors is in incorrect translation of ODBC headers, which then may be used for non-Unicode or Unicode Delphi version. For example:
XxxW(UTF16) functions fromODBCCP32.DLL, thanXxx(ANSI) functions;Xxxfunctions. And thenwideCharsshould be defined asarray[..] of Char;SqlConfigDataSourcemay be defined asXxxWwith PAnsiChar;I wanted to show you the idea, because without full sources I can only speculate. Then you have suspicious call
StringToWideChar(strAttributes, wideChars, 12);. strAttributes value is much more long than 12 characters.The following code works well in Delphi XE2: