> Hi All.
> I tested libpq for win32 coming with 6.4-BETA and have a 
> question about
> LIBPQ.
> 
> I used LIBPQ from a C program,but coundn't connect PostgreSQL DB.
> 
> Calling pg_connect() without WSAStartup() failed because of 
> gethostbyname()
> error in LIBPQ.
> so I modified DllMain in LIBPQ as follows.
> 
> BOOL WINAPI DllMain( HINSTANCE hinstDLL,   DWORD fdwReason,
>                      LPVOID lpReserved ){
> 
> 	WSADATA wsaData;
> 	switch (fdwReason)
> 		   {
> 	 case DLL_PROCESS_ATTACH:
> 		  WSAStartup( WSAStartup(MAKEWORD(1, 1), &wsaData);
> 		  break;
> 	 case DLL_PROCESS_DETACH:
> 		  WSACleanup();
> 		  break;
> 		   }
> 
> 	return (TRUE);
> }
> 
> and the connection to DB was OK.
> 
> Why WSAStartup() is  not called in LIBPQ?

This is probably a good thing to do :-)
When I wrote the code, I thought that I was only allowed to
a) Call WSAStartup() once or
b) All calls to WSAStartup() had to have the same version number
But after reading the specs a little more careful, I see that this is not
the case.

The specs say:
An application or DLL can call WSAStartup more than once if it needs to
obtain the WSAData structure information more than once. On each such call
the application can specify any version number supported by the DLL.

The question is - what will happen when you call it with different versions.
Say the application asks for Winsock 2.0, and then the Libpq library goes in
and asks for Winsock 1.1. Will the application still have access to Winsock
2.0 functions?
With the current implementation, I don't beleive it will make a difference -
I don't think that any part of the Winsock system is actually hidden if you
ask for a lower version. But it is _permitted_ by the specification that the
DLL can hide parts that belong to a higher version than the one requested.

So I'm not 100% sure... Does anybody have access to a Winsock that actually
hides some details when you ask for the wrong version?


//Magnus

Mon, 28 Sep 1998 10:30:01 +0200
Magnus Hagander <mha@edu.sollentuna.se>
RE: [HACKERS] LIBPQ for WIN32
> > Hi All.
> > I tested libpq for win32 coming with 6.4-BETA and have a
> > question about
> > LIBPQ.
> >
> > I used LIBPQ from a C program,but coundn't connect PostgreSQL DB.
> >
> > Calling pg_connect() without WSAStartup() failed because of
> > gethostbyname()
> > error in LIBPQ.
> > so I modified DllMain in LIBPQ as follows.
> >
> > BOOL WINAPI DllMain( HINSTANCE hinstDLL,   DWORD fdwReason,
> >                      LPVOID lpReserved ){
> >
> > 	WSADATA wsaData;
> > 	switch (fdwReason)
> > 		   {
> > 	 case DLL_PROCESS_ATTACH:
> > 		  WSAStartup( WSAStartup(MAKEWORD(1, 1), &wsaData);
> > 		  break;
> > 	 case DLL_PROCESS_DETACH:
> > 		  WSACleanup();
> > 		  break;
> > 		   }
> >
> > 	return (TRUE);
> > }
> >
> > and the connection to DB was OK.
> >
> > Why WSAStartup() is  not called in LIBPQ?
>
> This is probably a good thing to do :-)
> When I wrote the code, I thought that I was only allowed to
> a) Call WSAStartup() once or
> b) All calls to WSAStartup() had to have the same version number
> But after reading the specs a little more careful, I see that this is not
> the case.
>
> The specs say:
> An application or DLL can call WSAStartup more than once if it needs to
> obtain the WSAData structure information more than once. On each such call
> the application can specify any version number supported by the DLL.
>
> The question is - what will happen when you call it with different
versions.
> Say the application asks for Winsock 2.0, and then the Libpq library goes
in
> and asks for Winsock 1.1. Will the application still have access to
Winsock
> 2.0 functions?

I think that current DLL supports version 1.0,1.1,2.0,2.1 and 2.2 and we can
specify any version from 1.0 to 2.2 .
For example,if we call WSAStartup(MAKEWORD(1.0),&wsaData) from libpq
and call WSAStartup(MAKEWORD(2,2),&wsaData) from psql,both return OK
and wsaData.wVersion is 1.0 for libpq and 2.2 for psql.

> With the current implementation, I don't beleive it will make a
difference -
> I don't think that any part of the Winsock system is actually hidden if
you
> ask for a lower version. But it is _permitted_ by the specification that
the
> DLL can hide parts that belong to a higher version than the one requested.
>
> So I'm not 100% sure... Does anybody have access to a Winsock　that
actually
> hides some details when you ask for the wrong version?
>

By the specs there may be the DLL that doesn't support lower versions.
In that case my code doesn't work well.
But we can delay to call WSAStartup() after the first socket call in LIBPQ.
My example code in fe-connect.c is such as follows.

	hp = gethostbyname(conn->pghost);
#ifdef	WIN32
	if ((hp == NULL) && (GetLastError() == WSANOTINITIALISED))
	{
		WSADATA	wsaData;
		if (WSAStartup(MAKEWORD(1,1),&wsaData))
		{
			fprintf(stderr, "Failed to start winsock: %i\n", WSAGetLastError());
			exit(1);
		}
		....
		???? for WSACleanup() ????
		....
		hp = gethostbyname(conn->host);
	}
#endif
	if ((hp == NULL) || (hp->h_addrtype != AF_INET))
	{
		(void) sprintf(conn->errorMessage,
					   "connectDB() --  unknown hostname: %s\n",
					   conn->pghost);
		goto connect_errReturn;
	}

Hiroshi Inoue
inoue@tpf.co.jp


Tue, 29 Sep 1998 08:58:44 +0900
"Hiroshi Inoue" <Inoue@tpf.co.jp>
RE: [HACKERS] LIBPQ for WIN32

On 28-Sep-98 Hiroshi Inoue wrote:
> I think that current DLL supports version 1.0,1.1,2.0,2.1 and 2.2 and we
> can
> specify any version from 1.0 to 2.2 .
> For example,if we call WSAStartup(MAKEWORD(1.0),&wsaData) from libpq
> and call WSAStartup(MAKEWORD(2,2),&wsaData) from psql,both return OK
> and wsaData.wVersion is 1.0 for libpq and 2.2 for psql.

Whichever is done, try not to ever request a version higher than what is
required.  It forces upgrades that may not even be necessary.  It's been
awhile since I've done windows code, but I've been bitten by requiring a
higher version of a library than necessary - more than once without even
realising it.  It's not very fun when it comes time to fix it.

Vince.
-- 
==========================================================================
Vince Vielhaber -- KA8CSH   email: vev@michvhf.com   flame-mail: /dev/null
       # include <std/disclaimers.h>                   TEAM-OS2
   Online Searchable Campground Listings    http://www.camping-usa.com
       "There is no outfit less entitled to lecture me about bloat
               than the federal government"  -- Tony Snow
==========================================================================



Mon, 28 Sep 1998 20:28:05 -0400 (EDT)
Vince Vielhaber <vev@michvhf.com>
RE: [HACKERS] LIBPQ for WIN32
>
> On 28-Sep-98 Hiroshi Inoue wrote:
> > I think that current DLL supports version 1.0,1.1,2.0,2.1 and 2.2 and we
> > can
> > specify any version from 1.0 to 2.2 .
> > For example,if we call WSAStartup(MAKEWORD(1.0),&wsaData) from libpq
> > and call WSAStartup(MAKEWORD(2,2),&wsaData) from psql,both return OK
> > and wsaData.wVersion is 1.0 for libpq and 2.2 for psql.
>
> Whichever is done, try not to ever request a version higher than what is
> required.  It forces upgrades that may not even be necessary.  It's been
> awhile since I've done windows code, but I've been bitten by requiring a
> higher version of a library than necessary - more than once without even
> realising it.  It's not very fun when it comes time to fix it.
>

How about my sample code at the end of my previous post.
In that code,libpq does nothing if main application calls WSAStartup().

Hiroshi Inoue
inoue@tpf.co.jp


Tue, 29 Sep 1998 10:46:34 +0900
"Hiroshi Inoue" <Inoue@tpf.co.jp>
RE: [HACKERS] LIBPQ for WIN32
On Tue, 29 Sep 1998, Hiroshi Inoue wrote:

> >
> > On 28-Sep-98 Hiroshi Inoue wrote:
> > > I think that current DLL supports version 1.0,1.1,2.0,2.1 and 2.2 and we
> > > can
> > > specify any version from 1.0 to 2.2 .
> > > For example,if we call WSAStartup(MAKEWORD(1.0),&wsaData) from libpq
> > > and call WSAStartup(MAKEWORD(2,2),&wsaData) from psql,both return OK
> > > and wsaData.wVersion is 1.0 for libpq and 2.2 for psql.
> >
> > Whichever is done, try not to ever request a version higher than what is
> > required.  It forces upgrades that may not even be necessary.  It's been
> > awhile since I've done windows code, but I've been bitten by requiring a
> > higher version of a library than necessary - more than once without even
> > realising it.  It's not very fun when it comes time to fix it.
> >
> 
> How about my sample code at the end of my previous post.
> In that code,libpq does nothing if main application calls WSAStartup().

According to the winsock 1.1 spec an intermediate dll should handle it's
own startup and cleanup functions.  Thinking about it, you really have 
no way of knowing if the app is going to stop using and unload winsock
if you're not done with it.  It may have loaded it for any number of 
reasons not necessarily related to the database access.

Vince.
-- 
==========================================================================
Vince Vielhaber -- KA8CSH   email: vev@michvhf.com   flame-mail: /dev/null
       # include <std/disclaimers.h>                   TEAM-OS2
   Online Searchable Campground Listings    http://www.camping-usa.com
       "There is no outfit less entitled to lecture me about bloat
               than the federal government"  -- Tony Snow
==========================================================================




Tue, 29 Sep 1998 06:09:06 -0400 (EDT)
Vince Vielhaber <vev@michvhf.com>
RE: [HACKERS] LIBPQ for WIN32