Kernel32DLL / GetComputerName in MT4 Build 1090 ?

 

Hello everybody,

I need to retrieve the ComputerName from the local machine and I found some (older) posts regarding this question from 2011 and 2014, but actual NOT ONLY ONE of these exchanged solutions are working with MT4 running on Win7 (neither 32 nor 64-Bit OS).

Either I get EmptyStrings as result or strings containing just extra-character but not the ComputerName which am locking for.

I have tested ALL examples, which I found here in this forum, the code from my last test and the related output are placed below.

Can someone provide an actual axample, how the ComputerName can be retrieved while Running MT4 on Win7-OS ?

Many thanks in advance...

dsalomon

**********************************************

CODE:

#import "kernel32.dll" 
int GetComputerNameW(char &lpBuffer[], int &nSize[]);
int GetEnvironmentVariableW(string lpName, string lpBuffer, int nSize);
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   char buf[1024];
   int CNSZ[1024];
   string sCOMPUTERNAME = "012345678901234567890123456789012345678901234567890123456789";
   string ComputerName = "";
   //---
   int result = GetEnvironmentVariableW("COMPUTERNAME", "", StringLen(sCOMPUTERNAME));
   Alert("ComputerName is "+ComputerName);
   ArrayResize(buf, result * 2);
   ArrayResize(CNSZ, result * 2);
   GetComputerNameW(buf, CNSZ);
   //---
   for(int i = 0; i < ArraySize(buf); i++)
      {
      if(buf[i] != 0)
      ComputerName = ComputerName + CharToString(buf[i]);
      }
   Alert("Computer Name is=: ", ComputerName);
   Alert("result is=: ", result);
  }
//+------------------------------------------------------------------+


OUTPUT:

2017.12.25 16:48:52.146 DLL2 USDJPY,H1: Alert: Computer Name is=: wpj:Ø)wð



**********************************************

 
Hostname
  • technet.microsoft.com
Hostname displays the name of the host on which the command is issued. The command has no other switches or parameters. The host name displayed matches the name configured on the Network table in Control Panel-System .
 
Marco vd Heijden:

https://technet.microsoft.com/en-us/library/cc940113.aspx

Hello again,

as hostname is an OS-command it is not possible to execute this within the MT4-Environment (as far as I know).

The hostname/ComputerName is needed here within an EA/Script, therefore I tried to use the system´s DLLs according to the historical posts.

Maybe I have an informational gap - can I execute the hostname-command directly from the MT4/5-environment and how to pass the output then back to MT4/5 ?

I guesss, that this would no tbe possible already for security-reasons ... (?)

Any comments are welcome :-)

Many thanks

dsalomon

 

Can you try this?

#import "kernel32.dll" 
int GetComputerNameW(short &lpBuffer[], int &nSize[]);
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {

   ushort buf[1024];
   int len [1];
   len[0] = 1024;
   string ComputerName = "";
   //---
   GetComputerNameW(buf, len);
   //---
   Alert (ShortArrayToString(buf));
  }
//+------------------------------------------------------------------+
 
Vadim Larionov:

Can you try this?

Hello Vadim,

great ! This works with MT4 (Build 1090) running on Win7 32-Bit and on Win7 64-Bit too.

Seems, that char vs. ushort was the main-issue !

I will provide an update regarding Win10 once tested.

Thanks a lot !!

dsalomon

Reason: