Article View: borland.public.cppbuilder.nativeapi
Article #9856Re: Telnet functions
From: Alan Bellingham
Date: Fri, 11 Jun 2004 15:22
Date: Fri, 11 Jun 2004 15:22
74 lines
3462 bytes
3462 bytes
Feico <feicorem0veth1s@gmx.net> wrote:
>I tried to use LoadLibrary(WSOCK32.dll) and GetProcAddress. This failed after including the header
>wsock2.h, because connect, recv and send where not declared as LValues. I found that they already
>had the right value. I don't know how it works, but it is convenient.
Err, if you are using the header, then those names are those of
functions. Do I assume that you're trying something like
recv = GetProcAddress(hLib, "recv"):
because if so, then you will get the errors. As far as the compiler is
concerned, it's been told about the recv function, it knows its the
address of a function, and you can't change it.
_Either_
include the header, call those functions, and link with Ws2_32.lib
_or_
use GetProcAddress using your own pointers to functions
The first is easiest, but it relies on the DLL to exist on the target
machine for your code to even start running. If you can't rely on that
(and occasionally you can't), _then_ you can try the step-by-step
process of using LoadLibrary, GetProcAddress and the rest.
The following code illustrates some points:
#include <winsock2.h>
typedef int (__stdcall *recv_fn)(SOCKET s, char* buf, int len, int f);
int main()
{
recv_fn MyRecv = recv;
HINSTANCE hLib = ...;
MyRecv = reinterpret_cast<recv_fn>(GetProcAddress(hLib, "recv"));
SOCKET s;
char buff[12];
MyRecv(s, buff, 12, 0);
recv(s, buff, 12, 0);
}
The typedef defines a pointer-to-function type. It looks almost
identical to the function definition for recv() itself:
int __stdcall recv(SOCKET s, char* buf, int len, int f);
- but with 'typedef' prepended, brackets placed round the name and its
calling type, and a '*' placed before the name.
The assignment "MyRecv = recv" shouldn't be in your code if you don't
want to link to the winsock library itself - but it's there in the
example to show the assignment of the address of a function to a
function pointer. You'll notice that recv is here _not_ accompanied with
its normal parentheses.
The GetProcAddress() call shows how you would actually initialise the
pointer.
The final two lines show the calls to the normal function, and through
the function pointer.
The code is obviously horribly incomplete.
Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
Technical Details
Message-ID:
<caijc0pispkfqei3334mo865nir2fp4ebe@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<nahhc055dhv20vlcattoa3856kvspftch7@4ax.com> <1jjhc0lvd68k34c6niorecfa9l289bhcqc@4ax.com> <40c8d404$1@newsgroups.borland.com> <v7uic054ldlklv0p07q5p5eqnvvp4q4784@4ax.com> <2rejc0p3qvdg6040ou48gln7gd7c3oio33@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,75989986132d48dd X-Google-Attributes: gid10cc67,public From: Alan Bellingham <alanb@episys.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Telnet functions Date: Fri, 11 Jun 2004 16:22:53 +0100 Reply-To: alanb@episys.com Message-ID: <caijc0pispkfqei3334mo865nir2fp4ebe@4ax.com> References: <nahhc055dhv20vlcattoa3856kvspftch7@4ax.com> <1jjhc0lvd68k34c6niorecfa9l289bhcqc@4ax.com> <40c8d404$1@newsgroups.borland.com> <v7uic054ldlklv0p07q5p5eqnvvp4q4784@4ax.com> <2rejc0p3qvdg6040ou48gln7gd7c3oio33@4ax.com> X-Newsreader: Forte Agent 2.0/32.646 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 193.129.251.130 X-Trace: newsgroups.borland.com 1086967372 193.129.251.130 (11 Jun 2004 08:22:52 -0700) Lines: 74 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail