Can't get ComputerName anymore

 

Hi,

I'm having a new problem with an old function that gets the Computer Name on Build 646.

#import "kernel32.dll" 
int GetComputerNameW(string  lpBuffer, int nSize);
int GetEnvironmentVariableW(string lpName, string lpBuffer, int nSize);
#import
string GetComputerName()
  {
   string buf="123456789012345678901234567890123456789012345678901234567890";
   string   sCOMPUTERNAME="012345678901234567890123456789012345678901234567890123456789";
   int result;   
   
   if(!IsDllsAllowed()) MessageBox("DLLs need to be enabled for this.");

   // get the Computername from the Environment Var
   result = GetEnvironmentVariableW("COMPUTERNAME", sCOMPUTERNAME, StringLen(sCOMPUTERNAME)-12); // get the Computername from the Environment Var
   Print ("GetEnvironmentVariableW() returned=", result, "; COMPUTERNAME= ",sCOMPUTERNAME);
   
   // get the Computername from the GetComputerNameW API...
   result = GetComputerNameW(buf, StringLen(buf));
   Print("GetComputerNameW(): returned ", result, "; ComputerName=", buf);

   return(buf);
  }

Previously, the function only had a call to GetComputerNameA, but that started causing an "Invalid Ex4 file" error when loading the EA. I've updated it to GetComputerNameW, but the buffer I'm passing does not get updated.

I added a call to GetEnvironmentVariableW to try to get the computer name from the COMPUTERNAME env var.

The GetEnvironmentVariableW() call returns 7 (the length of the environment variable/machine name), but does not update the buffer either.

I've tried 2 import definitions for GetComputerNameW():

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

Causes error: Access violation read to 0x0000003c in 'kernel32.dll'.

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

Returns 1 (success) but does not update the buffer. (need to cast param 2 in the call to string).

Can someone please help me work out what I'm doing wrong, or confirm if you are also having this problem?

Many thanks,

Mike

 
int GetComputerNameW(char  &lpBuffer[], int &nSize[]);
 
qjol:


Hi qjol,

Thanks, that helped me work it out. Although I had to make 2 changes.

My import definition is now:

int GetComputerNameA(char  &lpBuffer[], int &nSize);

Note the change back to GetComputerNameA. If I used GetComputerNameW, the char buffer would have every second element as 0, and CharBufferToString() would only convert the first char.

My call is:

   string sComputerName="";
   char cBuffer[20];
   int  nSize=20;
   int result;   

   // get the Computername from the GetComputerNameA API...
   result = GetComputerNameA(cBuffer, nSize);
   sComputerName=CharArrayToString(cBuffer,0,WHOLE_ARRAY,CP_ACP);

I really appreciate your help with this, I spent all morning on it..

Mike

 

you maybe get problems in the future using GetComputerNameA since MQ claims/wants from B600 > to use strings as UNICODE

i do used W (IMHO you should either)

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


   char buf[1024];
   int CNSZ[1024];
   string sCOMPUTERNAME = "012345678901234567890123456789012345678901234567890123456789";
   string ComputerName = "";
   //---
   int result = GetEnvironmentVariableW("COMPUTERNAME", "", StringLen(sCOMPUTERNAME));
   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);
 
qjol:

you maybe get problems in the future using GetComputerNameA since MQ claims/wants from B600 > to use strings as UNICODE

i do used W (IMHO you should either)




I agree that the W call should be used. I've updated to use it.

However I had to use this declaration:

int GetComputerNameW(char &lpBuffer[], int &nSize);

Cheers

 
Mike #: However I had to use this declaration:
int GetComputerNameW(char &lpBuffer[], int &nSize[]);
int GetEnvironmentVariableW(string lpName, string lpBuffer, int nSize);

Wrong. Perhaps you should read the manual.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

  1. The “W” indicates it needs a wide string, not a char. All strings in MT4/MT5 are wide since Build 600 (2013) You can not use the “A” variant.

  2. The buffers are output variables, you need to pass by reference.

  3. The buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1 characters.
              GetComputerNameW function (winbase.h) - Win32 apps | Microsoft Learn
  4. LpName is also passed by reference
              GetEnvironmentVariableW function (processenv.h) - Win32 apps | Microsoft Learn