Avatar of newpro2010
newpro2010
 asked on

Passing arguments to main function in C++


Hello,
I have my own application and I need to insert this code in a saparate class and call it from my own main function.
The problem that I am facing is when I change this main function into another and try to call it from my main function I get a bunch of error messages, mainly becuase of the argument passing (I think)

Any suggestions?
#include "stdafx.h"
#include "pcap.h"

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

int main(int argc, char **argv)
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_dumper_t *dumpfile;

struct pcap_pkthdr *header;
const u_char *pkt_data;
int res;
pcap_t *fp;
    
    /* Check command line */
    if(argc != 2)
    {
        printf("usage: %s filename", argv[0]);
        return -1;
    }
    
    /* Retrieve the device list on the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }
    
    printf("Enter the interface number (1-%d):",i);
    scanf_s("%d", &inum);
    
    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);

		return -1;
    }
        
    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
    
    /* Open the device */
    if ( (adhandle= pcap_open(d->name,          // name of the device
                              65536,            // portion of the packet to capture
                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
                              1000,             // read timeout
                              NULL,             // authentication on the remote machine
                              errbuf            // error buffer
                              ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* Open the dump file */
    dumpfile = pcap_dump_open(adhandle, argv[1] ); //adhandle, argv[1]

    if(dumpfile==NULL)
    {
        fprintf(stderr,"\nError opening output file\n");
        return -1;
    }
    
    printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
    
    /* At this point, we no longer need the device list. Free it */
    pcap_freealldevs(alldevs);
    
    /* start the capture */
    pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);

    /* while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
    {
	pcap_dump((unsigned char *) dumpfile, header, pkt_data);
	 }
	*/
    return 0;
}


// Callback function invoked by libpcap for every incoming packet 
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    // save the packet on the dump file 
    pcap_dump(dumpfile, header, pkt_data);
}

Open in new window

C++CVisual C++.NET

Avatar of undefined
Last Comment
sarabande

8/22/2022 - Mon
AndyAinscow

Showing the errors would be useful.

do you mean something like this?

#include "stdafx.h"
#include "pcap.h"

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

//prototype of the new function
int foo(int argc, char **argv);


int main(int argc, char **argv)
{
  return foo(argc, argv);
}

int foo()
{
....  your code from main goes here

Open in new window

newpro2010

ASKER
Hello AndyAniscow,

Thanks for the fast reply.. Here is the complete code:

Every time I change the arguments or the call I get a different error message. but mainly ((function can not take 0 arguments))

I've started to learn C++ recently :)

Thanks in advance
(((CreatingDumpFile.cpp)))

#include "stdafx.h"
#include "pcap.h"
#include "CreatingDumpFile.h"

using namespace std;

// prototype of the packet handler
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int CreatDump (int argc, char **argv);

        CreatingDumpFile::CreatingDumpFile()
	{
	}

	CreatingDumpFile::~CreatingDumpFile()
	{
	} 

	int CreatingDumpFile::UsingCreatDump()
	{
        CreatDump();
        return 0;
	}

	int argc;
	char **argv;

int CreatDump () /*this was the main method, I'm trying to call it from UsingCreatDump, and then call the later one from my main method in Final.cpp */
	{
	//int argc;
	//char **argv;
 pcap_if_t *alldevs;
 pcap_if_t *d;
 int inum;
 int i=0;
 pcap_t *adhandle;
 char errbuf[PCAP_ERRBUF_SIZE];
 pcap_dumper_t *dumpfile;

 struct pcap_pkthdr *header;
 const u_char *pkt_data;
 int res;
 pcap_t *fp;
    
    /* Check command line */
    if(argc != 2)
    {
        printf("usage: %s filename", argv[0]);
        return -1;
    }
    
    /* Retrieve the device list on the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }
    
    printf("Enter the interface number (1-%d):",i);
    scanf_s("%d", &inum);
    
    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);

		return -1;
    }
        
    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
    
    /* Open the device */
    if ( (adhandle= pcap_open(d->name,          // name of the device
                              65536,            // portion of the packet to capture
                                                // 65536 guarantees that the whole packet will be captured on all the link layers
                              PCAP_OPENFLAG_PROMISCUOUS,    // promiscuous mode
                              1000,             // read timeout
                              NULL,             // authentication on the remote machine
                              errbuf            // error buffer
                              ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* Open the dump file */
    dumpfile = pcap_dump_open(adhandle, argv[1] ); //adhandle, argv[1]

    if(dumpfile==NULL)
    {
        fprintf(stderr,"\nError opening output file\n");
        return -1;
    }
    
    printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
    
    /* At this point, we no longer need the device list. Free it */
    pcap_freealldevs(alldevs);
    
    /* start the capture */
    pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);

    /* while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
    {
	pcap_dump((unsigned char *) dumpfile, header, pkt_data);
	 }
	*/
    return 0;
}


// Callback function invoked by libpcap for every incoming packet 
void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    // save the packet on the dump file 
    pcap_dump(dumpfile, header, pkt_data);
} 

..............................................................................................

(((CreatingDumpFile.h)))

#include "stdafx.h"
#include <iostream>
#ifndef CREATINGDUMPFILE_H
#define CREATINGDUMPFILE_H

class CreatingDumpFile {
public:
	CreatingDumpFile(); //Constructor
	~CreatingDumpFile();//destructor
	int UsingCreatDump();

};

#endif 

.................................................................................................

(((Final.cpp)))

#include "CreatingDumpFile.h"

.............
........
....
.. //switch function, then in case 5:

	case 5:
		Dumping = new CreatingDumpFile();
		Dumping->UsingCreatDump();
		//delete Dumping;

Open in new window

ozo

int CreatDump (int argc, char **argv);

int CreatDump ()


your prototype and declaration are inconsistent.
decide which way you want to do it.



This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
AndyAinscow

CreatingDumpFile::CreatingDumpFile()

You use that sort of coding when you are writing the function CreatingDumpFile that is a member of a class CreatingDumpFile.  (This is specifically a constructor when the function anme is the same as a class name).
newpro2010

ASKER
I went with the second one (no arguments), but the application crashes :)

Access violation reading location 0x000000000.
when I set a breakpoint I can see that the application stops here:
  printf("usage: %s filename", argv[0]);
ozo

Where was argv declared, and how was argv[0] set?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
AndyAinscow

I think you first need to sit down with pencil and paper and decide what needs to be the logic of your code.  Then you can start writing the crude outline of what each function should do and what it needs to be able to accomplish that task.
newpro2010

ASKER
I went to project properties>>Configuration Properties >> Debugging
I have set the Command arguments to "save" .. which is a name to the dump file that will be created
newpro2010

ASKER
The code that I have posted already works just fine.. I have tested it with the debugging settings that I have mentioned above.. but the problems comes when I try to make it as a class and call it from my main function
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
AndyAinscow

lines 27 and 28 - you declare variables global to the file, then attempt to use them without actually giving them any values first hence the crash.

As I said before - you do not have the logic sorted out of what you want to do.  That is the foundation you write the code on and must be sorted out first.
ASKER CERTIFIED SOLUTION
sarabande

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sarabande

i don't know where you got that old c code, but i don't think it makes sense to make a c++ class wrapper around it. you don't get any c++ advantage by that and still have all c disadvantages like unitialized variables, pointer management, ancient runtime api, and more.  

Sara