开发者

Strange compiler message when using GetProcAddress() in windows.h

开发者 https://www.devze.com 2023-03-30 17:27 出处:网络
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 funct

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?


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消