Get Computer Name in mql4

 

Hi,

I want to get computer name in mql4 in order to make original log file including computer name which MetaTrader runs.

I think it is possible using GetComputerName function in kernel32.dll, but I don't know how to implement it in mql4. I found a kernel32 api example and modified it, but it doesn't work. Can anybody teach me how to use GetComputerName function in mql4?

I tried following code, but terminal says "GetComputerName GBPUSD,H1: cannot call function 'GetComputerName' from dll 'kernel32.dll' (error 127)".

#import "kernel32.dll" 
int GetComputerName(string lpBuffer, int nSize);
#import

string buf="";
int result;
result = GetComputerName(buf, StringLen(buf));
Alert(result);
Alert(buf);

Thank you.

 
#import "kernel32.dll" 
int GetComputerNameA(string lpBuffer, string nSize);
#import

string buf="";
int result;
result = GetComputerNameA(buf, StringLen(buf));
Alert(result);
Alert(buf);
 

qjol,

Thank you so much. It worked.

 

Is there some way to use kernel32.dll to enable my EA to algorithmically determine the current CPU load?

There are some things I'd rather my EA not do if the CPU utilization is currently above a threshold value.

 
 

So would I do something like this:

#import "kernel32.dll"
//Retrieves system timing information. On a multiprocessor system, the values returned are the sum of the designated times across all processors.
//http://msdn.microsoft.com/en-us/library/ms724400(v=vs.85).aspx

bool GetSystemTimes(int lpIdleTime, int lpKernelTime, int lpUserTime);

#import

int InitialIdleTime=0,InitialKernelTime=0,InitialUserTime=0; // initialize the variables

int GetSystemTimes(InitialIdleTime, InitialKernelTime, InitialUserTime);  // get the initial system times

sleep(5000);  // sleep 5 seconds

int FinalIdleTime=0,FinalKernelTime=0,FinalUserTime=0;  // initialize the variables

int GetSystemTimes(FinalIdleTime, FinalKernelTime, FinalUserTime);  // get the final system times after 5 second wait

double CPUUsage=1-(FinalIdleTime-InitialIdleTime)/(FinalIdleTime-InitialIdleTime+FinalKernelTime-InitialKernelTime+FinalUserTime-InitialUserTime); // CPU Utilization

Print("CPU utilization computed to be ",CPUUsage*100,"%");
 

i'm not sure about the calculation

#import "kernel32.dll"
bool GetSystemTimes(int & lpIdleTime[], int & lpKernelTime[], int & lpUserTime[]);
#import

   int a[1], b[1], c[1], d[1], e[1], f[1];

   GetSystemTimes(a, b, c);  // get the initial system times

   Sleep(1000);  // sleep 1 seconds
   GetSystemTimes(d, e, f);  // get the final system times after 1 second wait
   int i =0;
   int idle = a[i]-d[i];
   int ker  = b[i]-e[i];
   int user = c[i]-f[i];
   int sys  = ker + user;
   double cpuusa =( (sys - idle) *100 / sys );
   Alert ("cpu usage is now: " + cpuusa + " %");

c here & here is a demo project & try to dig from source code the calculation if i'm right

correct me if i'm wrong

 

Yeah you got your initial minus final ordering backwards, and while you declared cpuusa to be a double all the math involves integers that are not cast as doubles so it rounds the result to zero or unity.

Here is what I implemented and verified works correctly:

#import "kernel32.dll"
bool GetSystemTimes(int & lpIdleTime[], int & lpKernelTime[], int & lpUserTime[]);
#import

   int a[1], b[1], c[1], d[1], e[1], f[1];

   GetSystemTimes(a, b, c);  // get the initial system times

   Sleep(1000);  // sleep 1 seconds
   GetSystemTimes(d, e, f);  // get the final system times after 1 second wait
   int i =0;
   int idle = d[i]-a[i]; // final time count minus initial time count
   int ker  = e[i]-b[i];
   int user = f[i]-c[i];
   int sys  = ker + user;
   double cpuusa =( (1.*sys - 1.*idle) *100. / (1.*sys) ); // ensure the integer math is treated as doubles math for casting purposes
   Alert ("cpu usage is now: " + cpuusa + " %");
Thanks for your qjol! The CPU utilization code works great :)
 
1005phillip:

Yeah you got your initial minus final ordering backwards, and while you declared cpuusa to be a double all the math involves integers that are not cast as doubles so it rounds the result to zero or unity.

Here is what I implemented and verified works correctly:

Thanks for your qjol! The CPU utilization code works great :)


as i said i'm not sure about the calculation

thanks for the correction of the calculation

 

just one thing u can change the double

double cpuusa =( (1.*sys - 1.*idle) *100. / (1.*sys) );

to an int

int cpuusa =( (1.*sys - 1.*idle) *100. / (1.*sys) );
 

Thanks qjol, I messed around for ages trying to get GetCurrentDirectoryA to work, and used your method to fix that as well. Can you explain WHY it works? The SDK signature appears to need an int, but we are passing a string?

#import "kernel32.dll"

int GetCurrentDirectoryA(string bufferlen, string currentdir);

#import

qjol: