File: synce-list-programs.c

package info (click to toggle)
librapi2 0.9.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,204 kB
  • ctags: 1,258
  • sloc: sh: 9,013; ansic: 5,743; cpp: 522; makefile: 171
file content (154 lines) | stat: -rw-r--r-- 3,150 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* $Id: synce-list-programs.c,v 1.6 2004/01/09 20:18:14 twogood Exp $ */
#include <rapi.h>
#include <synce_log.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void show_usage(const char* name)
{
	fprintf(stderr,
			"Syntax:\n"
			"\n"
			"\t%s [-d LEVEL] [-h]\n"
			"\n"
			"\t-d LEVEL  Set debug log level\n"
			"\t              0 - No logging (default)\n"
			"\t              1 - Errors only\n"
			"\t              2 - Errors and warnings\n"
			"\t              3 - Everything\n"
			"\t-h        Show this help message\n",
			name);
}

static bool handle_parameters(int argc, char** argv)
{
	int c;
	int log_level = SYNCE_LOG_LEVEL_LOWEST;

	while ((c = getopt(argc, argv, "d:h")) != -1)
	{
		switch (c)
		{
			case 'd':
				log_level = atoi(optarg);
				break;
			
			case 'h':
			default:
				show_usage(argv[0]);
				return false;
		}
	}

	synce_log_set_level(log_level);

	return true;
}

int main(int argc, char** argv)
{
	int return_value = 1;
	HRESULT hr;
	LONG result;
	HKEY parent_key;
	WCHAR* parent_key_name = NULL;
	WCHAR* value_name = NULL;
	DWORD i;
  bool smartphone = false;
	
	if (!handle_parameters(argc, argv))
		goto exit;

	hr = CeRapiInit();

	if (FAILED(hr))
	{
		fprintf(stderr, "%s: Unable to initialize RAPI: %s\n", 
				argv[0],
				synce_strerror(hr));
		goto exit;
	}
	
  
  /* Path on SmartPhone 2002 */
  parent_key_name = wstr_from_current("Security\\AppInstall");

	result = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, parent_key_name, 0, 0, &parent_key);
  
	if (ERROR_SUCCESS == result)
  {
    smartphone = true;
  }
  else
  {
    smartphone = false;
    wstr_free_string(parent_key_name);

    /* Path on Pocket PC 2002 */
    parent_key_name = wstr_from_current("Software\\Apps");

    result = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, parent_key_name, 0, 0, &parent_key);

    if (ERROR_SUCCESS != result)
    {
      fprintf(stderr, "%s: Unable to open parent registry key: %s\n", 
          argv[0],
          synce_strerror(result));
      goto exit;
    }
  }
  
  value_name = wstr_from_current("Instl");
	
	for (i = 0; ; i++)
	{
		WCHAR wide_name[MAX_PATH];
		DWORD name_size = sizeof(wide_name);
		HKEY program_key;
		DWORD installed = 0;
		DWORD value_size = sizeof(installed);

		result = CeRegEnumKeyEx(parent_key, i, wide_name, &name_size, NULL, NULL,
				NULL, NULL);
		if (ERROR_SUCCESS != result)
			break;

    if (smartphone)
    {
      char* name = wstr_to_current(wide_name);
      puts(name);
      wstr_free_string(name);
    }
    else
    {
      result = CeRegOpenKeyEx(parent_key, wide_name, 0, 0, &program_key);
      if (ERROR_SUCCESS != result)
        continue;

      result = CeRegQueryValueEx(program_key, value_name, NULL, NULL,
          (LPBYTE)&installed, &value_size);

      if (ERROR_SUCCESS == result && installed)
      {
        char* name = wstr_to_current(wide_name);
        puts(name);
        wstr_free_string(name);
      }
      CeRegCloseKey(program_key);
    }

	}

	CeRegCloseKey(parent_key);

	return_value = 0;

exit:
	wstr_free_string(parent_key_name);
	wstr_free_string(value_name);

	CeRapiUninit();
	return return_value;
}