So I did a little experiment with CreateDirectory() and fopen(). I wrote a small function to test if the application was run with Administrator rights, simply by creating a empty file/directory on the system drive. (I know there's the OpenProcessToken()/GetTokenInformation() pair which does the job better, but let's keep this simple).
This is my code
// Like str_clip (in fact it actually calls str_clip), but returns the cut string.
STRING *str_clip_a( STRING *s, int n ) {
STRING *l = str_create(_chr(s));
str_trunc(l, str_len(s)-n );
str_clip(s, n);
return l;
}
BOOL IsElevated () {
const short len = 384;
const short sublen = 3;
char drivedir[384], cdir[3];
GetSystemDirectory( drivedir, &len );
strcpy(drivedir, _chr(str_clip_a(drivedir, 3))); // strlen("C:\") = 3
strcat(drivedir, _chr ( str_for_num(NULL, rand()%256) ));
// if(0 == CreateDirectory(drivedir, 0))
if(!fopen(drivedir,"w"))
printf("Can't create file");
}
With fopen(), everything works fine. Compiled my script, run it normally, printf got executed, says it can't create the file. Right-click, chose "Run as administrator" and the file is created.
But if I substitute fopen() with CreateDirectory(), then the compiled script never creates any folder. I think creating a folder is as the same as creating a file? If so, why fopen() works but not CreateDirectory() ?
Thanks in advance.
Sorry for my bad English.