首页
社区
课程
招聘
[旧帖] [求助]安装SPI 基础服务后的一个BUG 0.00雪花
发表于: 2012-3-19 18:00 1294

[旧帖] [求助]安装SPI 基础服务后的一个BUG 0.00雪花

2012-3-19 18:00
1294
安装SPI 基础服务后导致socket在2个进程中无法互用。就是在A进程中创建的SOCKET.通过WSADuplicateSocket 获取的WSAPROTOCOL_INFOW信息发送到进程B中。进程B使用WSAPROTOCOL_INFOW 创建套接字会返回失败。错误码是10022.参数无效。不装SPI 基础服务这代码是没问题的。  求解。下面附上2个进程的代码。

//////////////////////////////////////进程A//////////////////////////////////////

int _tmain(int argc, _TCHAR* argv[])
{

  WSADATA wsaData;
  WSAStartup(0x202, &wsaData);

 
  SOCKET s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,NULL,0,WSA_FLAG_OVERLAPPED);
  if (s == INVALID_SOCKET)
  {
    printf("WSASocket 失败:%d.\r\n",WSAGetLastError());
    goto end;
  }

  printf("WSASocket 成功.Socket = %d.\r\n",s);
  sockaddr_in service ={0};
  service.sin_family = AF_INET;
  service.sin_addr.s_addr = INADDR_ANY;//inet_addr("192.168.2.36");
  service.sin_port = htons(2000);

  //----------------------
  // Bind the socket.
  int iResult = bind(s, (SOCKADDR *) &service, sizeof (service));
  if (iResult == SOCKET_ERROR) {
     
    printf("bind 失败:%d.\r\n",WSAGetLastError());
    closesocket(s);
    WSACleanup();
    goto end;
  }
  else
    printf("bind success.\r\n");

    if (SOCKET_ERROR  == listen(s,5))
    {
    printf("bind 失败:%d.\r\n",WSAGetLastError());
    closesocket(s);
    WSACleanup();
    goto end;
    }
  else
    printf("listen success.\r\n");

  TCHAR path[MAX_PATH] =_T("C:\\TestWSADuplicateSocket.exe");

  STARTUPINFO       StartupInfo ={0};        
     StartupInfo.cb = sizeof(STARTUPINFO);
  StartupInfo.lpReserved=NULL;       
  StartupInfo.lpDesktop=NULL;       
  StartupInfo.lpTitle=NULL;       
  StartupInfo.dwX=0;       
  StartupInfo.dwY=0;       
  StartupInfo.dwXSize=0;       
  StartupInfo.dwYSize=0;       
  StartupInfo.dwXCountChars=500;       
  StartupInfo.dwYCountChars=500;       
  StartupInfo.dwFlags=STARTF_USESHOWWINDOW;       
  StartupInfo.wShowWindow=SW_SHOWNORMAL;       
   
  StartupInfo.cbReserved2=0;       
  StartupInfo.lpReserved2=NULL;       
  StartupInfo.hStdInput=stdin;       
  StartupInfo.hStdOutput=stdout;       
  StartupInfo.hStdError=stderr;

 

  PROCESS_INFORMATION processInfo={0};

  if(!CreateProcess(NULL,path,FALSE,NULL,FALSE,CREATE_NEW_CONSOLE ,NULL,NULL,&StartupInfo,&processInfo))
  {
    printf("CreateProcess 失败:%d.\r\n",GetLastError());
    goto end;
  }
  else
    printf("CreateProcess success.\r\n");

  WSAPROTOCOL_INFOW ProtocolInfo= {0};
   iResult = WSADuplicateSocket(s,processInfo.dwProcessId,&ProtocolInfo);
  if(iResult != 0)
    printf("WSPDuplicateSocket Error :%d", iResult);
  else
    printf("WSPDuplicateSocket success.\r\n");

  

  CPolicyMgrShare::GetInstance()->Init();
  CPolicyMgrShare::GetInstance()->WirteBuf((char*)&ProtocolInfo,sizeof(WSAPROTOCOL_INFOW));

 

  printf("wait..\r\n");

  WaitForSingleObject(processInfo.hProcess,INFINITE); 

  CloseHandle(processInfo.hProcess);

  printf("Create process exit..\r\n");
 
end:

  getchar();

  return 0;
}

//////////////////////////////////////进程B////////////////////////////////////
 int _tmain(int argc, _TCHAR* argv[])
{
  
 
  MessageBox(NULL,_T("aaa"),_T("bbb"),MB_OK);

  WSADATA wsaData;
  if (WSAStartup(0x202, &wsaData) != 0)
  {
    printf("WSAStartup 失败:%d.\r\n",WSAGetLastError());
    goto end;
  }
   

  
  WSAPROTOCOL_INFO LocalProtocolInfo;

  CPolicyMgrShare::GetInstance()->Init();
  CPolicyMgrShare::GetInstance()->ReadBuf((char*)&LocalProtocolInfo,sizeof(WSAPROTOCOL_INFO));

  SOCKET s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,&LocalProtocolInfo,0,WSA_FLAG_OVERLAPPED);

  

  //SOCKET s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,NULL,0,WSA_FLAG_OVERLAPPED);
  if (s == INVALID_SOCKET)
  {
    printf("WSASocket 失败:%d.\r\n",WSAGetLastError());
    goto end;
  }
  else
    printf("WSASocket success socket = %d.\r\n",s);

  while (1)
  {
  
    sockaddr_in   local ={0};
    int nLen = sizeof(sockaddr_in);
    SOCKET saccess = accept(s,(sockaddr*)&local,&nLen);
    if (saccess == INVALID_SOCKET)
    {
      printf("accept INVALID_SOCKET.\r\n");
      Sleep(1000);
    }
    else
    {
      printf("accept success socket = %d.\r\n",saccess);
    }
  }
  
end:
  WSACleanup( );

  getchar();

  return 0;
}

这个BUG 我搞了1天没任何头绪。希望谁能给点提示!

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 28
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
求解啊!!!
2012-3-19 18:44
0
雪    币: 377
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我和你一样,都不是高手,学习http://blog.csdn.net/qq276592716/article/details/6760283
发现子进程需要停下来等待父进程运行指令,这样或许合理些
2012-3-20 10:10
0
雪    币: 28
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
呵呵,测试程序!主要是测试这个问题的。
2012-3-20 10:43
0
游客
登录 | 注册 方可回帖
返回
//