Download MetaTrader 5
To add comments, please log in or register
Any problems with the code? Find solution in documentation!
Carl Schreiber
6580
Carl Schreiber 2014.01.28 10:28 

Hi, I tried the google glass bowl and the 'local' search but none of them spoke to me.

How can I use windwos %HOMEPATH% - one of the windows-paths? (http://www.askvg.com/list-of-environment-variables-in-windows-xp-vista-and-7/)

Thanks in advance!

gooly

Ex Ovo Omnia
3111
Ex Ovo Omnia 2014.01.28 11:16  
gooly:

Hi, I tried the google glass bowl and the 'local' search but none of them spoke to me.

How can I use windwos %HOMEPATH% - one of the windows-paths? (http://www.askvg.com/list-of-environment-variables-in-windows-xp-vista-and-7/)

Thanks in advance!

gooly


Not sure if you are on the correct forum, this is neither Google, nor Microsoft here.
Carl Schreiber
6580
Carl Schreiber 2014.01.28 11:25  

I found it!

This works:

#import "kernel32.dll"
        bool ExpandEnvironmentStringsA(string lpSrc, string & lpDst[], string nSize);
        int GetEnvironmentVariableA(string lpName, string lpBuffer, string nSize);
#import

   string   g[1] = {""};
   GetEnvironmentVariableA("USERPROFILE", g[0], 1000);
   //ExpandEnvironmentStringsA("%HOMEPATH%",g, StringLen(g[0]));
   Print("USERPROFILE = >"+g[0]+"<");

But this causes mt4 to crash immediately - interesting way to stop everything immediately:

#import "kernel32.dll"
        bool ExpandEnvironmentStringsA(string lpSrc, string & lpDst[], string nSize);
        int GetEnvironmentVariableA(string lpName, string lpBuffer, string nSize);
#import

   string   g[1] = {""};
   //GetEnvironmentVariableA("USERPROFILE", g[0], 1000);
   ExpandEnvironmentStringsA("USERPROFILE",g, StringLen(g[0]));
   Print("USERPROFILE = >"+g[0]+"<");


%HOMEPATH% does not return this path: C:\Users\{username}

but only \Users\{username}

gooly

whroeder1
14457
whroeder1 2014.01.28 13:57  
gooly: This works:
   string   g[1] = {""};
   GetEnvironmentVariableA("USERPROFILE", g[0], 1000);
I doubt it. 1) You have to pass the array not an element g[0], 2) The buffer is zero length but you tell GEVA it's 1000, so you are over-writing other memory. Try:
string g[] = {"12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 ​6 2345678 ​7 2345678 ​8 2345678 ​9 23456789"};​
int len = GetEnvironmentVariableA("USERPROFILE", g, StringLen(g[0]);
string envVar = StringSubstr(g[0], 0, len);
Print("USERPROFILE = >"+envVar+"<");
GetEnvironmentVariable function (Windows)
Carl Schreiber
6580
Carl Schreiber 2014.01.28 15:25  

hmm - your (partly) right ;)

if I do this (in a script) (no matter whether I define g[] = {..} or g[1] = {..}:

        string g[1] = {"12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 ​6 2345678 ​7 2345678 ​8 2345678 ​9 23456789"};
        GetEnvironmentVariableA("USERPROFILE", g, StringLen(g[0]));
I get this compiler-errors:
',' - left square parenthesis expected for array        C:\Users\..\experts\scripts\testScript.mq4 (22, 41)
',' - parameter expected        C:\Users\..\experts\scripts\testScript.mq4 (22, 41)


If I do this:

        string g[1] = {"12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 ​6 2345678 ​7 2345678 ​8 2345678 ​9 23456789"};
        GetEnvironmentVariableA("USERPROFILE", g[], StringLen(g[0]));
I get this compiler errors:
']' - wrong dimension   C:\Users\..\experts\scripts\testScript.mq4 (22, 42)
',' - parameter expected        C:\Users\..\experts\scripts\testScript.mq4 (22, 43)

Only this compiles without an error:

        string g[1] = {"12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 ​6 2345678 ​7 2345678 ​8 2345678 ​9 23456789"};
        GetEnvironmentVariableA("USERPROFILE", g[0], StringLen(g[0]));

I know it against the 'rules' but... ?

Gooly

whroeder1
14457
whroeder1 2014.01.28 16:34  
 int GetEnvironmentVariableA(string lpName, string lpBuffer, string nSize);
string lpBuffer not string& lpBuffer. Try dropping the array:
string g="12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 ​6 2345678 ​7 2345678 ​8 2345678 ​9 23456789"};
int len = GetEnvironmentVariableA( "USERPROFILE", g, StringLen(g) );
Carl Schreiber
6580
Carl Schreiber 2014.01.28 16:48  

ok this is working - thanks!:

        string g = "12345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 ?6 2345678 ?7 2345678 ?8 2345678 ?9 23456789";
        GetEnvironmentVariableA("USERPROFILE", g, StringLen(g));

gooly

/
To add comments, please log in or register