Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   C++ C++ 2 Delphi, Core2duo/quad Reading (https://www.delphipraxis.net/98434-c-2-delphi-core2duo-quad-reading.html)

Razor 27. Aug 2007 13:40


C++ 2 Delphi, Core2duo/quad Reading
 
Can somebody translate this delphi,i want to use this for core2duo/core2quad/core2solo temperature reading,i have the driver and i will post it it when i am done,so if anybody knows how to translate this to delphi... :oops:


Code:
#include <float.h>
#include <stdio.h>
#include <windows.h>

#include "C2DTemp.h"
#include "RivaTunerExports.h"
#include "MonitoringSourceDesc.h"

HINSTANCE g_hModule = NULL;
HMODULE g_hHost = NULL;

READ_MSR_PROC g_pReadMSR = NULL;

DWORD g_dwCPU = 0;
BOOL g_bHasDTS = FALSE;
FLOAT g_fTjmax = 100.0f;

const char * const szDim = "°C";
const char * const szDesc = "CPU temperature as reported by on-die Digital Thermal Sensor";
const char * const szGroup = "CPU";

BOOL DetectCPUFeatures(void)

const char ven_intel[12] = {'G','e','n','u','i','n','e','I','n','t','e','l'};
char vendor[12];
DWORD last_fn = 0, fn6_eax = 0;

memset(vendor, 0, 12);

// try to execute CPUID instruction
__try { 
__asm { 
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
cpuid
mov dword ptr [last_fn], eax
mov dword ptr [vendor], ebx
mov dword ptr [vendor + 4], edx
mov dword ptr [vendor + 8], ecx


__except (GetExceptionCode() == STATUS_ILLEGAL_INSTRUCTION) { 
return FALSE;


// Is it GenuineIntel CPU? 
if (strncmp(vendor, ven_intel, 12) != 0) { 
return FALSE;


// Does it support Digital Thermal Sensor and Power Management CPUID leaf? 
if (last_fn < 6) { 
return FALSE;


__asm { 
mov eax, 6 
cpuid
mov dword ptr [fn6_eax], eax


// Is Digital Thermal Sensor feature supported? 
if ((fn6_eax & 0x1) == 0) { 
return FALSE;


return TRUE;


BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)

UNREFERENCED_PARAMETER(lpReserved);

if (dwReason == DLL_PROCESS_ATTACH) { 
g_hModule = hInstance;
g_bHasDTS = DetectCPUFeatures();


return TRUE;


C2DTEMP_API DWORD GetSourcesNum(void)

SYSTEM_INFO si;

GetSystemInfo(&si);

g_dwCPU = si.dwNumberOfProcessors;

return g_dwCPU;


C2DTEMP_API BOOL GetSourceDesc(DWORD dwIndex, LPMONITORING_SOURCE_DESC pDesc)

DWORD hi, lo;

if (g_pReadMSR == NULL) { 
g_hHost = GetModuleHandle(NULL);

if (g_hHost == NULL) { 
return FALSE;


g_pReadMSR = (READ_MSR_PROC)GetProcAddress(g_hHost, "ReadMSR");

if (g_pReadMSR == NULL) { 
return FALSE;


if (!g_bHasDTS) { 
return FALSE;
} else { 
// Try to detect Tjunction
if (!g_pReadMSR(0xEE, &hi, &lo)) { 
return FALSE;

if (lo & 0x40000000) { 
g_fTjmax = 85.0f;




sprintf(pDesc->szName, "CPU%ld temperature", dwIndex);
strcpy(pDesc->szDim , szDim);
strcpy(pDesc->szDesc , szDesc);

if (pDesc->dwVersion >= 0x00010002) { 
strcpy(pDesc->szGroup, szGroup);


pDesc->fltMaxLimit = 100.0f;
pDesc->fltMinLimit = 0.0f;
pDesc->fltGridDim = 10.0f;

return TRUE;


C2DTEMP_API FLOAT GetSourceData(DWORD dwIndex)

static FLOAT val[32] = { FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX,
FLT_MAX, FLT_MAX, FLT_MAX, FLT_MAX
};

DWORD_PTR dwMask, dwProcessAffinityMask, dwSystemAffinityMask;
DWORD hi, lo;

// Is DTS supported by this CPU? 
if (!g_bHasDTS) { 
return FLT_MAX;


// NOTE: This should be done by RivaTuner before calling plugins
// Will be removed when Alexey implements it internally

// Get current process and system affinity mask
if (!GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask)) { 
return FLT_MAX;


// Temporarily enable execution on all the CPUs in the system
if (!SetProcessAffinityMask(GetCurrentProcess(), dwSystemAffinityMask)) { 
return FLT_MAX;


dwMask = 1 << dwIndex;

// Move the thread to the proper core
if (!SetThreadAffinityMask(GetCurrentThread(), dwMask)) { 
return FLT_MAX;


// Read IA32_THERM_STATUS MSR
if (!g_pReadMSR(0x19C, &hi, &lo)) { 
return FLT_MAX;


// Is reading valid? 
// If not, just return previous value
if ((lo & 0x80000000) == 0) { 
return val[dwIndex];


val[dwIndex] = g_fTjmax - (FLOAT)((lo >> 16) & 0x7F);

return val[dwIndex];
}
[edit=SirThornberry]quote-tag replaced with c-tag - Mfg, SirThornberry[/edit]

Die Muhkuh 27. Aug 2007 14:00

Re: C++ 2 Delphi, Core2duo/quad Reading
 
You forget a lot of the Headerfiles like:

Code:
#include "C2DTemp.h"
#include "RivaTunerExports.h"
#include "MonitoringSourceDesc.h"

Razor 27. Aug 2007 14:24

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Liste der Anhänge anzeigen (Anzahl: 1)
Everything uploaded as you asked!:wink:

Muetze1 27. Aug 2007 14:40

Re: C++ 2 Delphi, Core2duo/quad Reading
 
What about the old thread with the same question and code? here?

Razor 27. Aug 2007 14:42

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Let moderators delete it,i stick to

New question>new thread :P

Razor 27. Aug 2007 20:13

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Liste der Anhänge anzeigen (Anzahl: 1)
Zitat:

@Razor:

I would say its very complex to translate C code into Delphi, and another problem is the driver you have. Every driver contains other methods with different calling parameters, so nobody knows how to access your specific driver.

The simplest way is to read the Intel® 64 and IA-32 Architectures Software Developer's Manual, where chapter 13 (Power And Thermal Management) contains the needed ways to gain the core temperatures. Before that you have to check the processor requirements, and if they are available (via the CPUID command, some bits in the Feature capabilities). The CPUID command is accessable in combination with Delphi itself, for the MSR access you need a device driver with ring 0 access and a delphi application with admin rights to load it.

If you have the routines to read MSR and CPUID functions, the temperature readout is quite simple. Just read the chapter 13 within the above Intel document. If there are other questions just ask. But be sure that nobody translates C code or wrote the Delphi code for you.

Hope that helps...

This is the driver i use.Will this do or it wont? :|

Another interesting code


Delphi-Quellcode:
function cDrvIface.ReadMSR(ulECX:DWord; var MSRreg:tmsr):boolean;
begin
  result:=false;
  FillChar(MSRreg, SizeOf(MSRreg), 0);
  if not isLoad then exit;
  if Assigned(fOSMethods[WindowsVer].RMSR) then
       result:=fOSMethods[WindowsVer].RMSR(ulECX, MSRreg);
end;
[edit=SirThornberry]delphi-tags replaced with quote-tags and quote-tags replaced with delphi-tags - Mfg, SirThornberry[/edit]

christian_u 27. Aug 2007 20:43

Re: C++ 2 Delphi, Core2duo/quad Reading
 
How many Threads with this Question you will Open in the Future ?
What is your offer for the Translation ?

Razor 27. Aug 2007 20:48

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Dont u think other people will consider use of it also,so say what do we offer

christian_u 28. Aug 2007 06:39

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Your right, as i see a lot of people want to translate the code.

DevidEspenschied 28. Aug 2007 10:33

Re: C++ 2 Delphi, Core2duo/quad Reading
 
The driver does not work, because it provides only functions to read/write to ports or the AMD chipset registers, but no MSR access. The separate function to read the MSR is for itself alone not runable, to see if it works I would need to see the complete code that function uses.

To be honest: I have written that complete temperature readout including the driver for x32/x64, but because there is a lot of work in it I'm NOT willing to share that for free. Other companies pay a lot of money to do that. And I believe nobody will translate C code into Delphi for free either (at least not in that amount)...

Razor 28. Aug 2007 12:05

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Ahh i provided core2duo/core2quad cpu usage and nobody was thankfull,i see people begging for codes to be translated and people just do it?Explain that?

Why you think i am posting in this forum at all?So other users can benefit from this also!




I only need MSR Access code i figured the cpuid and other stuff by myself :x

EDIT: i found this code on newsgroups

Delphi-Quellcode:
type
{ hier können eigene, bessere Namen definiert werden } 
  TMachineStateRegisterRange = (Bit0 = 0, Bit63 = 63);
  TMachineStateRegister = set of TMachineStateRegisterRange;


function getMsr(whichone: Integer): TMachineStateRegister;
var
  res: TMachineStateRegister;
begin
  asm
    mov ecx, whichone
    rdmsr
    lea esi, res
    mov [esi], ax
    mov [esi+4], dx
  end;
  getMsr := res;
end;


var
  msr: TMachineStateRegister;
  i: TMachineStateRegisterRange;


begin
  msr := getMsr(0);
  for i := Low(TMachineStateRegisterRange) to
           High(TMachineStateRegisterRange) do
    if i in msr then
      WriteLn('Bit ', Ord(i), ' ist gesetzt.');
end.

Found this also

Delphi-Quellcode:
    function ReadMSR(msr_num: DWord; var msr: tmsr): boolean;
    function WriteMSR(msr_num: DWord; const msr: tmsr): boolean;

Muetze1 28. Aug 2007 12:51

Re: C++ 2 Delphi, Core2duo/quad Reading
 
As I stated many times before: you can not read out any MSR register with the RDMSR instruction in your ring 3 application. As said before: you will get a EPrivilegeInstruction exception. Try this code on your system - and not only just paste it here and hoping to find someone who glues your pieces to an working application...

Razor 28. Aug 2007 12:58

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Alright wrong thinking yes...


As i have seen in other code C code the athor of it said to me that hi and lo are dwords

So...

Delphi-Quellcode:
Var
hi,lo:DWORD;
But the address is 0x19C,so we have to read MSR for 100 TjMAX and 85 Tjmax





i have this function in another driver i found,can this read this thing as we want to?

Delphi-Quellcode:
function ReadMSR(msr_num: DWord; var msr: tmsr): boolean;

ReadMSR(lo,0x19C):=true; //i doubt this is the way and thats why i ask if it is

Razor 9. Nov 2007 15:05

Re: C++ 2 Delphi, Core2duo/quad Reading
 
If someone has core 2 duo contact me i need betatesters... :)

Razor 12. Nov 2007 19:07

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Hi!


As i said i read msr register this way
Delphi-Quellcode:
var
  Form1: TForm1;
    isLoadDr: boolean;
   C0:string;
   HiPart, LowPart:tmsr;
implementation

{$R *.dfm}

//Remember 2 registers $19C and $EE.
//19C is for DTS checking.
//EE is for Tjunction EE.
//Low > EAX
//Hi > EDX

procedure tform1.RDMSR;
var
 GetInstance:tomcdrv;
begin
GetInstance.Create();
   getInstance.IMSRIORef.ReadMSR($19C,lowPart);
   
  //
    //getInstance.IMSRIORef.ReadMSR($EE,HiPart);


end;

procedure tform1.intelC2D;
begin

  if   msr.lowPart=0 then
   RichEdit1.Lines.add('No Digital Thermal Sensor Detected!');


end;

Now here comes the conclusion the read msr is the same for EAX.When i tested this on my computer Celeron D cpu, i got read EAX 0
but when i tested on C2D i got the EAX 0.Here i stoped beceouse i dont know how to go on....
$EE is for determening Tjunction value and $19C is for detecting DTS-Digital thermal sensor value 0-no other value yes.
Forumula is Core temp=Tdata-Tjunction


So help is needed in order to finish this project.. :(



Offtopic:Does anybody know where can i get nforce specs/datasheets?

Razor 5. Mai 2008 08:33

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Forget everything i said till now.

-I have to start at the begining and thats making a x32/x64 driver.What would be the most suitable enviroment to make it since delphi i don't think can make them.

-Driver signing for x64 vista.

-I have new computer with q6600 so testing is no problem.

Razor 7. Mai 2008 13:01

Re: C++ 2 Delphi, Core2duo/quad Reading
 
Can some post a working example without the driver i have been working on this for 2 weeks no results..
I only found these by my own research..

//Remember 2 registers $19C and $EE.
//19C is for DTS checking.
//EE is for Tjunction EE.
//Low > EAX
//Hi > EDX

:wall:


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:37 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz