在Delphi中在运行时设置UDL连接

问题描述 投票:0回答:1

在我们的Delphi应用程序中,我们使用UDL文件存储连接属性。如果缺少UDL文件,那么我想创建它,并让用户选择服务器/数据库并设置用户名/密码。然后可以将其保存在UDL文件中,并在应用程序启动时一直使用。我知道这里存在安全问题,但这就是它在我的项目中的工作方式(我现在不打算对其进行更改)。

该UDL文件应该位于'C:\ Program Files(x86)\ Common Files \ system \ ole db \ Data Links'文件夹中,通常位于该文件夹中。但是,如果缺少它,我想创建它,并让用户在其中设置服务器/数据库等(每个客户都不同)。我已经知道如何创建UDL文件(使用CreateUDLFile函数)。但是如何在屏幕上将其弹出以供用户完成设置。它应该是一种只能设置一次的应用程序设置。现在可以手动完成(通过双击UDL文件),但我想使其自动化。

这是我想弹出的内容:

enter image description here

delphi delphi-xe
1个回答
1
投票

这是我的问题的答案:

procedure TdmMain.DataModuleCreate(Sender: TObject);
var
  lDataLinkDir: String;
begin
  lDataLinkDir := DataLinkDir;
  if not FileExists(Format('%s\MBMIS.udl',[lDataLinkDir])) then
  begin
    if Copy(lDataLinkDir, Length(lDataLinkDir) - 1, 1) <> '\' then
      lDataLinkDir := lDataLinkDir + '\';
  // Creates empty UDL datalink.
  CreateUDLFile(lDataLinkDir + 'MBMIS.udl', 'SQLOLEDB.1', '');
  // Opens up the created UDL datalink for user to complete setup.
  ShellExecuteAndWait(lDataLinkDir + 'MBMIS.udl', '');
end;

{ Runs application using ShellExecuteEx and waits for completion. }
function TdmMain.ShellExecuteAndWait(FileName: String; Params: String): Boolean;
var
  exInfo: TShellExecuteInfo;
  Ph: DWORD;
begin
  FillChar(exInfo, SizeOf(exInfo), 0);
  with exInfo do
  begin
    cbSize := SizeOf(exInfo);
    fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_DDEWAIT;
    Wnd := GetActiveWindow();
    exInfo.lpVerb := 'open';
    exInfo.lpParameters := PChar(Params);
    lpFile := PChar(FileName);
    nShow := SW_SHOWNORMAL;
  end;
  if ShellExecuteEx(@exInfo) then
    Ph := exInfo.hProcess
  else
  begin
    ShowMessage(SysErrorMessage(GetLastError));
    Result := true;
    exit;
  end;
  while WaitForSingleObject(exInfo.hProcess, 50) <> WAIT_OBJECT_0 do
    Application.ProcessMessages;
  CloseHandle(Ph);
  Result := true;
end;
© www.soinside.com 2019 - 2024. All rights reserved.