unit U_DeviceJkDll;

interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters,   cxClasses, dxLayoutContainer,
  dxLayoutControl, dxLayoutcxEditAdapters, cxContainer, cxEdit, Vcl.StdCtrls,
  cxTextEdit, Vcl.ExtCtrls, dxLayoutControlAdapters, U_BaseList, Data.DB,
  Data.Win.ADODB, Vcl.Buttons, Vcl.ComCtrls, Vcl.ToolWin, cxImage, cxDBEdit,
  cxStyles, cxCustomData,  cxData, cxDataStorage, cxNavigator,
  dxDateRanges, dxScrollbarAnnotations, cxDBData, dxBarBuiltInMenu, cxPC,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, U_frameBads,
  Datasnap.DBClient, RM_Common, RM_Class, RM_GridReport,
  cxCheckBox, cxMaskEdit, cxDropDownEdit, Vcl.Menus, cxButtonEdit, RM_Dataset,
  cxGeometry, dxFramedControl, dxPanel, U_KeyBoard, cxGroupBox,
  cxGridCustomPopupMenu, cxGridPopupMenu, MovePanel;

  procedure OpenCom(mHandle:Thandle;DllName: string;var fIsCommopen:boolean);
  procedure CloseCom(DllName: string);
implementation



procedure OpenCom(mHandle:Thandle;DllName: string;var fIsCommopen:boolean);
type
  TMyFunc = function(fhandle: hwnd; sCommName: PAnsiChar; IntTime: Integer;
    IsMessage: Integer): hwnd; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
  newh: hwnd;
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(mHandle, '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 CloseCom(DllName: string);
type
  TMyFunc = function(sCommName: PAnsiChar): hwnd; stdcall;
var
  Tf1: TMyFunc;
  Tp1: TFarProc;
  Th1: Thandle;
  newh1: hwnd;
begin
  Th1 := LoadLibrary(pchar(trim(DllName)));
  if Th1 > 0 then
  begin
    try
      Tp1 := GetProcAddress(Th1, 'CommClose');
      if Tp1 <> nil then
      begin
        Tf1 := TMyFunc(Tp1);
        newh1 := Tf1('Comm1');
      end
      else
      begin

      end;
    finally
      // FreeLibrary(Th1);
    end;
  end
  else
  begin
    Application.MessageBox(pchar('找不到 ' + trim(DllName) + ' 文件！'), '提示');
  end;
end;
end.
