ICMP winsock

Programming, for all ages and all languages.
Post Reply
fstream

ICMP winsock

Post by fstream »

ive recently decided to learn more about sockets (more specifically non-blocking a.k.a. asynch) and came across the sourcecode for a program that simply pings. I downloaded the code, compiled it, and got this error
C:\Documents and Settings\drewski\Desktop\37tech\sockets\ping\ping.cpp(36) : error C2664: 'GetProcAddress' : cannot convert parameter 1 from 'void *' to 'struct HINSTANCE__ *'

(plus 2 more times) and each time it has to do with this function

pIcmpCreateFile=GetProcAddress(hIcmp,"IcmpCreateFile");

the block of code that seems to be troubling it is this

hIcmp = LoadLibrary( "ICMP.DLL" );
         
WSAStartup( 0x0002, &wsa );

phostent = gethostbyname( "www.microsoft.com");
dwIPAddr = (DWORD *)( *phostent->h_addr_list );
         
pIcmpCreateFile=GetProcAddress(hIcmp,"IcmpCreateFile");
pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");
pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );

so each time i use hIcmp, it does not work. ICMP.dll is included in my project, as well as ICMP.dll. hIcmp is declared as a handle. any ideas why it isnt working?
fstream

Re:ICMP winsock

Post by fstream »

i meant i have both ICMP.dll and ICMP.lib in my project, as well as all the winsock files i need.
ark

Re:ICMP winsock

Post by ark »

you mean hIcmp is declared like this:

HANDLE hIcmp;

??

The first parameter to GetProcAddress is an HMODULE, which is probably interchangeable with the generic type HANDLE. Try a reinterpret_cast.

pIcmpCreateFile = GetProcAddress(reinterpret_cast<HMODULE>(hIcmp), "IcmpCreateFile");
ark

Re:ICMP winsock

Post by ark »

in other words, it sounds like the problem is that the first parameter to GetProcAddress is not a void* and that hIcmp is declared as a void* (i.e., typedef void* HANDLE or something to that effect)...and in C++ a cast from a void pointer to another type of pointer requires an explicit cast.
ark

Re:ICMP winsock

Post by ark »

'course if there's no reason why you need hIcmp to be a generic handle, then simply declare it as an HMODULE, then you don't need the cast.

Incidentally, I also found an article on MSDN that says you must cast a FARPROC function pointer to a more specific type of function pointer if the symbol STRICT is defined, otherwise the compiler will "warn you about an error in function parameter lists". GetProcAddress returns a FARPROC.
fstream

Re:ICMP winsock

Post by fstream »

i have tried everything you guys said, and it still gives me many errors. i did pIcmpCreateFile = GetProcAddress(reinterpret_cast<HMODULE>(hIcmp), "IcmpCreateFile"); and i get ping.cpp(36) : error C2440: '=' : cannot convert from 'int (__stdcall *)(void)' to 'void *(__stdcall *)(void)', when I declare hIcmp as a MODULE i get ping.cpp(36) : error C2440: '=' : cannot convert from 'int (__stdcall *)(void)' to 'void *(__stdcall *)(void)', pretty much the same. so obviously i need to convert int(__stdcall*) to void*(stdcall). any ideas?
ark

Re:ICMP winsock

Post by ark »

see my comments about the explicit cast of the FARPROC return value.

int (__stdcall*)(void) is a function pointer.

Same thing with void*(__stdcall*)(void)

what is the type of pIcmpCreateFile? try casting the return value of GetProcAddress to that type
Tim

Re:ICMP winsock

Post by Tim »

The easy way of doing this is to get a recent version of the Platform SDK from Microsoft's site and #include <icmpapi.h>. Then you'll get all the Icmp functions defined without the need for LoadLibrary/GetProcAddress.

Otherwise: hIcmp should be of type HINSTANCE or HMODULE. They're not interchangable with HANDLE (HANDLE is a handle to a kernel object; HINSTANCE is a handle to a DLL or EXE). Then you'll need to declare all the Icmp functions as appropriate, e.g.:

Code: Select all

typedef HANDLE (WINAPI *ICMPCREATEFILE)(void);
typedef DWORD (WINAPI *ICMPSENDECHO)(HANDLE, IPAddr, LPWORD, LPVOID, PIP_POPTION_INFORMATION, LPVOID, DWORD, DWORD);

ICMPCREATEFILE IcmpCreateFile;
ICMPSENDECHO IcmpSendEcho;
// ...
IcmpCreateFile = (ICMPCREATEFILE) GetProcAddress(hIcmp, "IcmpCreateFile");
etc.

But it would be easier to get a new copy of the Platform SDK. In fact, if you have icmp.lib, it seems likely that you have icmpapi.h.
fstream

Re:ICMP winsock

Post by fstream »

sorry it took so long for me to reply, but i did what u said, tom, and it works now. thanx a bunch for your help. :) i really apprectiate it, you too joel
Post Reply