今天用了一下RasEnumConnections函數(shù),發(fā)現(xiàn)MSDN上的例子是錯(cuò)誤的。
MSDN上RasEnumConnections的文檔是這么說(shuō)的:
代碼如下:
To determine the required buffer size, call RasEnumConnections with lprasconn set to NULL. The variable pointed to by lpcb should be set to zero. The function will return the required buffer size in lpcb and an error code of ERROR_BUFFER_TOO_SMALL.
MSDN上提供的示例代碼也是這么寫(xiě)的:
代碼如下:
DWORD __cdecl wmain(){
DWORD dwCb = 0;
DWORD dwRet = ERROR_SUCCESS;
DWORD dwConnections = 0;
LPRASCONN lpRasConn = NULL;
dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
if (dwRet == ERROR_BUFFER_TOO_SMALL) {
// ......
}
可惜這個(gè)例子是錯(cuò)誤的,至少在XP SP3上是錯(cuò)誤的,RasEnumConnections函數(shù)返回的是ERROR_INVALID_SIZE(632),而不是ERROR_BUFFER_TOO_SMALL。解決的方法是第一個(gè)參數(shù)不要傳入NULL,而是傳入一個(gè)正確設(shè)置了dwSize的RASCONN結(jié)構(gòu),一般dwSize設(shè)為sizeof(RASCONN)即可;但是如果你的程序要在早期的系統(tǒng)上運(yùn)行,則需要HARD CODE成目標(biāo)系統(tǒng)的值。
更多信息請(qǐng)查看IT技術(shù)專欄