; Write(pchar(mssrc.memory)[i]);
inc(i);
end;
end;
finally
mssrc.Free;
end;
end.
;
// Non-DISP interfaces: IID_xxxx
// *********************************************************************//
const
// TypeLibrary Major and minor versions
SdkSrvMajorVersion = 1;
SdkSrvMinorVersion = 0;
LIBID_SdkSrv: TGUID = '{3B01ECB9-6782-4B27-8BB4-84B2B4E4B962}';
IID_IMySendKey: TGUID = '{24049466-2060-4CAF-BBE7-559268B54127}';
DIID_IMySendKeyEvents: TGUID = '{A10A15B5-8B3E-4366-9252-E5418699ACF7}';
CLASS_MySendKey: TGUID = '{95E49D0E-D659-4366-9279-BB700D9161F0}';
type
// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
IMySendKey = interface;
IMySendKeyDisp = dispinterface;
IMySendKeyEvents = dispinterface;
// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
MySendKey = IMySendKey;
// *********************************************************************//
// Interface: IMySendKey
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {24049466-2060-4CAF-BBE7-559268B54127}
// *********************************************************************//
IMySendKey = interface(IDispatch)
['{24049466-2060-4CAF-BBE7-559268B54127}']
procedure SendStr(vwait: SYSINT); safecall;
function Get_WinName: WideString; safecall;
procedure Set_WinName(const Value: WideString); safecall;
function Get_KeyStr: WideString; safecall;
procedure Set_KeyStr(const Value: WideString); safecall;
procedure SetWinAndKey(const WinName: WideString; const KeyStr: WideString); safecall;
procedure SendStr2(const KeyStr: WideString; vwait: Integer); safecall;
property WinName: WideString read Get_WinName write Set_WinName;
property KeyStr: WideString read Get_KeyStr write Set_KeyStr;
end;
// *********************************************************************//
// DispIntf: IMySendKeyDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {24049466-2060-4CAF-BBE7-559268B54127}
// *********************************************************************//
IMySendKeyDisp = dispinterface
['{24049466-2060-4CAF-BBE7-559268B54127}
']
************************************//
CoMySendKey = class
class function Create: IMySendKey;
class function CreateRemote(const MachineName: string): IMySendKey;
end;
implementation
uses ComObj;
class function CoMySendKey.Create: IMySendKey;
begin
Result := CreateComObject(CLASS_MySendKey) as IMySendKey;
end;
>class function CoMySendKey.CreateRemote(const MachineName: string): IMySendKey;
begin
Result := CreateRemoteComObject(MachineName, CLASS_MySendKey) as IMySendKey;
end;
end.
//==========實現類型庫===========//
unit uSrvMain;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
ComObj, ActiveX, AxCtrls, Classes, SdkSrv_TLB, StdVcl,uComFactory;
type
TMySendKey = class(TAutoObject, IConnectionPointContainer, IMySendKey)
private
{ Private declarations }
FConnectionPoints: TConnectionPoints;
FConnectionPoint: TConnectionPoint;
FEvents: IMySendKeyEvents;
{ note: FEvents maintains a *single* event sink. For Access to more
than one event sink, use FConnectionPoint.SinkList, and iterate
through the list of sinks. }
FWinName:string;
FKeyStr:string;
//FInfoCount:integer;
public
procedure Initialize; override;
protected
{ Protected declarations }
property ConnectionPoints: TConnectionPoints read FConnectionPoints
implements IConnectionPointContainer;
procedure EventSinkChanged(const EventSink: IUnknown); override;
procedure SendStr(vwait: SYSINT); safecall;
function Get_WinName: WideString; safecall;
procedure Set_WinName(const Value: WideString); safecall;
function Get_KeyStr: WideString; safecall;
procedure Set_KeyStr(const Value: WideString); safecall;
procedure WriteInfo;
procedure SetWinAndKey(const WinName, KeyStr: WideString); safecall;
procedure SendStr2(const KeyStr: WideString; vWait: Integer); safecall;
end;
implementation
uses ComServ, sndkey32, skSrv, DateUtils;
procedure TMySendKey.EventSinkChanged(const EventSink: IUnknown);
begin
FEvents := EventSink as IMySendKeyEvents;
end;
procedure TMySendKey.Initialize;
begin
inherited Initialize;
FConnectionPoints := TConnectionPoints.Create(Self);
if AutoFactory.EventTypeInfo <> nil then
FConnectionPoint := FConnectionPoints.CreateConnectionPoint(
AutoFactory.EventIID, ckSingle, EventConnect)
else FConnectionPoint := nil;
end;
procedure TMySendKey.SendStr(vwait: SYSINT);
begin
if (FWinName<>'') and (FKeyStr<>'') then begin
if AppActivate(PAnsiChar(FWinName)) then begin
SendKeys(PAnsiChar(fkeystr),vwait=0);
if BlockInfo=0 then
writeinfo;
end;
end;
end;
function TMySendKey.Get_WinName: WideString;
begin
Result:=FWinName;
end;
procedure TMySendKey.Set_WinName(const Value: WideString);
begin
if Value<>'' then begin
FWinName:=Value;
end;
end;
function TMySendKey.Get_KeyStr: WideString;
begin
result:=FKeyStr;
end;
procedure TMySendKey.Set_KeyStr(const Value: WideString);
begin
if Value<>'' then begin
FKeyStr:=Value;
end;
end;
procedure TMySendKey.WriteInfo;
begin
With frmskSrv.memInfo.Lines do begin
csection.Acquire;
try
if InfoCount>1000 then begin
clear;
InfoCount:=0;
end;
Add(concat(FWinName,':',FKeyStr));
inc(InfoCount);
finally
csection.Release;
end;
end;
end;
procedure TMySendKey.SetWinAndKey(const WinName, KeyStr: WideString);
begin
FWinName:=WinName;
FKeyStr:=KeyStr;
if BlockInfo=0 then
WriteInfo;
end;
procedure TMySendKey.SendStr2(const KeyStr: WideString; vWait: Integer);
begin
if (FWinName<>'') then begin
if AppActivate(PAnsiChar(FWinName)) then begin
FKeyStr:=KeyStr;
SendKeys(PAnsiChar(FKeyStr),vwait=0);
if BlockInfo=0 then
writeinfo;
end;
end;
end;
initialization
TMyComApartmentFactory.Create(ComServer, TMySendKey, Class_MySendKey,
ciMultiInstance, tmApartment);
end.
//=======改寫的Apartment線程工廠類==============// { *********************************************************************** }
{ }
{ Delphi Runtime Library }
{ }
{ Copyright (c) 1997-2001 Borland Software Corporation }
{ }
{ *********************************************************************** }
unit uComFactory;
{$H+,X+}
interface
uses ActiveX, ComObj, Classes;
type
{ Component object factory }
TMyComApartmentFactory = class(TAutoObjectFactory, IClassFactory)
protected
function CreateInstance(const UnkOuter: IUnknown; const IID: TGUID;
out Obj): HResult; stdcall;
public
constructor Create(ComServer: TComServerObject;
ComClass: TAutoClass; const ClassID: TGUID;
Instancing: TClassInstancing; ThreadingModel: TThreadingModel = tmSingle);
end;
implementation
uses
Windows, SysUtils;
type
{ TApartmentThread }
TMyApartmentThread = class(TThread)
private
FFactory: IClassFactory2;
FUnkOuter: IUnknown;
FIID: TGuid;
FSemaphore: THandle;
FStream: Pointer;
FCreateResult: HResult;
protected
procedure Execute; override;
public
constructor Create(Factory: IClassFactory2; UnkOuter: IUnknown; IID: TGuid);
destructor Destroy; override;
property Semaphore: THandle read FSemaphore;
property CreateResult: HResult read FCreateResult;
property ObJStream: Pointer read FStream;
end;
{ TMyApartmentThread }
constructor TMyApartmentThread.Create(Factory: IClassFactory2;
UnkOuter: IUnknown; IID: TGuid);
begin
FFactory := Factory;
FUnkOuter := UnkOuter;
FIID := IID;
FSemaphore := CreateSemaphore(nil, 0, 1,nil);
FreeOnTerminate := True;
inherited Create(False);
end;
destructor TMyApartmentThread.Destroy;
begin
CloseHandle(FSemaphore);
inherited Destroy;
end;
procedure TMyApartmentThread.Execute;
var
msg: TMsg;
Unk: IUnknown;
begin
try
CoInitializeEx(nil,COINIT_APARTMENTTHREADED);
try
FCreateResult := FFactory.CreateInstanceLic(FUnkOuter, nil, FIID, '', Unk);
FUnkOuter := nil;
FFactory := nil;
if FCreateResult = S_OK then
CoMarshalInterThreadInterfaceInStream(FIID, Unk, IStream(FStream));
ReleaseSemaphore(FSemaphore, 1, nil);
if FCreateResult = S_OK then
while GetMessage(msg, 0, 0, 0) do
begin
DispatchMessage(msg);
Unk._AddRef;
if Unk._Release = 1 then break;
end;
finally
Unk := nil;
CoUninitialize;
end;
except
{ No exceptions should go unhandled }
end;
end;
{ TMyComApartmentFactory }
constructor TMyComApartmentFactory.Create(ComServer: TComServerObject;
ComClass:TAutoClass; const ClassID: TGUID;
Instancing: TClassInstancing; ThreadingModel: TThreadingModel);
begin
inherited Create(ComServer, ComClass,
ClassID, Instancing, ThreadingModel);
end;
function TMyComApartmentFactory.CreateInstance(const UnkOuter: IUnknown;
const IID: TGUID; out Obj): HResult; stdcall;
begin
if not IsLibrary and (ThreadingModel = tmApartment) then
begin
LockServer(True);
try
with TMyApartmentThread.Create(Self, UnkOuter, IID) do
begin
if WaitForSingleObject(Semaphore, INFINITE) = WAIT_OBJECT_0 then
begin
Result := CreateResult;
if Result <> S_OK then Exit;
Result := CoGetInterfaceAndReleaseStream(IStream(ObJStream), IID, Obj);
end else
Result := E_FAIL
end;
finally
LockServer(False);
end;
end else
Result := inherited CreateInstance(UnkOuter, IID, Obj);
end;
initialization
finalization
end.
//客戶端 關鍵代碼是uRmtobj.pas這個文件
//客戶端主窗體代碼
unit uSndClIEnt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, sdkSrv_tlb, comobj,activex, ExtCtrls,urmtobj,
ComCtrls, ToolWin,UApartThread,Buttons, LMDCustomComponent, LMDIniCtrl;
type
TfrmSendKey = class(TForm)
edWinName: TEdit;
edKeystr: TEdit;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
edComputer: TEdit;
edUser: TEdit;
edPsw: TEdit;
lmdIni: TLMDIniCtrl;
btnWriteIni: TButton;
btnLoadKey: TButton;
cbOnTop: TCheckBox;
ToolBar1: TToolBar;
tb1: TToolButton;
tb2: TToolButton;
tb3: TToolButton;
tb4: TToolButton;
tb5: TToolButton;
tb6: TToolButton;
ToolButton10: TToolButton;
tb7: TToolButton;
tb8: TToolButton;
btStop: TButton;
ToolButton1: TToolButton;
sbMini: TSpeedButton;
procedure Button2Click(Sender: TObject);
procedure btnWriteIniClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnLoadKeyClick(Sender: TObject);
procedure cbOnTopClick(Sender: TObject);
procedure tb1Click(Sender: TObject);
procedure btStopClick(Sender: TObject);
procedure sbMiniClick(Sender: TObject);
protected
private
FWinSize:integer;
FWoWKeyString:string;
FSendWinName:string;
FRegion:THandle;
FMainInt:MySendKey;
procedure SetWoWKeyString(const Value: string);
function ReadWoWKeyString: string;
procedure SetSendWinName(const Value: string);
function ReadSendWinName: string;
procedure WMNCHitTest(var M: TWMNCHitTest); message wm_NCHitTest;
procedure FreeCurrentRegion;
{ Private declarations }
public
FWoWKeyList:TStringList;
sComputer,sUser,sPsw:widestring;
property WoWKeyString:string read ReadWoWKeyString write SetWoWKeyString;
property SendWinName:string read ReadSendWinName write SetSendWinName;
{ Public declarations }
end;
var
frmSendKey: TfrmSendKey;
rmtObject:IMySendKey;
KeyCount:integer;
thr:TTmpThread;
>
implementation
uses Math, StrUtils;
{$R *.dfm}
{ TTmpThread }
SendWinName);
Caption:='S&top';