今天用了一下RasEnumConnections函数,发现MSDN上的例子是错误的。
MSDN上RasEnumConnections的文档是这么说的:
<br />To determine the required buffer size, call RasEnumConnections with lprasconn set to NULL. The <div>……本2文来源gaodai.ma#com搞##代!^码@网3</div><code>搞代gaodaima码</code>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.<br />
MSDN上提供的示例代码也是这么写的:
<br />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) {
// ……
}
可惜这个例子是错误的,至少在XP SP3上是错误的,RasEnumConnections函数返回的是ERROR_INVALID_SIZE(632),而不是ERROR_BUFFER_TOO_SMALL。解决的方法是第一个参数不要传入NULL,而是传入一个正确设置了dwSize的RASCONN结构,一般dwSize设为sizeof(RASCONN)即可;但是如果你的程序要在早期的系统上运行,则需要HARD CODE成目标系统的值。