停止"DOS BOX"从开! [英] Stop "DOS BOX" from opening!

查看:111
本文介绍了停止"DOS BOX"从开!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行此代码,我只需要将值作为变量返回.它是一个控制台应用程序.我需要停止打开"DOS BOX".我该怎么办?

I''m running this code and I need to return the value as a variable only. Its a console app. I need to stop the "DOS BOX" from opening. How would I do this?

//CODE BLOCK
#pragma warning( disable: 4996 )

#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma comment( lib, "ws2_32.lib" ) // linker must use this lib for sockets

// *** Prototypes
void translate_ip(DWORD _ip, char *_cip);
void decode_tcp(char *_packet);
void decode_icmp(char *_packet);
void get_this_machine_ip(char *_retIP);

// *** Defines and Typedefs

#define LS_HI_PART(x)  ((x>>4) & 0x0F)
#define LS_LO_PART(x)  ((x) & 0x0F)

#define LS_MAX_PACKET_SIZE 65535

#ifndef SIO_RCVALL
#  define SIO_RCVALL    _WSAIOW(IOC_VENDOR,1)
#endif

typedef struct _IP_HEADER_
{
   BYTE  ver_ihl;        // Version (4 bits) and Internet Header Length (4 bits)
   BYTE  type;           // Type of Service (8 bits)
   WORD  length;         // Total size of packet (header + data)(16 bits)
   WORD  packet_id;      // (16 bits)
   WORD  flags_foff;     // Flags (3 bits) and Fragment Offset (13 bits)
   BYTE  time_to_live;   // (8 bits)
   BYTE  protocol;       // (8 bits)
   WORD  hdr_chksum;     // Header check sum (16 bits)
   DWORD source_ip;      // Source Address (32 bits)
   DWORD destination_ip; // Destination Address (32 bits)
} IPHEADER;

typedef struct _TCP_HEADER_
{
   WORD  source_port;       // (16 bits)
   WORD  destination_port;  // (16 bits)
   DWORD seq_number;        // Sequence Number (32 bits)
   DWORD ack_number;        // Acknowledgment Number (32 bits)
   WORD  info_ctrl;         // Data Offset (4 bits), Reserved (6 bits), Control bits (6 bits)
   WORD  window;            // (16 bits)
   WORD  checksum;          // (16 bits)
   WORD  urgent_pointer;    // (16 bits)
} TCPHEADER;

typedef struct _ICMP_HEADER_
{
   BYTE type;               // (8 bits)  
   BYTE code;               // (8 bits)  
   WORD checksum;           // (16 bits)  
} ICMPHEADER;

// *********************************************************************
//                               MAIN
// *********************************************************************
int main( int _argc, char *_argv[] )
{
	struct   sockaddr_in sock_sniff;
	SOCKET   sniff_socket = -1;
   WSAData  sa_data;  
   WORD     ver;
   IPHEADER *ip_header = NULL;
   int      optval = 1;
   DWORD    dwLen = 0;
   char     packet[LS_MAX_PACKET_SIZE];
   int      iRet = 0;
   int      ip_header_size = 0;
   char     ipSrc[20], ipDest[20], thisIP[20];
   BOOL     bShowTCP = TRUE, bShowICMP = TRUE;
 
   // Check arguments
   if ( _argc > 1 )
   {
      if ( !_stricmp(_argv[1], "icmp") )
         bShowTCP = FALSE;
      else if ( !_stricmp(_argv[1], "tcp") )
         bShowICMP = FALSE;
      else
      {
         exit(0);
      }
   }

   // Init Windows sockets version 2.2   
   ver = MAKEWORD(2,2);
   WSAStartup(ver, &sa_data);

   // Get a socket in RAW mode
	sniff_socket = socket( AF_INET, SOCK_RAW, IPPROTO_IP );
	if ( sniff_socket == SOCKET_ERROR )
	{
      exit(-1);
	}

   // Bind it
   memset( thisIP, 0x00, sizeof(thisIP) );
   get_this_machine_ip(thisIP);

	sock_sniff.sin_family = AF_INET;
	sock_sniff.sin_port = htons(0);
   // If your machine has more than one IP you might put another one instead thisIP value
	sock_sniff.sin_addr.s_addr = inet_addr(thisIP);
	
	if ( bind( sniff_socket, (struct sockaddr *)&sock_sniff, sizeof(sock_sniff) ) == SOCKET_ERROR )
	{
      exit(-2);
	}

   // Set socket to promiscuous mode
   if ( WSAIoctl( sniff_socket,
                  SIO_RCVALL,
                  &optval,
                  sizeof(optval),
                  NULL,
                  0,
                  &dwLen,
                  NULL,
                  NULL ) == SOCKET_ERROR )

	{
      exit(-3);
	}

   while ( TRUE )
   {
      (void) memset( packet, 0x00, sizeof(packet) );

      iRet = recv( sniff_socket, packet, LS_MAX_PACKET_SIZE, 0 );
      if ( iRet < sizeof(IPHEADER) )
         continue;

      ip_header = (IPHEADER *)packet;

      // I only want IPv4 not IPv6
      if ( LS_HI_PART(ip_header->ver_ihl) != 4 ) 
         continue;

      ip_header_size = LS_LO_PART(ip_header->ver_ihl);
      ip_header_size *= sizeof(DWORD); // size in 32 bits words

      // Checks the protocol IP is encapsulating
      memset( ipSrc, 0x00, sizeof(ipSrc) );
      memset( ipDest, 0x00, sizeof(ipDest) );
      translate_ip(ip_header->source_ip, ipSrc);
      translate_ip(ip_header->destination_ip, ipDest);

      // Read http://www.ietf.org/rfc/rfc1700.txt?number=1700
      switch( ip_header->protocol )
      {
         case 1: // ICMP
            if ( bShowICMP )
            {
               char szIP[256];
				sprintf("\n   Source      IP: %s", ipSrc);
				MessageBox(NULL, szIP, "", MB_OK);
            }
			 break;

         case 6: // TCP
            if ( bShowTCP )
            {
               char szIP[256];
				sprintf("\n   Source      IP: %s", ipSrc);
				MessageBox(NULL, szIP, "", MB_OK);
            }
            break;

         case 17: // UPD
			 
            break;

         default:
            break;
      }
   
   } // end-while

   return 0;
}

void get_this_machine_ip(char *_retIP)
{
   char host_name[128];
   struct hostent *hs;
   struct in_addr in;

   memset( host_name, 0x00, sizeof(host_name) );
   gethostname(host_name,128);
   hs = gethostbyname(host_name);

   memcpy( &in, hs->h_addr, hs->h_length );
   strcpy( _retIP, inet_ntoa(in) );
}

void translate_ip(DWORD _ip, char *_cip)
{
   struct in_addr in;

	in.S_un.S_addr = _ip;
	strcpy( _cip, inet_ntoa(in) );
}

void decode_tcp(char *_packet)
{
   TCPHEADER *tcp_header = (TCPHEADER *)_packet;
   BYTE flags = ( ntohs(tcp_header->info_ctrl) & 0x003F ); 

   >destination_port));
  
}

void decode_icmp(char *_packet)
{
   ICMPHEADER *icmp_header = (ICMPHEADER *)_packet;

   switch ( icmp_header->type )
   {
      case 0:
         break;

      case 8:
         break;

      default:
         break;
   }

 }

推荐答案

似乎您正在创建控制台模式应用程序.如果不需要控制台,则需要创建Windows模式应用程序.我认为对于初学者来说,最好的方法是创建一个新的VC ++项目并使用Win32项目向导(而不是Win32控制台应用程序向导).代替main,您将看到一个WinMain .而且您没有UI,也不需要消息循环.您的代码是旧的自上而下的代码.所以这将为您工作.

或者,您可以修改现有的编译器设置以使用/SUBSYSTEM:WINDOWS,手动使用#include <windows.h>,然后将main 更改为WinMain.那也可以,但是由于您是C ++的新手,所以这可能会给您带来一些难度,尤其是在第一次使用时.

顺便说一句,这些天大多数人只是说控制台或命令shell/提示符.说 Dos Box 不太准确.
It seems you are creating a console mode application. If you do not need the console, then you need to create a windows mode application. I reckon the best way to do this for a beginner is to create a new VC++ project and use the Win32 project wizard (and not the Win32 console application wizard). Instead of main, you''ll see a WinMain there. And you don''t have an UI and you don''t need a message loop. You code is old style top-down code. So this will work for you.

Alternatively, you can modify your existing compiler settings to use /SUBSYSTEM:WINDOWS, manually #include <windows.h> and then change main to WinMain. That will work too, but since you are new to C++, this may be a little harder for you to do, specially the first time.

And btw, most people just say console or command shell/prompt these days. Saying Dos Box is not very accurate.


您可以尝试将其用作将 Dos Box 控制台应用程序转换为Windows的快捷方式应用程序: QuickWin-将控制台应用程序转换为Windows程序 [
You could try this as a shortcut to turning your Dos Box console app into a Windows app: QuickWin - Turn a console application into a Windows program[^]


没有"DOS盒子"了!

只有使用控制台或将控制台与Windows UI结合使用的应用程序.例如,"CMD.EXE"仅是一种这样的应用程序,即普通的Win32或64位应用程序,具体取决于Windows版本.

—SA
There is no such thing as "DOS box" anymore!

There is just an application using console or combining console with Windows UI. For example, "CMD.EXE" is just one of such applications, normal Win32 or 64-bit one, depending on Windows version.

—SA


这篇关于停止"DOS BOX"从开!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆