Where shall I add the file name in the code?

Hi,

I am trying to run the below code, and when I execute it the massage "The command line must include a valid log file name." displayed.

Pls advise. for more infor about the code check
http://msdn.microsoft.com/en-us/library/windows/desktop/aa373228(v=vs.85).aspx

Regards,

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
  #include <windows.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h>

#pragma comment(lib, "pdh.lib")

CONST PWSTR COUNTER_PATH = L"\\Processor(0)\\% Processor Time";
CONST ULONG SAMPLE_INTERVAL_MS = 1000;

void DisplayCommandLineHelp(void)
{
	wprintf(L"The command line must include a valid log file name.\n");
}

void wmain(int argc, WCHAR **argv)
{
	HQUERY hQuery = NULL;
	HLOG hLog = NULL;
	PDH_STATUS pdhStatus;
	DWORD dwLogType = PDH_LOG_TYPE_CSV;
	HCOUNTER hCounter;
	DWORD dwCount;

	if (argc != 2)
	{
		DisplayCommandLineHelp();
		goto cleanup;
	}

	// Open a query object.
	pdhStatus = PdhOpenQuery(NULL, 0, &hQuery);

	if (pdhStatus != ERROR_SUCCESS)
	{
		wprintf(L"PdhOpenQuery failed with 0x%x\n", pdhStatus);
		goto cleanup;
	}

	// Add one counter that will provide the data.
	pdhStatus = PdhAddCounter(hQuery,
		COUNTER_PATH,
		0,
		&hCounter);

	if (pdhStatus != ERROR_SUCCESS)
	{
		wprintf(L"PdhAddCounter failed with 0x%x\n", pdhStatus);
		goto cleanup;
	}

	// Open the log file for write access.
	pdhStatus = PdhOpenLog(argv[1],
		PDH_LOG_WRITE_ACCESS | PDH_LOG_CREATE_ALWAYS,
		&dwLogType,
		hQuery,
		0,
		NULL,
		&hLog);

	if (pdhStatus != ERROR_SUCCESS)
	{
		wprintf(L"PdhOpenLog failed with 0x%x\n", pdhStatus);
		goto cleanup;
	}

	// Write 10 records to the log file.
	for (dwCount = 0; dwCount < 10; dwCount++)
	{
		wprintf(L"Writing record %d\n", dwCount);

		pdhStatus = PdhUpdateLog(hLog, NULL);
		if (ERROR_SUCCESS != pdhStatus)
		{
			wprintf(L"PdhUpdateLog failed with 0x%x\n", pdhStatus);
			goto cleanup;
		}

		// Wait one second between samples for a counter update.
		Sleep(SAMPLE_INTERVAL_MS);
	}

cleanup:

	// Close the log file.
	if (hLog)
		PdhCloseLog(hLog, 0);

	// Close the query object.
	if (hQuery)
		PdhCloseQuery(hQuery);
}
closed account (18hRX9L8)
You have to add parameters to your call to the executable file. I'm not sure what IDE you are using, but you should look up how to add parameters when calling the executable.

For example, if run in CMDLine: myCProgram.exe myPdhLog.log will set the log name to myPdhLog.log.

The arguments in wmain: int wmain(int argc, WCHAR **argv) mean:
1. argc: The number of arguments on the CMDLine (in this case there will be 2, 1st is the program name itself (myCProgram.exe) and the 2nd is the log name (myPdhLog.log)).
2. argv: The tokens called in the CMDLine (in this case: argv[0] == "myCProgram.exe" and argv[1] == "myPdhLog.log").

So again, you must add the extra parameter in the CMDLine for the log name.

EDIT: Isn't wmain supposed to return an int?
Thanks usandfrinds , I am using MS Visual Stedio 2013 and the code is one of the MS examples as mentioned above.

To help me understand your response, may I know where should I add the log file, and what should be the file extension?
closed account (18hRX9L8)
Create a blank .txt file called pdh.txt and put it in the same directory as the executable. Assuming the executable's name is exec.exe, cd into the directory and call the executable like so: $ exec.exe pdh.txt. I think the program should write something to the file. Try to open it up in NotePad and see what's in it... Never done this before so no idea what's going to happen.

EDIT: See here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373228(v=vs.85).aspx for more information.
Last edited on
Topic archived. No new replies allowed.