我有一个windows的标准Service,它是一个exe。
我想在这个Service里面,提供一个接口,提供给任何想访问的程序来访问,这个接口就类似于一个函数,如:function(参数:WideString):wideString;要求他返回一个字符串给调用者,这个如何实现?
我想在这个Service里面,提供一个接口,提供给任何想访问的程序来访问,这个接口就类似于一个函数,如:function(参数:WideString):wideString;要求他返回一个字符串给调用者,这个如何实现?
com源码:unit testcom_TLB;{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interfaceuses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;
// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:
// Type Libraries : LIBID_xxxx
// CoClasses : CLASS_xxxx
// DISPInterfaces : DIID_xxxx
// Non-DISP interfaces: IID_xxxx
// *********************************************************************//
const
// TypeLibrary Major and minor versions
testcomMajorVersion = 1;
testcomMinorVersion = 0; LIBID_testcom: TGUID = '{1944D318-38DE-41CF-98EE-5CC036E97B56}'; IID_Itest: TGUID = '{7A7984B7-B865-4882-9EDE-C95C4EE88F0F}';
CLASS_test: TGUID = '{7A24B559-BF04-44DA-9660-6DE14E532D06}';
type// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
Itest = interface;
ItestDisp = dispinterface;// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
test = Itest;
// *********************************************************************//
// Interface: Itest
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {7A7984B7-B865-4882-9EDE-C95C4EE88F0F}
// *********************************************************************//
Itest = interface(IDispatch)
['{7A7984B7-B865-4882-9EDE-C95C4EE88F0F}']
function getstring(const para: WideString): WideString; safecall;
end;// *********************************************************************//
// DispIntf: ItestDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {7A7984B7-B865-4882-9EDE-C95C4EE88F0F}
// *********************************************************************//
ItestDisp = dispinterface
['{7A7984B7-B865-4882-9EDE-C95C4EE88F0F}']
function getstring(const para: WideString): WideString; dispid 201;
end;// *********************************************************************//
// The Class Cotest provides a Create and CreateRemote method to
// create instances of the default interface Itest exposed by
// the CoClass test. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
Cotest = class
class function Create: Itest;
class function CreateRemote(const MachineName: string): Itest;
end;implementationuses ComObj;class function Cotest.Create: Itest;
begin
Result := CreateComObject(CLASS_test) as Itest;
end;class function Cotest.CreateRemote(const MachineName: string): Itest;
begin
Result := CreateRemoteComObject(MachineName, CLASS_test) as Itest;
end;end.
unit test;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
ComObj, ActiveX, testcom_TLB, StdVcl;type
Ttest = class(TAutoObject, Itest)
protected
function getstring(const para: WideString): WideString; safecall; end;implementationuses ComServ;function Ttest.getstring(const para: WideString): WideString;
begin
Result:='hello '+para;
end;initialization
TAutoObjectFactory.Create(ComServer, Ttest, Class_test,
ciMultiInstance, tmApartment);
end.
编译->注册 Regsvr32 D:\testcom\testcom.dll
调用:var
test: OleVariant;
begin
test:=CreateOleObject('testcom.test');
try
ShowMessage(test.getstring('hongqi162'));
finally
VarClear(test);
end;
end;