1) 使用windows 頭文件
API 頭文件允許32位和64位應用程序,包含了ANSI版本和UNICODE版本的聲明。
如果安裝更新SDK的話,那么就可能有頭文件的多個版本在機器上面。
一些函數的使用可能會通過使用條件編譯代碼依賴于某個特定版本的操作系統,為了編譯成功,你得定義比較合適的macro.頭文件使用宏來指示哪個版本系統支持編程元素。
http://msdn2.microsoft.com/en-us/library/aa383745(VS.85).aspx2) 初始化類中的成員模板
i) 使用非模板的方式
template <typename Argtype>
class Option
{
public:
Option( void (*func_name)(Argtype), Argtype Arg1 )
: MenuFunction<Argtype>( (*func_name)(Argtype), Argtype Arg1 )
{
}
private:
MenuFunction<Argtype> function;
};
template <typename Argtype>
Option<Argtype> makeOption(void (*func_name)(Argtype), Argtype Arg1 )
{
return Option<Argtype>(func_name, Arg1);
}ii) 使用多態
class Option
{
private:
class FunctionBase
{
public:
virtual ~FunctionBase() {}
virtual void call() = 0;
};
template <typename Argtype>
class Function : public FunctionBase
{
public:
Function(void (*func_name)(Argtype), Argtype arg) :
m_func(func_name, arg)
{
}
void call()
{
m_func(); // or whatever
}
private:
MenuFunction<Argtype> m_func;
};public:
template<typename Argtype> Option( void (*func_name)(Argtype), Argtype Arg1 )
{
// of course, this means you need a destructor, copy constructor, and assignment operator
// function->call() would invoke the function
function = new Function<Argtype>(func_name, Arg1);}
FunctionBase * function;};
3 大小寫字符串比較大小(考慮區域性語言的問題)
#include <iostream>
#include<algorithm>
#include<functional>
#include<boost/bind.hpp>
#include<string>
#include<locale>
struct CaseSensitiveString
{
public:
bool operator()(const std::string & lhs,const std::string & rhs)
{
std::string lhs_lower;
std::string rhs_lower;
std::transform(lhs.begin(),lhs.end(),std::back_inserter(lhs_lower),bind(std::tolower<char>,_1,_loc));
std::transform(rhs.begin(),rhs.end(),std::back_inserter(rhs_lower),bind(std::tolower<char>,_1,_loc));
return lhs_lower < rhs_lower;
}
CaseSensitiveString(const std::locale & loc):_loc(loc){}
private:
std::locale _loc;
};
詳細內容見:
http://learningcppisfun.blogspot.com/2008/04/case-insensitive-string-comparison.html4 找不到msctf.h問題
在用DX自帶的dxut做界面程序的時候,整個程序編制下來就出現了這個錯誤
fatal error C1083: Cannot open include file: 'msctf.h': No such file or directory
很詭異的,在dxsdk里面也找不到,想了很久,才發現自己沒有安裝platform sdk.因為win32程序之類的,最好都要安裝這些sdk之類的。具體的信息可以在這里得到
http://www.gamedev.net/community/forums/topic.asp?topic_id=4813585 重載 , 覆蓋,隱藏
重載與覆蓋有以下的區別:
重載:同一類,相同函數名,不同函數參數,不一定要有virtual 關鍵字
覆蓋:子類和父類,相同函數名, 相同函數參數,一定要有virtual 關鍵字
隱藏:1)如果派生類的函數名與基類的函數名相同,但是參數不同,不論有無virtual關鍵字,基類的函數將被隱藏(與重載區別開來)
2)如果派生類的函數名與基類的函數名相同,并且參數相同,但是基類沒有virtual關鍵字,基類的函數將被隱藏(與覆蓋區別開來)
6 快速加載文件
在游戲里面,一般對從硬盤或者DVD加載資源要求比較高的,一般采用這樣的方法:
for(int i = 0; < NumBlocks; i++)
{
// VirtualAlloc() creates storage that is page aligned
// and so is disk sector aligned
blocks[i] = static_cast<char *>
(VirtualAlloc(0, BlockSize, MEM_COMMIT, PAGE_READWRITE));
ZeroMemory(&overlapped[i], sizeof(OVERLAPPED));
overlapped[i].hEvent = CreateEvent(0, false, false, 0);
}
HANDLE hFile = CreateFile(FileName, GENERIC_READ, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING |
FILE_FLAG_OVERLAPPED | FILE_FLAG_SEQUENTIAL_SCAN, 0);
int iWriterPos = 0;
int iReaderPos = 0;
int iIOPos = 0;
int iPos = 0;
do
{
while(iWriterPos - iReaderPos != NumBlocks && iIOPos < FileSize)
{
overlapped[iWriterPos & NumBlocksMask].Offset = iIOPos;
int iLeft = FileSize - iIOPos;
int iBytesToRead = iLeft > BlockSize ? BlockSize: iLeft;
const int iMaskedWriterPos = iWriterPos & NumBlocksMask;
ReadFile(hFile, blocks[iMaskedWriterPos], iBytesToRead, 0,
&overlapped[iMaskedWriterPos]);
iWriterPos++;
iIOPos += iBytesToRead;
}
const int iMaskedReaderPos = iReaderPos & NumBlocksMask;
WaitForSingleObject(overlapped[iMaskedReaderPos].hEvent, INFINITE);
int iLeft = FileSize - iPos;
int iBytesToRead = iLeft > BlockSize ? BlockSize: iLeft;
memcpy(&g_buffer[iPos], blocks[iMaskedReaderPos], iBytesToRead);
iReaderPos++;
iPos += iBytesToRead;
}
while(iPos < FileSize);
CloseHandle(hFile);
for(int i = 0; i < NumBlocks; i++)
{
VirtualFree(blocks[i], BlockSize, MEM_COMMIT);
CloseHandle(overlapped[i].hEvent);
}
char* s vs char s[]
char s1[] = "abcd";// 定義一個未指定長度的char型數組,并使用字符串"abcd"將之初始化
char *s2 = "abcd";// 定義一個char型指針,并將其指向字符串"abcd",該字串位于靜態存儲區
s1[0] = 'm';// 無編譯期、運行期錯誤
s2[0] = 'm';// 無編譯器錯誤,但運行期試圖修改靜態內存,所以發生運行期錯誤
char s*只是被賦予了一個指針,char s[]是在棧中重新開辟了空間,可以在程序中寫,而不引起程序崩潰。
所以相比較而言,使用字符串數組要比字符指針要安全的多,要慎用char*s 和char s[].
7 can not find MSVCR80.dll
在安裝了vc2005之后,發現錯誤報告說MSVCR80.dll,以為又要重新安裝vc2005了,但是在網絡上面搜索到另外一個例子說,其實可以不用安裝vc2005,直接改變配置就好了,于是就有這個了:
http://blogs.msdn.com/seshadripv/archive/2005/10/30/486985.aspx
http://www.codeguru.com/forum/showthread.php?t=439964
工程架構:
新建一個空白的 solution.
然后在新建的solution上面添加vcproject.
并且也可以在子空白solution上面添加vcproject.
1> ]
1>正在編譯資源...
1>正在鏈接...
1>LINK : warning LNK4075: 忽略“/INCREMENTAL”(由于“/OPT:ICF”規范)
1>fatal error C1900: Il mismatch between 'P1' version '20060201' and 'P2' version '20050411'
1>LINK : fatal error LNK1257: 代碼生成失敗
1>生成日志保存在“file://e:\demo-work\LocalVersionTianJi\_out\DragoonApp\Release\BuildLog.htm”
1>DragoonApp - 1 個錯誤,5382 個警告
========== 全部重新生成: 0 已成功, 1 已失敗, 0 已跳過 ==========
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1512436&SiteID=1
http://forum.codecall.net/c-c/6244-fatal-error-c1900-il-mismatch-between-p1-version-20060201-p2-version-2005-a.html
http://www.codeguru.com/forum/archive/index.php/t-144030.html
timeGetTime: 頭文件 mmsystem.h,庫文件 winmm.lib
獲得當前窗口的句柄。
HWND hwnd=::GetForegroundWindow();
hwnd就保存了當前系統的最頂層窗口的句柄
GetSafehWnd 取你程序所在窗口類的句柄
GetActiveWindow 取當前活動窗口句柄
AfxGetMainWnd 取主窗口句柄
GetForegroundWindow 取前臺窗口句柄
FindWindow
EnumWindow
改變窗口屬性:
SetWindowLong
SetClassLong.
strcpy strncpy memcpy.
strcpy:按照msdn的話說是:No overflow checking is performed when strings are copied or appended,即沒有嚴格的長度檢查,所以即使是溢出也無法被檢查出來,以及The behavior of strcpy is undefined if the source and destination strings overlap.
strncpy:雖然加入了size這個來限制,但是這個size小于或者等于字符長度的話,那么該信息是不被加上字符串結束符的.并且仍舊存在跟strcpy一樣的問題, The behavior of strncpy is undefined if the source and destination strings overlap
memcpy:具體的用法跟strncpy類似,也加入了size的成分在里面,但是卻比strncpy好用得多.
If the source and destination overlap, this function does not ensure that the original source characters in the overlapping region are copied before being overwritten.
Use memmove to handle overlapping regions.
顯然它能夠處理重疊的部分,安全可靠.
并且:
The first argument, dest, must be large enough to hold count characters of src; otherwise, a buffer overrun can occur.
上次遇到的問題是我將一串漢字用strcpy來拷貝到緩沖里面,結果發現出現了亂碼.
strncpy, strcpy還是建議少用,換用memcpy+memmove(如果存在重疊的情況)吧:)
未完待續.