windows ce gprs 拨号程序(转)

#include "stdafx.h" 
#include "ras.h" 

HANDLE hPort = INVALID_HANDLE_VALUE; 
HRASCONN hRasConn = NULL; 
BOOL CreatePort() 
{ 
    DWORD dwNumBytesWritten; 
    BOOL fRet; 
    BYTE Bytes[]={'A','T','+','C','G','D','C','O','N','T','=','1',',','"','I','P','"',',',
                                     '"','C','M','N','E','T','"',0x0D,'A','T','D','T','*','9','9','#',0x0D}; 
    //open the serial port 
    hPort = CreateFile(TEXT("COM8:"),GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); 


    if ( hPort == INVALID_HANDLE_VALUE ) 
    { 
        // Could not open the port. 
        //printf("Open the serial port failed\n\r"); 
        return FALSE; 
    } 
    //BYTE Bytes[]={'A','T',0x0D}; 
    //DWORD CgdcontSize = sizeof(Cgdcont); 
    DWORD BytesSize = sizeof(Bytes); 
    //fRet = WriteFile(hPort,Cgdcont,CgdcontSize, &dwNumBytesWritten,NULL); 
    fRet = WriteFile(hPort,Bytes,BytesSize, &dwNumBytesWritten,NULL); 
    fRet = ReadFile(hPort,Bytes,4,&dwNumBytesWritten,NULL); 
    if(fRet) 
    { 
        //printf("open the serial port successful \n\r"); 
        CloseHandle(hPort); 
    } 
    else 
    { 
        //close the serial port 
        CloseHandle(hPort); 
        //printf("Close serial port successful\n\r"); 
    } 

    return TRUE; 

} 

LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) 
{ 
    RASCONNSTATE RasState = (RASCONNSTATE)wParam; 
    switch(Msg) 
    { 
    case WM_RASDIALEVENT: 
        switch(RasState) 
        { 
        case RASCS_OpenPort: 
            printf("正在打开端口\n\r"); 
            break; 
        case RASCS_PortOpened: 
            printf("端口已经打开\n\r"); 
            break; 
        case RASCS_ConnectDevice: 
            printf("正在连接设备\n\r"); 
            break; 
        case RASCS_Connected: 
            //int i; 
            //printf("已连接\n\r"); 
            //scanf("%d",i); 
            //if(i==1) 
            //{ 
            // RasHangUp(hRasConn); 
            //} 
            break; 
        case RASCS_Disconnected: 
            printf("连接已断开\n\r"); 
            break; 
        default: 
            printf("未知\n\r"); 
            break; 
        } 
        break; 
    default: 
        return DefWindowProc(hWnd, Msg, wParam, lParam); 
    } 
    return TRUE; 
} 

int WINAPI WinMain( HINSTANCE hInstance, 
                   HINSTANCE hPrevInstance, 
                   LPTSTR    lpCmdLine, 
                   int       nCmdShow) 
{ 
    // TODO: Place code here. 
    HWND hwnd; 
    MSG msg; 
    WNDCLASS wc; 
    wc.style = 0; 
    wc.lpfnWndProc = (WNDPROC)WndProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = 0; 
    wc.hCursor = 0; 
    wc.hbrBackground = NULL; 
    wc.lpszMenuName = 0; 
    wc.lpszClassName = _T("embo"); 

    RegisterClass(&wc); 

    hwnd = CreateWindow(_T("embo"),_T("EmboConnection"),0,0,0,0,0,NULL,NULL,hInstance,NULL); 

    ShowWindow(hwnd,0); 

    CreatePort(); 

    RASDIALPARAMS rdParams;//拨号参数

    //初始化拨号参数
    rdParams.dwSize=sizeof(RASDIALPARAMSW); 
    wcscpy(rdParams.szEntryName,_T("Embo Connection")); 
    wcscpy(rdParams.szPhoneNumber,_T("")); 
    wcscpy(rdParams.szCallbackNumber,_T("")); 
    wcscpy(rdParams.szUserName,_T("")); 
    wcscpy(rdParams.szPassword,_T("")); 
    wcscpy(rdParams.szDomain,_T("")); 
    DWORD dwRet; 
    //拨号连接
    //printf("connecting  \n\r"); 
    dwRet = RasDial(NULL,NULL,&rdParams,0xFFFFFFFF,hwnd ,&hRasConn); 

    if(dwRet != 0)//connection successful 
    { 
        RasHangUp(hRasConn); 
    } 



    while ( GetMessage(&msg, NULL, 0, 0) != FALSE ) 
    { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    } 
    return 0; 
} 
posted @ 2009-03-17 16:46  木瓜脑袋  阅读(3032)  评论(0编辑  收藏  举报