怎样写一个unit1,把一些函数定义为公共函数,在另一个UNIT中调用?该怎么处理
怎样写一个unit1,把一些函数定义为公共函数,在另一个UNIT中调用?
如以下二个函数,
Function Get_CPU_Info:string;
var
reg2:TRegistry;
begin
Result:= ' ';
reg2:=TRegistry.Create;
with reg2 do
begin
RootKey:=HKEY_LOCAL_MACHINE;
//获取CPU信息
OpenKey( 'HARDWARE\DESCRIPTION\System\CentralProcessor\0 ',False);
Result:=ReadString( 'ProcessorNameString ');
end;
reg2.free;
end;
Function Get_Disk_Size:string;
var
d:Char;
DiskType:Word; //字型 无符号 16位
Str,S1,S2:String;
i:Integer;
total,free,userFreeBytes,totalBytes,freeBytes:Int64;
begin
total:=0; // 此处必须初始化,否则返回值巨大
free:=0;
for i:= 0 to 25 do
begin
d:=Chr(i+65);
Str:= 'd ';
DiskType:=GetDriveType(pChar(d+ ':\ '));
if(DiskType = DRIVE_FIXED) or (DiskType = DRIVE_REMOTE) then
begin
GetDiskFreeSpaceEx(pChar(d+ ':\ '),userFreeBytes,totalBytes,@freeBytes);
total:= total+totalBytes;
free:= free+freeBytes;
end;
end;
S1:= '硬盘总容量 '+Formatfloat( '###,##0 ',(((total)div 1000)div 1000)div 1000)+ 'G ';
S2:= '硬盘可用空间 '+Formatfloat( '###,##0 ',(((free)div 1000)div 1000)div 1000)+ 'G ';
Result:=S1+ ' '+S2;
end;
写入unit1,然后在unit2中调用 ,,,,,,
麻烦写出源码,我不懂这一块的写法。。。。。。
------解决方案--------------------
...
interface
...
Function Get_CPU_Info:string;
Function Get_Disk_Size:string;
如以下二个函数,
Function Get_CPU_Info:string;
var
reg2:TRegistry;
begin
Result:= ' ';
reg2:=TRegistry.Create;
with reg2 do
begin
RootKey:=HKEY_LOCAL_MACHINE;
//获取CPU信息
OpenKey( 'HARDWARE\DESCRIPTION\System\CentralProcessor\0 ',False);
Result:=ReadString( 'ProcessorNameString ');
end;
reg2.free;
end;
Function Get_Disk_Size:string;
var
d:Char;
DiskType:Word; //字型 无符号 16位
Str,S1,S2:String;
i:Integer;
total,free,userFreeBytes,totalBytes,freeBytes:Int64;
begin
total:=0; // 此处必须初始化,否则返回值巨大
free:=0;
for i:= 0 to 25 do
begin
d:=Chr(i+65);
Str:= 'd ';
DiskType:=GetDriveType(pChar(d+ ':\ '));
if(DiskType = DRIVE_FIXED) or (DiskType = DRIVE_REMOTE) then
begin
GetDiskFreeSpaceEx(pChar(d+ ':\ '),userFreeBytes,totalBytes,@freeBytes);
total:= total+totalBytes;
free:= free+freeBytes;
end;
end;
S1:= '硬盘总容量 '+Formatfloat( '###,##0 ',(((total)div 1000)div 1000)div 1000)+ 'G ';
S2:= '硬盘可用空间 '+Formatfloat( '###,##0 ',(((free)div 1000)div 1000)div 1000)+ 'G ';
Result:=S1+ ' '+S2;
end;
写入unit1,然后在unit2中调用 ,,,,,,
麻烦写出源码,我不懂这一块的写法。。。。。。
------解决方案--------------------
...
interface
...
Function Get_CPU_Info:string;
Function Get_Disk_Size:string;