unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs; type TForm2 = class(TForm) procedure FormShow(Sender: TObject); private fIsCommopen: Boolean; procedure OpenCom(DllName: string); { Private declarations } public { Public declarations } end; var Form2: TForm2; newh: hwnd; implementation uses U_iniParam; {$R *.dfm} procedure TForm2.OpenCom(DllName: string); type TMyFunc = function(fhandle: hwnd; sCommName: PAnsiChar; IntTime: Integer; IsMessage: Integer): hwnd; stdcall; var Tf: TMyFunc; Tp: TFarProc; Th: Thandle; begin Th := LoadLibrary(pchar(trim(DllName))); if Th > 0 then begin try Tp := GetProcAddress(Th, 'CommOpen'); if Tp <> nil then begin Tf := TMyFunc(Tp); newh := Tf(Self.Handle, 'Comm1', 500, 1); if newh < 1 then begin Application.MessageBox(pchar('打开串口失败!'), '提示'); end else fIsCommopen := true; end else begin fIsCommopen := false; end; finally // FreeLibrary(Th); end; end else begin fIsCommopen := false; Application.MessageBox(pchar('找不到 ' + trim(DllName) + ' 文件!'), '提示'); end; end; procedure TForm2.FormShow(Sender: TObject); begin if trim(MBDYDllName) <> '' then OpenCom(MBDYDllName); end; end.