Download MetaTrader 5
To add comments, please log in or register
Sell products via the Market and track your sales statistics
Mike
10
Mike 2014.05.09 00:26 

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

qjol
3243
qjol 2014.05.09 05:49  
int GetComputerNameW(char  &lpBuffer[], int &nSize[]);
Mike
10
Mike 2014.05.09 09:35  
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

qjol
3243
qjol 2014.05.09 10:32  

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);
Mike
10
Mike 2014.05.10 08:46  
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

/
To add comments, please log in or register