New --->ActiveX--->ActiveX Library
New -->ActiveX--->Com Object
--->Class Name:mytest
View --->Type Library---->Imaytest--->New property(GetSystemPath)
删除其中的写属性 set_GetSystemPath
代码如下
unit Unit1;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj, Ptest_TLB, StdVcl;
type
Tmytest = class(TTypedComObject, Imytest)
protected
function Get_GetSystemPath(out Value: OleVariant): HResult;
stdcall;
{Declare Imytest methods here}
end;
implementation
uses ComServ;
function Tmytest.Get_GetSystemPath(out Value: OleVariant): HResult;
var
Temp : array[0..255] of Char;
s:string;
begin
GetSystemDirectory(Temp,255);
s:=Temp;
value:=s;
end;
initialization
TTypedComObjectFactory.Create(ComServer, Tmytest, Class_mytest,
ciMultiInstance, tmApartment);
end.
工程另存为Ptest
编译后注册 此程序 regsvr32 c:/ptest.dll
新建vb程序
引用Ptest Library
加一个按钮和标签写代码如下
Private Sub Command1_Click()
Dim obj As Ptest.mytest
Set obj = New Ptest.mytest
Label1.Caption = obj.GetSystemPath
End Sub
这时Label1.Caption=C:/WINDOWS/SYSTEM32