这个程序在vc.net为什么编译通不过?请高手帮忙啊!

alcatel2 2003-12-29 12:09:31
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include <tchar.h>
#include <pdhmsg.h>
#include <pdh.h>


#define BROWSER_COUNTER_PATH_SIZE 8196
#define MAX_COUNTERS 100


// This structure is defined so that the caller of
// PdhBrowseCounters can pass information to the
// callback function that is called once for each
// "Add" click.
typedef struct {
LPTSTR lpszCounterPath;
HQUERY hQuery;
HCOUNTER *ahCounters;
DWORD dwMaxCounters;
DWORD dwIndexCount;
} PDH_BROWSE_COUNTER_ARGS, *PPDH_BROWSE_COUNTER_ARGS;


// prototypes for functions defined in this source
void print_pdh_error(
LPTSTR lpszFuncName,
PDH_STATUS pdhStatus
);

BOOL get_cmd_args(
int argc,
TCHAR **argv,
LPTSTR *lppszInfile,
LPTSTR *lppszOutfile,
LPDWORD lpdwOutFileType
);


void print_usage(LPTSTR lpszAppName);
void print_copyright(void);



PDH_STATUS WINAPI pdhBrowseCallback(
DWORD dwArg
);

// -----------------------------------------------------------------------
// FUNCTION: _tmain
//
// The main routine contains the bulk of the algorithm of transferring
// data from one log file and placing it into another performance log.
//
// The procedure for doing so is
// open source log using a query
// add counters to the query
// open the destination log file and refer to the query
// for each data sample in the source log
// read a data sample and write it to the output log
// close both log files.
// -----------------------------------------------------------------------

void __cdecl _tmain(int argc, TCHAR **argv)
{
PDH_BROWSE_DLG_CONFIG BrowseInfo;
TCHAR szCounterBuffer[BROWSER_COUNTER_PATH_SIZE];
PDH_STATUS pdhStatus = 0;
HLOG hLog = NULL;
HQUERY hQuery = NULL;
HCOUNTER ahCounters[MAX_COUNTERS];
PDH_BROWSE_COUNTER_ARGS pdhCallbackArgs;
LPTSTR lpszInfile = NULL;
LPTSTR lpszOutfile = NULL;
DWORD dwOutFileType = 0;
DWORD dwNumEntries = 0;
DWORD dwBuffSize = 0;
PDH_TIME_INFO pdhTimeRange;
DWORD i;


print_copyright();

if (
!get_cmd_args(argc, argv, &lpszInfile,
&lpszOutfile, &dwOutFileType)
)
{
print_usage(argv[0]);
return;
}

memset(szCounterBuffer,0,BROWSER_COUNTER_PATH_SIZE * sizeof(TCHAR));
memset(ahCounters, 0, MAX_COUNTERS * sizeof(HCOUNTER));
memset(&pdhCallbackArgs, 0, sizeof(PDH_BROWSE_COUNTER_ARGS));
memset(&pdhTimeRange, 0, sizeof(PDH_TIME_INFO));

pdhStatus = PdhOpenQuery(lpszInfile, 0, &hQuery);
if (IsErrorSeverity(pdhStatus))
{
print_pdh_error(TEXT("PdhOpenQuery"), pdhStatus);
return;
}


// setup the args for the browse callback
pdhCallbackArgs.hQuery = hQuery;
pdhCallbackArgs.ahCounters = ahCounters;
pdhCallbackArgs.dwMaxCounters = MAX_COUNTERS;
pdhCallbackArgs.dwIndexCount = 0;
pdhCallbackArgs.lpszCounterPath = szCounterBuffer;


memset(&BrowseInfo, 0, sizeof(BrowseInfo));

BrowseInfo.bReserved = 0;
BrowseInfo.bIncludeInstanceIndex = FALSE;
BrowseInfo.bSingleCounterPerAdd = FALSE;
BrowseInfo.bSingleCounterPerDialog = FALSE;
BrowseInfo.bLocalCountersOnly = FALSE;
BrowseInfo.bWildCardInstances = FALSE;
BrowseInfo.bHideDetailBox = TRUE;
BrowseInfo.bDisableMachineSelection = FALSE;
BrowseInfo.bInitializePath = FALSE;
BrowseInfo.bIncludeCostlyObjects = FALSE;
BrowseInfo.hWndOwner = NULL;
BrowseInfo.szReturnPathBuffer = szCounterBuffer;
BrowseInfo.cchReturnPathLength = BROWSER_COUNTER_PATH_SIZE;
BrowseInfo.pCallBack = pdhBrowseCallback;
BrowseInfo.dwCallBackArg = (DWORD)&pdhCallbackArgs;
BrowseInfo.CallBackStatus = ERROR_SUCCESS;
BrowseInfo.dwDefaultDetailLevel = PERF_DETAIL_WIZARD;
BrowseInfo.szDialogBoxCaption = TEXT("Select counters from log file");
BrowseInfo.szDataSource = (LPTSTR)lpszInfile;


pdhStatus = PdhBrowseCounters (&BrowseInfo);
if (IsErrorSeverity(pdhStatus))
{
PdhCloseQuery(hQuery);
print_pdh_error(TEXT("PdhBrowseCounters"), pdhStatus);
return;
}


pdhStatus = PdhOpenLog(
lpszOutfile,
PDH_LOG_WRITE_ACCESS | PDH_LOG_CREATE_ALWAYS,
&dwOutFileType,
hQuery, // links the input file to output file
0,
NULL, // no user caption
&hLog
);

if (IsErrorSeverity(pdhStatus))
{
PdhCloseQuery(hQuery);
print_pdh_error(TEXT("PdhOpenLog"), pdhStatus);
return;
}

// now do the transfer from the hQuery to the hLog
// this will loop until all datapoint from the
// input log file is exhausted


// Typically, you can call PdhUpdateLog until
// the all of the records of the input log have
// been read. But in my experience PdhUpdateLog
// may fail for the first record or two on some
// perfmon logs. So instead we can get the
// number of data samplings and use a for loop


// Get the time range of the input log data.
// This also retrieves the number sample count
// which is the number of records in the log file
dwBuffSize = sizeof(PDH_TIME_INFO);
pdhStatus = PdhGetDataSourceTimeRange(lpszInfile,
&dwNumEntries,
&pdhTimeRange,
&dwBuffSize);


// PdhUpdateLog will get the data from the hQuery
// and write it to the output log
for (i=0; i<pdhTimeRange.SampleCount; i++)
pdhStatus = PdhUpdateLog(hLog, NULL);


pdhStatus = PdhUpdateLogFileCatalog(hLog);
if (IsErrorSeverity(pdhStatus))
{
print_pdh_error(TEXT("PdhUpdateLogFileCatalog"), pdhStatus);
}

PdhCloseLog(hLog, 0);
PdhCloseQuery(hQuery);

}


...全文
57 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunhw2002 2004-01-12
  • 打赏
  • 举报
回复
学习ing
dddd8888 2004-01-12
  • 打赏
  • 举报
回复
学习
abitz 2004-01-04
  • 打赏
  • 举报
回复
1。这里
void print_pdh_error(
LPTSTR lpszFuncName,
PDH_STATUS pdhStatus
)
{
HMODULE hModule = NULL;
LPTSTR lpszErrMsg = NULL;

hModule = GetModuleHandle(TEXT("PDH"));

lpszErrMsg = (LPTSTR)malloc(512 * sizeof(TCHAR)); // 加上个类型转换。
....
}

2。这里

BOOL get_cmd_args(
int argc,
TCHAR **argv,
LPTSTR *lppszInfile,
LPTSTR *lppszOutfile,
LPDWORD lpdwOutFileType
)
{
if (argc < 4)
return FALSE;

*lppszInfile = argv[1];
*lppszOutfile = argv[2];

if ( ( argv[3][0] == (TCHAR)'c') || ( argv[3][0] == (TCHAR)'C') )
{
*lpdwOutFileType = PDH_LOG_TYPE_CSV;
return TRUE;
}

if ( (argv[3][0] == (TCHAR)'t') || (argv[3][0] == (TCHAR)'T') )
{
*lpdwOutFileType = PDH_LOG_TYPE_TSV;
return TRUE;
}

if ( (argv[3][0] == (TCHAR)'b') || (argv[3][0] == (TCHAR)'B') )
{
*lpdwOutFileType = PDH_LOG_TYPE_BINARY; // 原来的PDH_LOG_TYPE_BLG不知为何,
// 看起来应该改成这个。
return TRUE;
}

return FALSE;

}

3。连接时要导入pdh.lib。
具体的,在项目菜单最下端,***属性。打开,找“链接器”—>命令行,
在附加选项里写上:pdh.lib。OK。

这个东东好像要在命令行方式下执行,直接执行只会显示usage。
具体格式估计你应该知道吧?
alcatel2 2004-01-04
  • 打赏
  • 举报
回复
up
alcatel2 2003-12-29
  • 打赏
  • 举报
回复
// -----------------------------------------------------------------------
// FUNCTION: pdhBrowseCallback
//
// When the user clicks the Add button on the browse counters dialog
// presented by the PdhBrowseCounters function, this function is called.
//
// The browse counters dialog fills in the szCounterBuffer buffer (locally
// declared in main) with a path of the counter selected by the user.
// This function has a reference to this buffer, the query handle, and
// other information for counter set bookkeeping by a pointer to a
// structure (PDH_BROWSE_COUNTER_ARGS) I define in the beginning of
// this source.
// -----------------------------------------------------------------------

PDH_STATUS WINAPI pdhBrowseCallback(DWORD dwArg)
{
PDH_STATUS pdhStatus = 0;
PPDH_BROWSE_COUNTER_ARGS pdhArgs = NULL;
LPTSTR lptrPath = NULL;
HQUERY hQuery = NULL;
HCOUNTER * ahCounters = NULL;
DWORD i = 0;

pdhStatus = ERROR_SUCCESS;
pdhArgs = (PPDH_BROWSE_COUNTER_ARGS)dwArg;

hQuery = pdhArgs->hQuery;
ahCounters = pdhArgs->ahCounters;
i = pdhArgs->dwIndexCount; // current location in ahCounters array

// lpszCounterPath may be a multistring
// this works for one or many paths passed to this function
lptrPath = pdhArgs->lpszCounterPath;
while(*lptrPath)
{
if (i >= pdhArgs->dwMaxCounters)
{
_tprintf(TEXT("Counter Handle Array Limit (%d) Reached\n"),
pdhArgs->dwMaxCounters);
return ERROR_SUCCESS;
break;
}
_tprintf(TEXT("adding counter -- %s\n"), lptrPath);

pdhStatus = PdhAddCounter(hQuery, lptrPath, 0, &(ahCounters[i]));
if (ERROR_SUCCESS != pdhStatus)
{
print_pdh_error(TEXT("PdhAddCounter"), pdhStatus);
break;
}

lptrPath += lstrlen(lptrPath) + 1;
i++;
}

pdhArgs->dwIndexCount = i; // update new location in ahCounters array

return ERROR_SUCCESS; // means give control back to the UI

}


// -----------------------------------------------------------------------
// FUNCTION: print_pdh_error
//
// This function simply looks up the message text given the PDH status
// code and displays it along with the function name passed in by the
// caller.
// -----------------------------------------------------------------------

void print_pdh_error(
LPTSTR lpszFuncName,
PDH_STATUS pdhStatus
)
{
HMODULE hModule = NULL;
LPTSTR lpszErrMsg = NULL;

hModule = GetModuleHandle(TEXT("PDH"));

lpszErrMsg = malloc(512 * sizeof(TCHAR));

if (!FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, hModule,
pdhStatus, 0, lpszErrMsg, 512, NULL))
{
_tprintf(TEXT("Error %d from FormatMessage\n"), GetLastError());
if (lpszErrMsg != NULL)
free(lpszErrMsg);
return;
}

if (pdhStatus == ERROR_SUCCESS)
_tprintf(TEXT("%s returned success\n"), lpszFuncName);
else
_tprintf(TEXT("%s failed with error code %08X\n%s\n"), lpszFuncName,
pdhStatus, lpszErrMsg);

free(lpszErrMsg);
}

// -----------------------------------------------------------------------
// FUNCTION: get_cmd_args
//
// This function does the grunt-work of parsing the command-line arguments
// passed in by the user.
// -----------------------------------------------------------------------

BOOL get_cmd_args(
int argc,
TCHAR **argv,
LPTSTR *lppszInfile,
LPTSTR *lppszOutfile,
LPDWORD lpdwOutFileType
)
{
if (argc < 4)
return FALSE;

*lppszInfile = argv[1];
*lppszOutfile = argv[2];

if ( ( argv[3][0] == (TCHAR)'c') || ( argv[3][0] == (TCHAR)'C') )
{
*lpdwOutFileType = PDH_LOG_TYPE_CSV;
return TRUE;
}

if ( (argv[3][0] == (TCHAR)'t') || (argv[3][0] == (TCHAR)'T') )
{
*lpdwOutFileType = PDH_LOG_TYPE_TSV;
return TRUE;
}

if ( (argv[3][0] == (TCHAR)'b') || (argv[3][0] == (TCHAR)'B') )
{
*lpdwOutFileType = PDH_LOG_TYPE_BLG;
return TRUE;
}

return FALSE;

}


// -----------------------------------------------------------------------
// FUNCTION: print_usage
//
// This function simply displays the command-line options available in
// this utility.
// -----------------------------------------------------------------------

void print_usage(LPTSTR lpszAppName)
{
_tprintf(TEXT("Usage:\n\t%s <input file> <output file> <c | t | b>\n"),
lpszAppName);
_tprintf(TEXT("\tuse one of the following options\n"));
_tprintf(TEXT("\t\tc -- specifies outfile to be CSV\n"));
_tprintf(TEXT("\t\tt -- specifies outfile to be TSV\n"));
_tprintf(TEXT("\t\tb -- specifies outfile to be BINARY (.BLG)\n"));
}

// -----------------------------------------------------------------------
// FUNCTION: print_copyright
//
// This function simply outputs a copyright banner to the user.
// -----------------------------------------------------------------------

void print_copyright(void)
{
_tprintf(
TEXT("Performance Monitor/System Monitor Log File Convert Utility\n")
);
_tprintf(TEXT("Microsoft Systems Journal, 1999\n\n"));
}
i_jianyong 2003-12-29
  • 打赏
  • 举报
回复
总得给点出错信息吧
alcatel2 2003-12-29
  • 打赏
  • 举报
回复
up

684

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧