在服务中
CSocket *lp=new CSocket();
if(lp!=NULL)
{   
   lp->Create(2000); 报异常  
}然后我就在 前加了
AfxSocketInit();//这个是成功的
CSocket *lp=new CSocket();
if(lp!=NULL)
{   
   lp->Create(2000); 不报异常了 ,但是代码不往下了  
}

解决方案 »

  1.   

    改用winsock api写吧。
    服务程序中对于窗口是很麻烦的,而不少的MFC中都隐含的各种窗口,消息框的使用。基本上用MFC写服务是不大现实的。
      

  2.   

    我用 GetLastError() 他报 操作已完成  ,这是什么意思
      

  3.   

    对于你的第一个例子,我试了一下,用GetLastError()返回值为10048,查找MSDN知道段口碑系统默认使用了,换一个端口。通过。
    WSAEADDRINUSE 
    (10048) 
    Address already in use. 
    Only one usage of each socket address (protocol/IP address/port) is normally permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that wasn't closed properly, or one that is still in the process of closing. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt(SO_REUSEADDR). Client applications usually need not call bind at all - connect will choose an unused port automatically. When bind is called with a wild-card address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is "committed." This could happen with a call to other function later, including connect, listen, WSAConnect or WSAJoinLeaf. 
      

  4.   

    对于你的第一个例子,我试了一下,用GetLastError()返回值为10048,查找MSDN知道2000端口是系统默认使用了,换一个端口。通过。
      

  5.   

    我知道是怎么回事了, 原来 ,在NT服务程序中 如果用  CSocket 的话,就要用到 和 在NT 服务中显示DIALOG 的做法是一样的 ,都要 DWORD dwThreadId; 
    HWINSTA hwinstaSave; 
    HDESK hdeskSave; 
    HWINSTA hwinstaUser; 
    HDESK hdeskUser; // Ensure connection to service window station and desktop, and 
    // save their handles. GetDesktopWindow(); 
    hwinstaSave = GetProcessWindowStation(); 
    dwThreadId = GetCurrentThreadId(); 
    hdeskSave = GetThreadDesktop(dwThreadId); // Impersonate the client and connect to the User's 
    // window station and desktop. hwinstaUser = OpenWindowStation("WinSta0", FALSE, MAXIMUM_ALLOWED); 
    if(hwinstaUser == NULL){ 
    return ; 

    SetProcessWindowStation(hwinstaUser); 
    hdeskUser = OpenDesktop("Default", 0, FALSE, MAXIMUM_ALLOWED); 
    if(hdeskUser == NULL){ 
    SetProcessWindowStation(hwinstaSave); 
    CloseWindowStation(hwinstaUser); 
    return ; 

    SetThreadDesktop(hdeskUser); dwGuiThreadId = dwThreadId; ///////////////////
    if (!AfxSocketInit  ()) {
    return FALSE;
    }
    try
    {
    CSocket *lp=new CSocket();
    if (lp->Create(2100))
    {
    if(lp->Listen())
    return FALSE;
    }
    else
    {
    delete lp; 
    return FALSE;
    }
    }catch (...) {
    delete lp;
    return FALSE;
    }
    ///////////////////
    dwGuiThreadId = 0; // Restore window station and desktop. SetThreadDesktop(hdeskSave); 
    SetProcessWindowStation(hwinstaSave); 
    CloseDesktop(hdeskUser); 
    CloseWindowStation(hwinstaUser); 
    return TRUE;