我可以通过 Windows GUI RasDial 界面以及它的 CLI 等效 ( c:\windows\rasdial.exe
) 完美连接到我的 VPN。但是,当尝试在 C 中自动化它时,RasDial
返回 633 ERROR_PORT_NOT_AVAILABLE
:(来源)
这不是我特有的。我已经在四台不同的计算机上对其进行了测试,每台计算机都在单独的互联网连接上。
这是我的源代码:
#include <windows.h>
#include <winerror.h>
#include <Ras.h>
#include <raserror.h>
#include <wchar.h>
#include <stdio.h>
int EnumConnections();
int DialOut();
int HangUp();
int Debug();
int main()
{
printf("Dial out return code: %d\n", DialOut());
printf("Debug status: %i\n", Debug());
EnumConnections();
HangUp();
return 0;
}
int EnumConnections()
{
DWORD dwCb = 0;
DWORD dwRet = ERROR_SUCCESS;
DWORD dwConnections = 0;
LPRASCONN lpRasConn = NULL;
if (dwRet == ERROR_BUFFER_TOO_SMALL)
{
lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
if (lpRasConn == NULL){
wprintf(L"HeapAlloc failed!\n");
return 0;
}
lpRasConn[0].dwSize = sizeof(RASCONN);
dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);
if (ERROR_SUCCESS == dwRet){
wprintf(L"The following RAS connections are currently active:\n");
DWORD i;
for (i = 0; i < dwConnections; i++){
wprintf(L"%s\n", lpRasConn[i].szEntryName);
}
}
HeapFree(GetProcessHeap(), 0, lpRasConn);
lpRasConn = NULL;
}
if(dwConnections >= 1)
wprintf(L"The operation failed to acquire the buffer size.\n");
else
wprintf(L"There are no active RAS connections.\n");
return 0;
}
int DialOut()
{
LPCTSTR pbkLoc = "C:\\rasphone.pbk\0";
char* szPhoneNumberToDial = "127.0.0.1";
char* szUserName = "test\0";
char* szPassword = "test\0";
RASDIALPARAMS rdParams;
rdParams.dwSize = sizeof(RASDIALPARAMS);
rdParams.szEntryName[0] = '\0';
lstrcpy(rdParams.szPhoneNumber, szPhoneNumberToDial);
rdParams.szCallbackNumber[0] = '\0';
lstrcpy( rdParams.szUserName, szUserName );
lstrcpy( rdParams.szPassword, szPassword );
rdParams.szDomain[0] = '\0';
HRASCONN hRasConn = NULL;
return RasDial(NULL, pbkLoc, &rdParams, 0L, NULL, &hRasConn);
}
int HangUp()
{
printf("Hung up\n");
HRASCONN hRasConn = NULL;
return RasHangUp(hRasConn);
}
int Debug()
{
RASCONNSTATUS RasConnStatus;
HRASCONN hRasConn = NULL;
RasConnStatus.dwSize = sizeof(RasConnStatus);
return RasGetConnectStatus(hRasConn,&RasConnStatus);
}
有任何想法吗?我真的被困在这里了。我阅读了所有 RasDial 文档。我仍然不知道从哪里开始。