🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
6 messages
6 total messages Started by DeKayA Sun, 29 Oct 2006 02:30
console ping example
#1795
Author: DeKayA
Date: Sun, 29 Oct 2006 02:30
82 lines
3546 bytes
I don't know if this is the correct place for this but since it uses
sockets and ports I made a guess.

This is reputed to be a PING.c   example.
it came from 
http://www.vijaymukhi.com/vmis/wsockexp.htm


I downloaded it directly from the web and attempted to create a
console app.  I think it is quite old code.

When I compile it it gives  me several errors but all  are so similar
that I think the resolution for one will apply to many.

So here goes.

The error messages are 
indicated by a -> at the begriming of the line
No doubt very basic for experienced programmers but...

//------------------------------------------------------------------
[34] E2034 Cannot convert 'void*" to HINSTANCE__*'
[34] E2343 Type mismatch in parameter "hModule" in call to '__stdcall
GetProcAddress(HINSTANCE__*,const char *]"
[34] E2034 Cannot convert *int(__stdcall*)()' to void * (__stdcall *
)()'

//------------------------------------------------------------------


#include <windows.h>
 struct o
 {
  unsigned char Ttl,Tos,Flags,OptionsSize,*OptionsData;
 };
 struct
 {
  DWORD Address;
  unsigned long  Status,RoundTripTime;
  unsigned short DataSize,Reserved;
  void *Data;
  struct o  Options;
 } E;

 HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent 
    *phostent;
 DWORD d;char aa[100];struct o I;

 HANDLE ( WINAPI *pIcmpCreateFile )( VOID );
 BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );
 DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,LPVOID,
                                                 LPVOID,DWORD,DWORD);


int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrev,
                                         LPSTR lpCmd,int nShow )
  {
    hIcmp = LoadLibrary( "ICMP.DLL" );
    WSAStartup( 0x0101, &wsa );
    phostent = gethostbyname( "www.microsoft.com");
    dwIPAddr = (DWORD *)( *phostent->h_addr_list );

->    pIcmpCreateFile=GetProcAddress( hIcmp,"IcmpCreateFile");
->    pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");
->    pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );

    hIP = pIcmpCreateFile();
    I.Ttl=6;
    pIcmpSendEcho(hIP,*dwIPAddr,0,0,&I,&E,sizeof(E),8000 );
    d=E.Address;
    phostent = gethostbyaddr((char *)&d,4,PF_INET);
    sprintf(aa,"gethostbyaddr %p",phostent );
    MessageBox(0,aa,aa,0);
    if ( phostent  != 0 ) MessageBox(0,phostent->h_name,"hi",0);
    wsprintf(aa,"RTT: %dms,TTL:%d", E.RoundTripTime, E.Options.Ttl );
    MessageBox(0,aa,"hi",0);
    pIcmpCloseHandle( hIP );
    FreeLibrary( hIcmp );
    WSACleanup();
  }

//-----------------------------------------------------------------
Re: console ping example
#1796
Author: Michel Leunen
Date: Sun, 29 Oct 2006 10:25
53 lines
2657 bytes
DeKayA@bellsouth.net wrote:


> #include <windows.h>

#include <stdio.h> 	// for sprintf

>  HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent 
>     *phostent;

Replace the HANDLE hIcmp;
par :
HINSTANCE hIcmp;
LoadLibrary() returns a HINSTANCE not a HANDLE.


>  HANDLE ( WINAPI *pIcmpCreateFile )( VOID );
>  BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );
>  DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,LPVOID,
>                                                  LPVOID,DWORD,DWORD);

Use typedefs when you have to deal with function pointers. It helps a lot:

typedef HANDLE ( WINAPI *ICMPCREATEFILE )( VOID );
typedef BOOL ( WINAPI *ICMPCLOSEHANDLE )( HANDLE );
typedef DWORD (WINAPI *ICMPSENDECHO)(HANDLE,DWORD,LPVOID,WORD,LPVOID,
                                        LPVOID,DWORD,DWORD);


> ->    pIcmpCreateFile=GetProcAddress( hIcmp,"IcmpCreateFile");
> ->    pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");
> ->    pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );

You just need to cast the return value of GetProcAddress() to the 
correct function pointer. It's here your typedefs help you :

ICMPCREATEFILE pIcmpCreateFile=(ICMPCREATEFILE)GetProcAddress( 
hIcmp,"IcmpCreateFile");
        ICMPCLOSEHANDLE 
pIcmpCloseHandle=(ICMPCLOSEHANDLE)GetProcAddress( hIcmp,"IcmpCloseHandle");
        ICMPSENDECHO pIcmpSendEcho =(ICMPSENDECHO)GetProcAddress( 
hIcmp,"IcmpSendEcho" );


HTH
Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: console ping example
#1798
Author: DeKayA
Date: Sun, 29 Oct 2006 13:30
26 lines
1545 bytes


>Replace the HANDLE hIcmp;
>par :
>HINSTANCE hIcmp;
>LoadLibrary() returns a HINSTANCE not a HANDLE.

It's truest like christmas
Thanks so much for your time.

I made the changes you recommended and looked up the functions to try
to understand the corrections.  

It got rid of all the errors but one linker error.
I can't tell you where the error is, because it doesn't tell me in a
way I can understand.

The error is [Linker Error]Unresolved external'_main' referenced from
C:\BCB4\LIB\C0X32.OBJ

I found C0X32.OBJ in bcb4\lib 
but where and how do I reference it in the code?

Thanks
You have been a tremendous help.
Re: console ping example
#1799
Author: DeKayA
Date: Sun, 29 Oct 2006 15:35
37 lines
2159 bytes
On Sun, 29 Oct 2006 13:30:30 GMT, DeKayA@bellsouth.net wrote:

>The error is [Linker Error]Unresolved external'_main' referenced from
>C:\BCB4\LIB\C0X32.OBJ

Somewhere on the web someone else was dealing with something kind of
similar.  a users suggested,"Add an empty main function to MustWork.c"
void main( int i, char** c )
{
} 

Well I tried that and it compiled, but not at all what I expected.
It was just jumping right to the empty main and compiling that.  

That gave me an idea. I threw out the empty main and renamed WinMain
to main.  Voila;  It worked.


Perhaps I started the project wrong in BCB4.
I selected New\Consoile Wizard. Is that wrong for a plain console App?

I would like to provide it with an IP address or computer name on my
local net instead of the "WWW.Microsoft.com"

I notice it return an address but it is not in standard format
i.e.  for microsoft it was 001531d8.

a standard ping returns [207.46.130.108]
while tracert to www.microsoft.com brings this up 
tracing route to lb1.www.ms.akadns.net [207.46.198.30]  


I know this program uses the function 
  phostent = gethostbyaddr((char *)&d,4,PF_INET);
But how would you convert  192.168.1.3 to ?

anyway  Thanks a lot
Re: console ping example
#1801
Author: Michel Leunen
Date: Sun, 29 Oct 2006 16:40
17 lines
1587 bytes
DeKayA@bellsouth.net wrote:

> The error is [Linker Error]Unresolved external'_main' referenced from
> C:\BCB4\LIB\C0X32.OBJ

It means you're trying to build a console application but the linker 
doesn't find any code for the main() function. IOW, since the code has a 
WinMain(), you've to build a windows application even if there is no form.

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Re: console ping example
#1800
Author: Michel Leunen
Date: Sun, 29 Oct 2006 16:50
32 lines
2121 bytes
DeKayA@bellsouth.net wrote:

> Perhaps I started the project wrong in BCB4.
> I selected New\Consoile Wizard. Is that wrong for a plain console App?

Yes, see my other post. You must create a windows application instead.
Your other choice is to replace the WinMain() by a main() function and 
the create a console application.

> 
> I would like to provide it with an IP address or computer name on my
> local net instead of the "WWW.Microsoft.com"

You can pass arguments to a Windows application as well.


> I know this program uses the function 
>   phostent = gethostbyaddr((char *)&d,4,PF_INET);
> But how would you convert  192.168.1.3 to ?

I don't have the winsock help file at hand but look at the HOSTENT 
structure members. I think there is a member giving you the dotted 
address IIRC.

Michel
-- 
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads