VC ++ MFC activex 控件获取连接的VPN 信息

时间:2022-01-08 21:25:21

vc++  MFC 进行activex  控件的开发步骤就不用多写了,只是简单的说明一下方法,以及具体的代码:

使用的类库是 windows 系统的 rasapi32.dll

记住需要添加的头文件如下:

#include <Windows.h>
#include <Ras.h>
#include <RasError.h>
#pragma comment(lib, "rasapi32.lib")

代码如下:

VARIANT_BOOL CsecurityControlCtrl::VPNApp(void)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    // TODO: 在此添加调度处理程序代码

    DWORD dwCb = ;
    DWORD dwRet = ERROR_SUCCESS;
    DWORD dwConnections = ;
    LPRASCONN lpRasConn = NULL;
    // Call RasEnumConnections with lpRasConn = NULL. dwCb is returned with the required buffer size and
    // a return code of ERROR_BUFFER_TOO_SMALL
    dwRet = RasEnumConnectionsW(lpRasConn, &dwCb, &dwConnections);
    if (dwRet == ERROR_BUFFER_TOO_SMALL){
        // Allocate the memory needed for the array of RAS structure(s).
        lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
        if (lpRasConn == NULL){

            return false;
        }
        // The first RASCONN structure in the array must contain the RASCONN structure size
        lpRasConn[].dwSize = sizeof(RASCONN);

        // Call RasEnumConnections to enumerate active connections
        dwRet = RasEnumConnectionsW(lpRasConn, &dwCb, &dwConnections);

        // If successful, print the names of the active connections.
        if (ERROR_SUCCESS == dwRet){

            )
            {
               return true;
            }
            else
            {
            return false;
            }
        }
        //Deallocate memory for the connection buffer
        HeapFree(GetProcessHeap(), , lpRasConn);
        lpRasConn = NULL;

    }
    return false;

}