January 10, 2009

How to correctly call HttpOpenRequest() with specified accept types?

Windows API: (Wininet)

HINTERNET HttpOpenRequest(
  __in  HINTERNET hConnect,
  __in  LPCTSTR lpszVerb,
  __in  LPCTSTR lpszObjectName,
  __in  LPCTSTR lpszVersion,
  __in  LPCTSTR lpszReferer,
  __in  LPCTSTR *lplpszAcceptTypes,
  __in  DWORD dwFlags,
  __in  DWORD_PTR dwContext
);

Tips:
lplpszAcceptTypes is not a pointer to a null-terminated string, it is a pointer to a null-terminated array of strings. 

Sample Code:

// A null-terminated array of null-terminated strings
const char* lplpszAcceptTypes[] = {"text/*", NULL}; // Accept only text documents

HINTERNET  hHttpFile = HttpOpenRequestA(
  hConnect,
  "GET",
  "/TextDocument",
  HTTP_VERSION,
  NULL,
  lplpszAcceptTypes,
  INTERNET_FLAG_DONT_CACHE, 1);

Reference in MSDN:

January 01, 2009

How to get the last modified date of a file in C++?

On Win32 platform, you can get the attribute information of a file easily by just calling GetFileAttributesEx().
The information you got includes creation time, last access time, and last write time (last modified date).

Windows API:

BOOL WINAPI GetFileAttributesEx(
  __in   LPCTSTR lpFileName,
  __in   GET_FILEEX_INFO_LEVELS fInfoLevelId,
  __out  LPVOID lpFileInformation
);

Sample Code:

WIN32_FILE_ATTRIBUTE_DATA fileAttrData = {0};
GetFileAttributesExW(L"C:\\abc.txt, GetFileExInfoStandard, &fileAttrData);
// GetFileAttributesExA for non-unicode

FILETIME ftLastModifiedDate = fileAttrData.ftLastWriteTime;

Reference in This Site:

Reference in MSDN:

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.

Solution:
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,

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

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

Reference in SQLite:
http://www.sqlite.org/cvstrac/wiki?p=PerformanceConsiderations

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:
DebugBreak()

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:
http://msdn.microsoft.com/en-us/library/ms679297.aspx

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";
transform(
  strTest.begin(), strTest.end(),
  strTest.begin(),
  tolower); // toupper for upper case

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

Reference in MSDN: