RE: [HACKERS] LIBPQ for WIN32 - Mailing list pgsql-hackers
From | Hiroshi Inoue |
---|---|
Subject | RE: [HACKERS] LIBPQ for WIN32 |
Date | |
Msg-id | 000301bdeb3b$ed9c8e60$2801007e@cadzone.tpf.co.jp Whole thread Raw |
In response to | RE: [HACKERS] LIBPQ for WIN32 (Magnus Hagander <mha@edu.sollentuna.se>) |
Responses |
RE: [HACKERS] LIBPQ for WIN32
|
List | pgsql-hackers |
> > 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
pgsql-hackers by date: