1

I'm developing a simple packet sender/receiver for some purpose (in Dev c++)..wanna add more features to it. But I'm stuck at a point where I'm getting a strange error "Too many arguments to function"..My code is

#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
 HINSTANCE dllhandle = LoadLibrary("wpcap.dll");
 FARPROC sendpacket = NULL, iface_handle = NULL;
 iface_handle = GetProcAddress(dllhandle, "pcap_open");
 char* iface_name = "\\Device\\NPF_{EADB4C21-B0AF-4EF2-86AB-80A37F399D1C}";
 char *errbuf[256];
 int iface = iface_handle(iface_name, 1000, 1, 500, NULL, errbuf);  // The Error is here
 system("pause");
 return 0;
}

Can anyone tell me where I am going wrong?

In silico
  • 51,091
  • 10
  • 150
  • 143
Rushil Paul
  • 2,010
  • 4
  • 26
  • 39
  • Sorry, I don't know the answer. – Robert Harvey Aug 24 '11 at 17:34
  • @Rushil: Here on Stack Overflow, you need to at least *accept* some amount of answers, especially ones that are helpful. You can do that by clicking on what looks like a checkmark on the left of the answer. – In silico Aug 24 '11 at 21:25
  • That can happen only if there ARE any answers.. :-D – Rushil Paul Aug 26 '11 at 11:16
  • @Rushil: So [these](http://stackoverflow.com/questions/6903619/6903713#6903713) [are](http://stackoverflow.com/questions/6559305/6559312#6559312) [not](http://stackoverflow.com/questions/6557028/6557500#6557500) [answers](http://stackoverflow.com/questions/6371231/6371269#6371269)? I see at least one answer that you said "was exactly what [you] wanted". You should accept the answers you find most helpful. If there aren't any helpful answers, you should try clarifying/adding more details to your question. If you don't accept enough answers, people may be less willing to answer your question. – In silico Aug 26 '11 at 11:55
  • Oh I didn't know you were talking about my previous posts! Sorry about that though.. – Rushil Paul Aug 26 '11 at 14:13

1 Answers1

1

First, refer to the official documentation for pcap_open():

pcap_t* pcap_open  ( const char *  source,  
  int  snaplen,  
  int  flags,  
  int  read_timeout,  
  struct pcap_rmtauth *  auth,  
  char *  errbuf   
 ) 

Then look at the definition of FARPROC in windef.h:

typedef INT_PTR (FAR WINAPI *FARPROC)();

You're attempting to call pcap_open() using a completely wrong function signature. That's why the compiler is complaining that there's too many arguments. If you even manage to make this compile, you're almost certainly going to screw up the stack.

And why are you dynamically loading the WinPcap dll using LoadLibrary()? Why not use the method outlined in the official documentation?

To create an application that uses wpcap.dll with Microsoft Visual C++, follow these steps:

Include the file pcap.h at the beginning of every source file that uses the functions exported by library.

If your program uses Win32 specific functions of WinPcap, remember to include WPCAP among the preprocessor definitions.

If your program uses the remote capture capabilities of WinPcap, add *HAVE_REMOTE* among the preprocessor definitions. Do not include remote-ext.h directly in your source files.

Set the options of the linker to include the wpcap.lib library file specific for your target (x86 or x64). wpcap.lib for x86 can be found in the \lib folder of the WinPcap developer's pack, wpcap.lib for x64 can be found in the \lib\x64 folder.

You're using Dev C++, which probably doesn't have the VC++ compiler. You still need to declare the proper function signature. One possible way is through a typedef:

#include <iostream>
#include <windows.h>

struct pcap_t;
struct pcap_rmtauth;
typedef pcap_t* (*pcap_open_func_ptr)(const char *source,
    int snaplen, int flags, int read_timeout,
    pcap_rmtauth *auth, char *errbuf);

int main(int argc, char *argv[])
{
    HINSTANCE dllhandle = LoadLibrary("wpcap.dll");
    pcap_open_func_ptr iface_handle =
        reinterpret_cast<pcap_open_func_ptr>(
            GetProcAddress(dllhandle, "pcap_open"));
    char *errbuf[256];
    pcap_t* iface = iface_handle(iface_name, 1000, 1, 500, NULL, errbuf);
    // ...
    return 0;
}
In silico
  • 51,091
  • 10
  • 150
  • 143
  • @silico, Thanks, I will try that out. and by the way, I didn't want to use winpcap developers pack for some reason..And I was using Dev C++ because I had made the program in visual C++ (which is workin fine) but when I run it on another computer, it gives an error saying xyz.dll required, even after supplying d developer's pack.. – Rushil Paul Aug 26 '11 at 11:38
  • I tried your way, but its again giving an error: invalid conversion from `int (*)()' to `pcap_t*(*)(const char*, int, int, int, pcap_rmtauth*, char*)' in the line GetProcAddress one.. – Rushil Paul Aug 26 '11 at 11:39
  • 1
    @Rushil: You have to `reinterpret_cast<>` the returned address from `GetProcAddress()` into the proper type. For future reference you can try looking at the documentation such as [this one](http://msdn.microsoft.com/en-us/library/ms683212.aspx) and [this one](http://msdn.microsoft.com/en-us/library/ms686944.aspx) to see how `GetProcAddress()` is used. – In silico Aug 26 '11 at 11:44
  • I'm finding it difficult to follow..can you rewrite that function in the correct way please?.. – Rushil Paul Aug 26 '11 at 13:05