As we know, we can use API OutputDebugString to show string on output window, like,
OutputDebugString(" This is debug message. ");
OutputDebugStringW(L" This is debug message. ");
You also can attach some variable to the output string.
wchar_t* world = L"world";
wchar_t* szBuffer = new wchar_t[512];
wsprintf(szBuffer, _T(" Hello %s \n"), world);
OutputDebugString(szBuffer);
delete[] szBuffer;
or
wchar_t* message = L"world !";
wchar_t* str = L" Hello ";
wchar_t* buffer = new wchar_t[sizeof(str)+sizeof(message)+1];
wcscpy(buffer, str);
wcscat(buffer, message);
OutputDebugStringW(buffer);
or
float value = 100;
wchar_t* szBuffer = new wchar_t[512];
wsprintf(szBuffer, L" My math grade is %d \n", (int)value);
OutputDebugString(szBuffer);
delete[] szBuffer;
or
float value = 100;
char* szBuffer = new char[512];
sprintf(szBuffer, L" My math grade is %f \n", value);
OutputDebugStringA(szBuffer);
delete[] szBuffer;
Murmur of a rookie software engineer came from Taiwan.
(Sorry for my poor English, I just practice it here.)
Please feel free to contact me for anything: ninna.tw@gmail.com
2013年1月16日 星期三
2012年8月20日 星期一
[C++] Threading Sample
This is a simple sample for understanding of thread create in C++.
Reference:
http://wuminfajoy.blogspot.tw/2010/10/c.html
| #include "stdafx.h" #include <windows.h> #include <process.h> #include <stdio.h> void myfunc(int n); int _tmain(int argc, _TCHAR* argv[]) { HANDLE thd[2]; DWORD tid; printf("Thead Start.\n"); for (int i=0; i<2; i++) { thd[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)myfunc, (void*)i, 0, &tid); printf("Thread %d is start \n",tid); } Sleep(1000); printf("main is end \n"); system("pause"); } void myfunc(int n) { int i; for(int i=0;i<5;i++){ printf("Thread %d, index=%d\n", n, i); Sleep(1000); } printf("Thread %d is over \n", getpid()); } |
Reference:
http://wuminfajoy.blogspot.tw/2010/10/c.html
2012年5月25日 星期五
Watch Virtual / Physical Memory Usage
With previous article http://twnin.blogspot.com/2012/05/watch-process-memory-usage.html
There is another way to watch memory usage in program with win32 api.
There is another way to watch memory usage in program with win32 api.
MEMORYSTATUS ms;
ms.dwLength = sizeof(ms);
GlobalMemoryStatus(&ms);
const int nBufSize = 512;
TCHAR chBuf[nBufSize];
::ZeroMemory(chBuf,nBufSize);
// Process-specific info (for calling process)
wsprintf(chBuf,_T("TotalVirtual: %ld\n"), ms.dwTotalVirtual);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("AvailVirtual: %ld\n"), ms.dwAvailVirtual);
OutputDebugString(chBuf);
// Process virtual memory usage (reserved + commit)
wsprintf(chBuf,_T("UsageVirtual: %ld\n"), ms.dwTotalVirtual - ms.dwAvailVirtual);
OutputDebugString(chBuf);
// System info
wsprintf(chBuf,_T("MemoryLoad: %ld\n"), ms.dwMemoryLoad);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("TotalPhys: %ld\n"), ms.dwTotalPhys);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("AvailPhys: %ld\n"), ms.dwAvailPhys);
OutputDebugString(chBuf);
// System usage
wsprintf(chBuf,_T("UsagePhys: %ld\n"), ms.dwTotalPhys - ms.dwAvailPhys);
OutputDebugString(chBuf);
// Unused/unsupported
wsprintf(chBuf,_T("TotalPageFile: %lu\n"), ms.dwTotalPageFile);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("AvailPageFile: %lu\n"), ms.dwAvailPageFile);
OutputDebugString(chBuf);
ms.dwLength = sizeof(ms);
GlobalMemoryStatus(&ms);
const int nBufSize = 512;
TCHAR chBuf[nBufSize];
::ZeroMemory(chBuf,nBufSize);
// Process-specific info (for calling process)
wsprintf(chBuf,_T("TotalVirtual: %ld\n"), ms.dwTotalVirtual);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("AvailVirtual: %ld\n"), ms.dwAvailVirtual);
OutputDebugString(chBuf);
// Process virtual memory usage (reserved + commit)
wsprintf(chBuf,_T("UsageVirtual: %ld\n"), ms.dwTotalVirtual - ms.dwAvailVirtual);
OutputDebugString(chBuf);
// System info
wsprintf(chBuf,_T("MemoryLoad: %ld\n"), ms.dwMemoryLoad);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("TotalPhys: %ld\n"), ms.dwTotalPhys);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("AvailPhys: %ld\n"), ms.dwAvailPhys);
OutputDebugString(chBuf);
// System usage
wsprintf(chBuf,_T("UsagePhys: %ld\n"), ms.dwTotalPhys - ms.dwAvailPhys);
OutputDebugString(chBuf);
// Unused/unsupported
wsprintf(chBuf,_T("TotalPageFile: %lu\n"), ms.dwTotalPageFile);
OutputDebugString(chBuf);
wsprintf(chBuf,_T("AvailPageFile: %lu\n"), ms.dwAvailPageFile);
OutputDebugString(chBuf);
Watch Process Memory Usage
To watch current process memory usage, we can use GetProcessMemoryInfo to get paging information.
It is sample code below.
HANDLE hProcess = ::GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
::GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
const int nBufSize = 512;
TCHAR chBuf[nBufSize];
::ZeroMemory(chBuf,nBufSize);
wsprintf(chBuf,_T("PageFaultCount: 0x%08X\n"), pmc.PageFaultCount );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("PeakWorkingSetSize: 0x%08X\n"),
pmc.PeakWorkingSetSize );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("WorkingSetSize: 0x%08X\n"), pmc.WorkingSetSize );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaPeakPagedPoolUsage: 0x%08X\n"),
pmc.QuotaPeakPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaPagedPoolUsage: 0x%08X\n"),
pmc.QuotaPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaPeakNonPagedPoolUsage: 0x%08X\n"),
pmc.QuotaPeakNonPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaNonPagedPoolUsage: 0x%08X\n"),
pmc.QuotaNonPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("PagefileUsage: 0x%08X\n"), pmc.PagefileUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("PeakPagefileUsage: 0x%08X\n"),
pmc.PeakPagefileUsage );
OutputDebugString(chBuf);
CloseHandle(hProcess);
PROCESS_MEMORY_COUNTERS pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
::GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
const int nBufSize = 512;
TCHAR chBuf[nBufSize];
::ZeroMemory(chBuf,nBufSize);
wsprintf(chBuf,_T("PageFaultCount: 0x%08X\n"), pmc.PageFaultCount );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("PeakWorkingSetSize: 0x%08X\n"),
pmc.PeakWorkingSetSize );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("WorkingSetSize: 0x%08X\n"), pmc.WorkingSetSize );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaPeakPagedPoolUsage: 0x%08X\n"),
pmc.QuotaPeakPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaPagedPoolUsage: 0x%08X\n"),
pmc.QuotaPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaPeakNonPagedPoolUsage: 0x%08X\n"),
pmc.QuotaPeakNonPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("QuotaNonPagedPoolUsage: 0x%08X\n"),
pmc.QuotaNonPagedPoolUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("PagefileUsage: 0x%08X\n"), pmc.PagefileUsage );
OutputDebugString(chBuf);
wsprintf(chBuf,_T("PeakPagefileUsage: 0x%08X\n"),
pmc.PeakPagefileUsage );
OutputDebugString(chBuf);
CloseHandle(hProcess);
Reference:
http://msdn.microsoft.com/en-us/library/ms682050(v=VS.85).aspx
http://blog.csdn.net/caimouse/article/details/1947575
Work around link error:
error lnk2019: unresolved external symbol _getprocessmemoryinfo@12 referenced in function
http://lanvu.wordpress.com/tag/error-lnk2019-unresolved-external-symbol-_getprocessmemoryinfo12-referenced-in-function/
It seems doesn't work on windows ce.
https://groups.google.com/group/microsoft.public.pocketpc.developer/browse_thread/thread/22c685d1df9ec17d/e05ad892433d56aa?lnk=raot&pli=1
Structure of PROCESS_MEMORY_COUNTERS
http://blog.csdn.net/zhsp1029/article/details/3162048
2012年4月12日 星期四
[Win API] Get Tick Count and Output Debug String
This is a sample to output string to debugger.
DWORD dwTickCount = GetTickCount();
LPWSTR szBuffer;
wsprintf(szBuffer, _T(" [Debug] The computer has ran %ld ms."), dwTickCount);
OutputDebugString(szBuffer);
GetTickCount() : Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.
OutputDebugString() : Sends a string to the debugger for display.
DWORD dwTickCount = GetTickCount();
LPWSTR szBuffer;
wsprintf(szBuffer, _T(" [Debug] The computer has ran %ld ms."), dwTickCount);
OutputDebugString(szBuffer);
GetTickCount() : Retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.
OutputDebugString() : Sends a string to the debugger for display.
2012年4月6日 星期五
[Win API] Check directory or file exist
This is a example for check file or directory is exist or not before you access it.
WIN32_FIND_DATA fileInfo;
HANDLE hFind = FindFirstFile(unicodestr, &fileInfo);
if (hFind == INVALID_HANDLE_VALUE)
{
bala...bala....
}
FindClose(hFind);
Reference:
http://blog.yam.com/csylvia/article/7026165
2012年3月29日 星期四
[Win API] Sample of reading txt file
It is just a simple example for reading text from .txt file using win32 api.
HANDLE hFile = CreateFile(dataFilePath, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
return;
}
// read all characters from file.
DWORD bufferSize = GetFileSize(hFile, 0);
char* buffer = new char[bufferSize];
memset(buffer, '\0', bufferSize);
bool result = false;
DWORD bytesRead = 0;
result = !!ReadFile(hFile, &buffer, bufferSize, &bytesRead, 0);
delete[] buffer;
CloseHandle(hFile);
Then, you can follow the example below to get file path.
http://twnin.blogspot.com/2012/03/get-current-directory-and-convert-to.html
訂閱:
文章 (Atom)