N30TheM4TRIX
شنبه 03 تیر 1391, 21:12 عصر
سلام و خسته نباشیدبه همه
من یه برنامه نوشتم (در vb) که باید پردازش هایی که بیش از مثلاً 5 درصد از cpu رو استفاده می کنن ببنده
این کد رو در دلفی نوشتم و به dll تبدیل کردم اما نوع داده ها در این دو زبان یکی نیست چکار کنم
راهنمایی کنید.
کد dll
library Project1;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
madKernel,
Windows;
{$R *.res}
{
(c) Janis Elsts, http://w-shadow.com/
Last Updated : 27.08.2006
uCpuUsage.pas provides some functions that let you
get the CPU usage (in percent) of a given process. Note that
the usage is calculated for a *period of time* elapsed since
last wsCreateUsageCounter or wsGetCpuUsage call for that process.
This unit is freeware, feel free to use/modify in any way you like.
Using this unit :
cnt : PCPUUsageData;
....
//Initialize the counter
cnt:=wsCreateUsageCounter(Process_id);
//Allow for some time to elapse
Sleep(500);
//Get the CPU usage
usage:=wsGetCpuUsage(cnt);
//The returned value is a real number between 0 and 100 (representint %).
//Destroy the counter and free memory
wsDestroyUsageCounter(cnt);
}
const
wsMinMeasurementInterval=250; {minimum amount of time that must
have elapsed to calculate CPU usage, miliseconds. If time elapsed
is less than this, previous result is returned, or zero, if there
is no previous result.}
type
TCPUUsageData=record
PID,Handle:cardinal;
oldUser,oldKernel:Int64;
LastUpdateTime:cardinal;
LastUsage:single; //Last result of wsGetCpuUsage is saved here
Tag:cardinal; //Use it for anythin you like, not modified by this unit
end;
PCPUUsageData=^TCPUUsageData;
function wsCreateUsageCounter(PID:cardinal):PCPUUsageData;
var
p:PCPUUsageData;
mCreationTime,mExitTime,mKernelTime,
mUserTime:_FILETIME;
h:cardinal;
begin
result:=nil;
//We need a handle with PROCESS_QUERY_INFORMATION privileges
h:=OpenProcess(PROCESS_QUERY_INFORMATION,false,PID );
if h=0 then exit;
new(p);
p.PID:=PID;
p.Handle:=h;
p.LastUpdateTime:=GetTickCount;
p.LastUsage:=0;
if GetProcessTimes(p.Handle,mCreationTime,mExitTime,m KernelTime,
mUserTime) then begin
//convert _FILETIME to Int64
p.oldKernel:=int64(mKernelTime.dwLowDateTime or
(mKernelTime.dwHighDateTime shr 32));
p.oldUser:=int64(mUserTime.dwLowDateTime or
(mUserTime.dwHighDateTime shr 32));
Result:=p;
end else begin
dispose(p);
end;
end;
procedure wsDestroyUsageCounter(aCounter:PCPUUsageData);
begin
CloseHandle(aCounter.Handle);
dispose(aCounter);
end;
function wsGetCpuUsage(aCounter:PCPUUsageData):single;
var
mCreationTime,mExitTime,mKernelTime,
mUserTime:_FILETIME;
DeltaMs,ThisTime:cardinal;
mKernel,mUser,mDelta:int64;
begin
result:=aCounter.LastUsage;
ThisTime:=GetTickCount;
//Get the time elapsed since last query
DeltaMs:=ThisTime-aCounter.LastUpdateTime;
if DeltaMs<wsMinMeasurementInterval then exit;
aCounter.LastUpdateTime:=ThisTime;
GetProcessTimes(aCounter.Handle,mCreationTime,mExi tTime,mKernelTime,
mUserTime);
//convert _FILETIME to Int64
mKernel:=int64(mKernelTime.dwLowDateTime or
(mKernelTime.dwHighDateTime shr 32));
mUser:=int64(mUserTime.dwLowDateTime or
(mUserTime.dwHighDateTime shr 32));
//get the delta
mDelta:=mUser+mKernel-aCounter.oldUser-aCounter.oldKernel;
aCounter.oldUser:=mUser;
aCounter.oldKernel:=mKernel;
Assert(DeltaMs>0);
Result:=(mDelta/DeltaMs)/100; //mDelta is in units of 100 nanoseconds, so...
aCounter.LastUsage:=Result; //just in case you want to use it later, too
end;
exports
wsCreateUsageCounter ,
wsGetCpuUsage ,
wsDestroyUsageCounter;
begin
end.
من یه برنامه نوشتم (در vb) که باید پردازش هایی که بیش از مثلاً 5 درصد از cpu رو استفاده می کنن ببنده
این کد رو در دلفی نوشتم و به dll تبدیل کردم اما نوع داده ها در این دو زبان یکی نیست چکار کنم
راهنمایی کنید.
کد dll
library Project1;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
madKernel,
Windows;
{$R *.res}
{
(c) Janis Elsts, http://w-shadow.com/
Last Updated : 27.08.2006
uCpuUsage.pas provides some functions that let you
get the CPU usage (in percent) of a given process. Note that
the usage is calculated for a *period of time* elapsed since
last wsCreateUsageCounter or wsGetCpuUsage call for that process.
This unit is freeware, feel free to use/modify in any way you like.
Using this unit :
cnt : PCPUUsageData;
....
//Initialize the counter
cnt:=wsCreateUsageCounter(Process_id);
//Allow for some time to elapse
Sleep(500);
//Get the CPU usage
usage:=wsGetCpuUsage(cnt);
//The returned value is a real number between 0 and 100 (representint %).
//Destroy the counter and free memory
wsDestroyUsageCounter(cnt);
}
const
wsMinMeasurementInterval=250; {minimum amount of time that must
have elapsed to calculate CPU usage, miliseconds. If time elapsed
is less than this, previous result is returned, or zero, if there
is no previous result.}
type
TCPUUsageData=record
PID,Handle:cardinal;
oldUser,oldKernel:Int64;
LastUpdateTime:cardinal;
LastUsage:single; //Last result of wsGetCpuUsage is saved here
Tag:cardinal; //Use it for anythin you like, not modified by this unit
end;
PCPUUsageData=^TCPUUsageData;
function wsCreateUsageCounter(PID:cardinal):PCPUUsageData;
var
p:PCPUUsageData;
mCreationTime,mExitTime,mKernelTime,
mUserTime:_FILETIME;
h:cardinal;
begin
result:=nil;
//We need a handle with PROCESS_QUERY_INFORMATION privileges
h:=OpenProcess(PROCESS_QUERY_INFORMATION,false,PID );
if h=0 then exit;
new(p);
p.PID:=PID;
p.Handle:=h;
p.LastUpdateTime:=GetTickCount;
p.LastUsage:=0;
if GetProcessTimes(p.Handle,mCreationTime,mExitTime,m KernelTime,
mUserTime) then begin
//convert _FILETIME to Int64
p.oldKernel:=int64(mKernelTime.dwLowDateTime or
(mKernelTime.dwHighDateTime shr 32));
p.oldUser:=int64(mUserTime.dwLowDateTime or
(mUserTime.dwHighDateTime shr 32));
Result:=p;
end else begin
dispose(p);
end;
end;
procedure wsDestroyUsageCounter(aCounter:PCPUUsageData);
begin
CloseHandle(aCounter.Handle);
dispose(aCounter);
end;
function wsGetCpuUsage(aCounter:PCPUUsageData):single;
var
mCreationTime,mExitTime,mKernelTime,
mUserTime:_FILETIME;
DeltaMs,ThisTime:cardinal;
mKernel,mUser,mDelta:int64;
begin
result:=aCounter.LastUsage;
ThisTime:=GetTickCount;
//Get the time elapsed since last query
DeltaMs:=ThisTime-aCounter.LastUpdateTime;
if DeltaMs<wsMinMeasurementInterval then exit;
aCounter.LastUpdateTime:=ThisTime;
GetProcessTimes(aCounter.Handle,mCreationTime,mExi tTime,mKernelTime,
mUserTime);
//convert _FILETIME to Int64
mKernel:=int64(mKernelTime.dwLowDateTime or
(mKernelTime.dwHighDateTime shr 32));
mUser:=int64(mUserTime.dwLowDateTime or
(mUserTime.dwHighDateTime shr 32));
//get the delta
mDelta:=mUser+mKernel-aCounter.oldUser-aCounter.oldKernel;
aCounter.oldUser:=mUser;
aCounter.oldKernel:=mKernel;
Assert(DeltaMs>0);
Result:=(mDelta/DeltaMs)/100; //mDelta is in units of 100 nanoseconds, so...
aCounter.LastUsage:=Result; //just in case you want to use it later, too
end;
exports
wsCreateUsageCounter ,
wsGetCpuUsage ,
wsDestroyUsageCounter;
begin
end.