Memo C++ Interfaces
In my development process, Although I have an impression on some C++ APIs, I can’t write them correctly and quickly. So I write this memo for my poor memory.
API-1 ShellExecute
To perform an operation on a specified file. For example:
1
2
// open a file
ShellExecute(NULL, "open", filePath, NULL, NULL, SW_SHOW);
API-2 std::to_string and std::to_wstring
To convert a value to a string or a wide string.
1
std::string s = std::to_string(2);
API-3 WritePrivateProfileString and GetPrivateProfileString
To copy a string into the specified section of an initialization file. To retrieve a string from the specified section in an initialization file. for example:
1
int nValue = GetPrivateProfileInt("AppName","SectionName",nDefaultValue,filePath);
API-4 CComboBox::GetItemData/SetItemData
Sets the 32-bit value associated with the specified item in a combo box.(also in a CListCtrl, Grid…)
1
2
3
int index=combobox.AddString("DisplayField");
combobox.SetItemData(index,databaseIndex);
int databaseIndex=combobox.GetItemData(index);
API-5 Shell Lightweight Utility Functions
This section describes the Windows Shell lightweight utility functions. The programming elements explained in this documentation are exported by Shlwapi.dll and defined in Shlwapi.h and Shlwapi.lib.
- String Functions
- Path Functions
- Registry Functions
- Color Palette Functions
- Miscellaneous
1
2
3
wchar_t path[_MAX_PATH];
GetModuleFileNameW(NULL, path, _MAX_PATH);
PathRenameExtensionW(path, L".ini");
API-6 _variant_t
_variant_t val;
if(val.vt != VT_EMPTY){
try{
val.ChargeType(VT_BSTR);
}catch(_com_error e){};
char* cs = _com_util::ConvertBSTRToString(val.bstrVal);
delete[] cs;
}
TRY can improve stability of the program, for example:
- double(1.23) -> string(1.23) yes
- string(1.23) -> double(1.23) yes
- string(abcd) -> double(????) error
API-7 std::unique
Removes duplicate elements that are adjacent to each other in a specified range.
std::vector v;
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
API-8 struct
Use struct only when there is data, and all others use class.
API-9 explicit constructors
use explicit for single argument constructors
class Demo{
explicit Demo(int n);
...
}
Demo d(1);
Demo d1 = 1;//error: Implicit conversion is not allowed
API-10 copy constructors
Use copy constructors only when you need to copy a object.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)\
TypeName(const TypeName&);\
void operator = (const TypeName&)
class Demo{
...
private:
DISALLOW_COPY_AND_ASSIGN(Demo);//defined in private
}
API-11 TODO comments
Use TODO to comment the code that is NOT perfect, Temporary and to be done in the future.
API-12 Multiple inclusion
#define: C++ standard support #pragma once: Compiler support
#ifndef PROJECT_FILE_H_
#define PROJECT_FILE_H_
...
#endif //PROJECT_FILE_H_
API-13 UrlEscapeA
Converts characters or surrogate pairs in a URL that might be altered during transport across the Internet (“unsafe” characters) into their corresponding escape sequences.