December 19, 2008

How to create a file that will be deleted automatically after close its handle?

Use CreateFile() API with the flag: FILE_FLAG_DELETE_ON_CLOSE

If we want to create a file when the process starts and delete it when the process exits, CreateFile() is convenient to you.

FILE_FLAG_DELETE_ON_CLOSE means the file will be deleted automatically after close hanlde. Even if the process was crashed, it would also be deleted.

Reference in MSDN:

November 08, 2008

What happened to LocalFileTimeToFileTime() / FileTimeToLocalFileTime()?

When you want to convert a time between UTC and local, you may choose the API LocalFileTimeToFileTime() or FileTimeToLocalFileTime() for this job.

Do you know you may get an unexpected result if it is daylight saving time?

LocalFileTimeToFileTime() and FileTimeToLocalFileTime() use the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, these functions will take daylight saving time into account, even if the time you are converting is in standard time.

You can use SystemTimeToTzSpecificLocalTime() and TzSpecificLocalTimeToSystemTime() instead.

Reference in This Site:

Reference in MSDN:

November 03, 2008

Why is INSERT / UPDATE command very bad performance on SQLite?

When you do a lot of INSERT / UPDATE on a table, it is a good time to contain them within a transaction,

insert into table values (..);
insert into table values (..);
insert into table values (..);

This will make SQLite save all changes to the disk at once.

Reference in SQLite:

October 12, 2008

How to raise an user break exception (int 3) dynamically in my code?

The embedded assembly code way:
_asm int 3

The Windows API way:

After raised an user break exception in your code, the program can be attached to a debugger. For some situation, this is a good way to debug your buggy problem.

Reference in MSDN:

October 04, 2008

How to convert STL string / wstring to upper / lower case?

This is the STL algorithm way, I think it is better than the itoa() way.

Sample Code:

#include <cctype>
#include <string>

#include <algorithm>

using namespace std;

// Convert string to lower case
string strTest = "I am a STL string";
  strTest.begin(), strTest.end(),
  tolower); // toupper for upper case

// Convert wstring to upper case
wstring wstrTest = L"I am a STL wstring";
  wstrTest.begin(), wstrTest.end(),
  towupper); // towlower for lower case

Reference in MSDN:

September 28, 2008

How to mute an Audio Capture Filter of webcam in DirectShow?

If you want to mute an audio capture of webcam, you have to query IAMAudioInputMixer interface on its INPUT PIN, and then use put_Enable method to disable it.

Both the audio capture filter itself and its input pin have IAMAudioInputMixer interface, but the put_Enable / get_Enable methods are implemented on input pin only.

Reference in MSDN:

September 07, 2008

How to get the string from GUID in DirectShow?

There is a very useful global array that contains strings representing the GUIDs defined in Uuids.h

char* GuidNames[guid]

const char* GetMajorTypeString(CMediaType* pMediaType)
  return GuidNames[*(pMediaType->Type())];


September 06, 2008

Small, simple, cross-platform, free and fast C++ XML Parser

I prefer using this one of XML Parser. It is really simple and convenience. And it contains ONLY TWO files, including "xmlparser.cpp" and "xmlparser.h"

I can compile it without error on VC++ (Visual C++) 2003 and 2005, and it works very well.

You can get more detail information on author's web site.

Small, simple, cross-platform, free and fast C++ XML Parser

September 05, 2008

The easiest way to toggle / next bookmark in Visual Studio

Toggle Bookmark: Ctrl + K Ctrl +K
Next Bookmark: Ctrl + K Ctrl + N

You can find the above shortcuts on the bookmark menu, but it is NOT the easiest way to do that.

The other Shortcuts:
Toggle Bookmark: Ctrl + F2
Next Bookmark: F2
(The key mapping is "Visual C++")

August 29, 2008

Why did the RegQueryValueEx() return ERROR_MORE_DATA?

Q: Why does the RegQueryValueEx() always return ERROR_MORE_DATA?

A: If the lpData buffer is too small to receive the data, the function returns ERROR_MORE_DATA.

Windows API:

LONG WINAPI RegQueryValueEx(
  __in HKEY hKey,
  __in_opt LPCTSTR lpValueName,
  __reserved LPDWORD lpReserved,
  __out_opt LPDWORD lpType,
  __out_opt LPBYTE lpData,
  __inout_opt LPDWORD lpcbData

When you are using RegQueryValueExW(), the parameter of lpcbData
SHOULD be the array size in BYTES, not in characters.

MSDN Reference:

August 21, 2008

How to use Application Verifier for a service on Vista?

Q: After added the execution file of a service to Application Verifier(AV), I still can not find any log reported by AV.

A: You can find the logs here "c:\windows\system32\config\systemprofile\appverifierlogs". You can also copy it to the current user profile, then view logs from AV.

July 18, 2008

How to generate UUID (GUID) in C++?

Sample Code:

#include <Rpc.h>
#pragma comment(lib, "Rpcrt4.lib")

UUID uuid;
::ZeroMemory(&uuid, sizeof(UUID));

// Create uuid or load from a string by UuidFromString() function

// If you want to convert uuid to string, use UuidToString() function
WCHAR* wszUuid = NULL;
::UuidToStringW(&uuid, &wszUuid);
if(wszUuid != NULL)
  wszUuid = NULL;

Reference in MSDN:

July 17, 2008

How to use named system events (global events) in C#?

There are three classes relating to windows events, EventWaitHandle, AutoResetEvent, and ManualResetEvent.

The EventWaitHandle class can represent either automatic or manual reset events and either local events or named system events.

The AutoResetEvent class derives from EventWaitHandle and represents a local event that resets automatically.

The ManualResetEvent class derives from EventWaitHandle and represents a local event that must be reset manually.

According to above description, you should use EventWaitHanlde class to represent named system events, of course it can be either automatic or manual reset events.

MSDN reference:

April 26, 2008

Call Windows API in Ruby

Sample Code:
# Close WMP by Window Message
require 'Win32API'

FindWindow ='user32', 'FindWindow', ["P", "P"], "L")
SendMessage ='user32', 'SendMessage', ["L", "L", "P", "P"], "L")
WM_CLOSE = 0x0010

def CloseWMP()
  hwnd =, "Windows Media Player")
  if hwnd != 0 then
    id =, WM_CLOSE, nil, nil)
# End of Close WMP by Window Message


February 24, 2008

0.0F vs 0.0 vs 0.0L and 0 vs 0L

There are different types in C++.

0.0F is a float, 0.0 is a double, and 0.0L is a long double.

0 is a int and 0L is a long.

"ABC" is a ansi string and L"ABC" is a wide string.

February 23, 2008

General Protection Fault

General Protection Fault = GP Fault = GPF

You received this kind of error when the program is attempting accessing a read-only memory.

For example:

class Object
  int nValue;

Object* pObject = NULL;
int nValueOfObject = pObject->nValue; // Accessing a memory which should not be accessed

Sample code for IWMMediaProps::GetMediaType()

// Get the required buffer size
DWORD cbType = 0;
pWMMediaProps->GetMediaType(NULL, &cbType);

// Get media type
WM_MEDIA_TYPE* pType = (WM_MEDIA_TYPE*)new BYTE[cbType];
if(SUCCEEDED(pWMMediaProps->GetMediaType(pType, &cbType)))
  // Use media type here
  // ...

// Free buffer
// Should not use "delete [] pType", we have to cast it to (BYTE*) before delete it.
delete [] (BYTE*)pType;

January 25, 2008

How to initialize static member variable?

class X
    static int i;
int X::i = 0; // definition outside class declaration

January 24, 2008

Notes for QueryFilterInfo()

FILTER_INFO Info = {0};
if(Info.pGraph != NULL)
    Info.pGraph->Release(); // You MUST release here

January 19, 2008

Microsoft Win32 to Microsoft .NET Framework API Map

Microsoft Win32 to Microsoft .NET Framework API Map

This is a very useful article for C# programmer.

January 17, 2008

Notes for writing DLL in which use STL objects

You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE.

Root Cause:
Since the static data members in the executable images are not in sync, this action could result in an access violation or data may appear to be lost or corrupted.

Avoid using STL object as the parameter or the return value of an exported DLL function.

MSDN Reference:

January 10, 2008

Notes for HttpWebResponse class

You MUST call Close method to close the response and release the connection.

HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("");

HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// Insert code that uses the response object.

January 01, 2008

Notes for to remove certain filter from a graph

You should check that the graph is stopped or it will fail to remove any filter.