Автор: codamateur
Дата сообщения: 01.03.2008 14:42
Sorry i cannot speak russian
//Program to replace rasdial.exe -> disconnect, wait few seconds and reconnect using Windows RASAPI interface to use with an USB modem. By Codamateur Feb 2008
//Don't forget to include RASAPI32.LIB when compiling !
#include <windows.h>
#include <Ras.h>
LRESULT CALLBACK WndProcedure(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
HANDLE hMutex;
DWORD dErr;
HWND hW;
RASDIALPARAMS rdParams;
HRASCONN hRasConn = NULL;
BYTE tab[sizeof(RASCONN)];
RASCONN* lpRasConn = NULL;
DWORD dwCb;
DWORD dwConnections = 0;
/* window class*/
WNDCLASSEX wc={sizeof(WNDCLASSEX),0,WndProcedure,0,0,0,0,0,0,0,"rasclass",0};
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,\
int nCmdShow)
{
MSG Msg;
dErr=0;
hMutex=CreateMutex(NULL,FALSE,"rasusd");
dErr=GetLastError();
if(dErr== ERROR_ALREADY_EXISTS) //We want only one instance of this program running
return 0;
wc.hInstance=hInstance;
rdParams.dwSize = sizeof(RASDIALPARAMS);
lstrcpy(rdParams.szEntryName,"Name of your internet connection here"); //here you need to put your personnal connection data
rdParams.szPhoneNumber[0]=0;
rdParams.szCallbackNumber[0] =0;
lstrcpy( rdParams.szUserName, "your login here"); //same here
lstrcpy( rdParams.szPassword, "your password here" ); //same here
rdParams.szDomain[0] =0;
/*register the window class just created:*/
RegisterClassEx(&wc);
/*create the window:*/
if(CreateWindowEx(0,"rasclass",0,0,0,0,0,0,0,0,hInstance,0))
{
/*message loop*/
while( GetMessage(&Msg,NULL,0,0))
{
DispatchMessage(&Msg);
}
}
/*exit:*/
return 0;
}
VOID WINAPI RasDialFunc( UINT unMsg, RASCONNSTATE rasconnstate,DWORD dwError)
{
PostMessage( hW,
WM_USER,
(WPARAM) rasconnstate,
(LPARAM) dwError );
}
/*callback function*/
LRESULT CALLBACK WndProcedure(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_TIMER:
KillTimer(hW,1);
if(dErr!= ERROR_ALREADY_EXISTS)
RasDial( NULL, NULL, &rdParams, 0,(RASDIALFUNC) RasDialFunc, &hRasConn );
return TRUE;
case WM_USER:
switch(wParam)
{
case RASCS_Connected:
PostQuitMessage(0);
return TRUE;
}
return TRUE;
case WM_CREATE:
hW=hWnd;
lpRasConn=(RASCONN*)tab;
lpRasConn->dwSize = sizeof(RASCONN);
dwCb=sizeof(RASCONN);
if(RasEnumConnections(lpRasConn,&dwCb,&dwConnections)==0)
{
RasHangUp(lpRasConn[0].hrasconn);
SetTimer(hW,1,15000,NULL); //Time (in milliseconds) can be changed
}
return TRUE;
case WM_DESTROY:
PostQuitMessage(0);
if(dErr!= ERROR_ALREADY_EXISTS)
CloseHandle(hMutex);
break;
default:
/*let Windows handle other messages*/
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
return FALSE;
}