I find the windows folder

Guys,

I'm trying to run a file that is located in:

C:Program Files

Only that there is so much C:Program Files as C:Program Files(x86)

I am using # define (as you can see in the code below), is there any way to detect which folder is in the system?

the current code is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>

#define Program_files    "\"C:\\Program Files\\Automatos\\Desktop Agent\\smbios_p.exe\""
#define Program_files86  "\"C:\\Program Files (x86)\\Automatos\\Desktop Agent\\smbios_p.exe\""

int main( void )
{

   int n;

     FILE *p = _popen(Program_files,"r");
    fscanf(p,"%s",&teste);

	 n = atoi(teste);
	 	 
	 printf("Processo: %d\n", n);

	

   system("Pause");
}

Last edited on
closed account (yADwAqkS)
1
2
#define Program_files    "C:\\Program Files\\Automatos\\Desktop Agent\\smbios_p.exe\"
#define Program_files86  "C:\\Program Files (x86)\\Automatos\\Desktop Agent\\smbios_p.exe\" 

You shouldn't use double "s at the end.
And, what is the point of
"\"...
?
You should detect these at runtime, not by using #define's.
first this
 
"\"C:\\Program Files\\Automatos\\Desktop Agent\\smbios_p.exe\""


should be this
 
"C:\\Program Files\\Automatos\\Desktop Agent\\smbios_p.exe"


and second after u open the file, check the return value of _popen(); (aka check pointer you've been given)

1
2
3
4
5
FILE *p = _popen(Program_files,"r");
if(p==NULL)
{
               //file is not opened, do stuff here
}
+1 modoran run-time detection is the correct way to do this. Unless you need to start up COM for something else I would use "GetEnvironmentStrings()" to list the available environment variables and look at the "ProgramFiles" variable.

- GetEnvironmentStrings(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx

If you do end up using COM then you'd run an "ExecQuery" against 'Win32_Product' and look for the 'Name' property with the "Next" method. Once you find what you're looking for grab the 'InstallLocation' property, it will be in a BSTR format which isn't quite the same as a wchar_t. This way takes a lot longer (as a human being you will notice the difference in time) but it is going to be the one with the fewest mistakes.

- IWbemServices::ExecQuery(): http://msdn.microsoft.com/en-us/library/aa392107(v=vs.85).aspx

- IEnumWbemClassObject::Next(): http://msdn.microsoft.com/en-us/library/aa390860(v=vs.85).aspx

EDIT: Whoops! Almost forgot:

- Win32_Product: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394378(v=vs.85).aspx
Last edited on
Topic archived. No new replies allowed.