more-rx-updates-20060504
[openafs.git] / src / WINNT / afsapplib / al_dynlink.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
16 }
17
18 #include <WINNT/afsapplib.h>
19 #include "al_dynlink.h"
20
21
22 /*
23  * LIBRARIES __________________________________________________________________
24  *
25  */
26
27 #define cszLIBRARY_UTIL        TEXT("AfsAdminUtil.dll")
28 #define cszLIBRARY_KAS         TEXT("AfsKasAdmin.dll")
29 #define cszLIBRARY_CLIENT      TEXT("AfsClientAdmin.dll")
30
31
32 /*
33  * ADMIN ______________________________________________________________________
34  *
35  */
36
37 static size_t g_cReqUtilLibrary = 0;
38 static HINSTANCE g_hiUtilLibrary = NULL;
39
40 util_AdminErrorCodeTranslate_t util_AdminErrorCodeTranslateP = NULL;
41
42
43 BOOL OpenUtilLibrary (ULONG *pStatus)
44 {
45    BOOL rc = FALSE;
46    ULONG status = ERROR_DLL_NOT_FOUND;
47
48    if ((++g_cReqUtilLibrary) == 1)
49       {
50       if ( ((g_hiUtilLibrary = LoadLibrary (cszLIBRARY_UTIL)) == NULL) ||
51            ((util_AdminErrorCodeTranslateP = (util_AdminErrorCodeTranslate_t)GetProcAddress (g_hiUtilLibrary, "util_AdminErrorCodeTranslate")) == NULL) )
52          {
53          status = GetLastError();
54          CloseUtilLibrary();
55          }
56       }
57    if (g_hiUtilLibrary)
58       {
59       rc = TRUE;
60       }
61
62    if (pStatus)
63       *pStatus = status;
64    return rc;
65 }
66
67
68 void CloseUtilLibrary (void)
69 {
70    if ((--g_cReqUtilLibrary) == 0)
71       {
72       if (g_hiUtilLibrary)
73          FreeLibrary (g_hiUtilLibrary);
74       g_hiUtilLibrary = NULL;
75       }
76 }
77
78
79 /*
80  * KAS ________________________________________________________________________
81  *
82  */
83
84 static size_t g_cReqKasLibrary = 0;
85 static HINSTANCE g_hiKasLibrary = NULL;
86
87 kas_PrincipalGetBegin_t kas_PrincipalGetBeginP = NULL;
88 kas_PrincipalGetNext_t kas_PrincipalGetNextP = NULL;
89 kas_PrincipalGetDone_t kas_PrincipalGetDoneP = NULL;
90 kas_PrincipalGet_t kas_PrincipalGetP = NULL;
91
92
93 BOOL OpenKasLibrary (ULONG *pStatus)
94 {
95    BOOL rc = FALSE;
96    ULONG status = ERROR_DLL_NOT_FOUND;
97
98    if ((++g_cReqKasLibrary) == 1)
99       {
100       if ( ((g_hiKasLibrary = LoadLibrary (cszLIBRARY_KAS)) == NULL) ||
101            ((kas_PrincipalGetBeginP = (kas_PrincipalGetBegin_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGetBegin")) == NULL) ||
102            ((kas_PrincipalGetNextP = (kas_PrincipalGetNext_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGetNext")) == NULL) ||
103            ((kas_PrincipalGetDoneP = (kas_PrincipalGetDone_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGetDone")) == NULL) ||
104            ((kas_PrincipalGetP = (kas_PrincipalGet_t)GetProcAddress (g_hiKasLibrary, "kas_PrincipalGet")) == NULL) )
105          {
106          status = GetLastError();
107          CloseKasLibrary();
108          }
109       }
110    if (g_hiKasLibrary)
111       {
112       rc = TRUE;
113       }
114
115    if (pStatus)
116       *pStatus = status;
117    return rc;
118 }
119
120
121 void CloseKasLibrary (void)
122 {
123    if ((--g_cReqKasLibrary) == 0)
124       {
125       if (g_hiKasLibrary)
126          FreeLibrary (g_hiKasLibrary);
127       g_hiKasLibrary = NULL;
128       }
129 }
130
131
132 /*
133  * CLIENT _____________________________________________________________________
134  *
135  */
136
137 static size_t g_cReqClientLibrary = 0;
138 static HINSTANCE g_hiClientLibrary = NULL;
139
140 typedef int (ADMINAPI *afsclient_Init_t)(afs_status_p st);
141
142 afsclient_TokenGetExisting_t afsclient_TokenGetExistingP = NULL;
143 afsclient_TokenGetNew_t afsclient_TokenGetNewP = NULL;
144 afsclient_TokenClose_t afsclient_TokenCloseP = NULL;
145 afsclient_TokenQuery_t afsclient_TokenQueryP = NULL;
146 afsclient_CellOpen_t afsclient_CellOpenP = NULL;
147 afsclient_CellClose_t afsclient_CellCloseP = NULL;
148 afsclient_LocalCellGet_t afsclient_LocalCellGetP = NULL;
149 afsclient_Init_t afsclient_InitP = NULL;
150
151
152 BOOL OpenClientLibrary (ULONG *pStatus)
153 {
154    BOOL rc = FALSE;
155    ULONG status = ERROR_DLL_NOT_FOUND;
156
157    if ((++g_cReqClientLibrary) == 1)
158       {
159       if ( ((g_hiClientLibrary = LoadLibrary (cszLIBRARY_CLIENT)) == NULL) ||
160            ((afsclient_TokenGetExistingP = (afsclient_TokenGetExisting_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenGetExisting")) == NULL) ||
161            ((afsclient_TokenGetNewP = (afsclient_TokenGetNew_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenGetNew")) == NULL) ||
162            ((afsclient_TokenCloseP = (afsclient_TokenClose_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenClose")) == NULL) ||
163            ((afsclient_TokenQueryP = (afsclient_TokenQuery_t)GetProcAddress (g_hiClientLibrary, "afsclient_TokenQuery")) == NULL) ||
164            ((afsclient_CellOpenP = (afsclient_CellOpen_t)GetProcAddress (g_hiClientLibrary, "afsclient_CellOpen")) == NULL) ||
165            ((afsclient_CellCloseP = (afsclient_CellClose_t)GetProcAddress (g_hiClientLibrary, "afsclient_CellClose")) == NULL) ||
166            ((afsclient_LocalCellGetP = (afsclient_LocalCellGet_t)GetProcAddress (g_hiClientLibrary, "afsclient_LocalCellGet")) == NULL) ||
167            ((afsclient_InitP = (afsclient_Init_t)GetProcAddress (g_hiClientLibrary, "afsclient_Init")) == NULL) )
168          {
169          status = GetLastError();
170          CloseClientLibrary();
171          }
172       else if (!(*afsclient_InitP)((afs_status_p)&status))
173          {
174          CloseClientLibrary();
175          }
176       }
177    if (g_hiClientLibrary)
178       {
179       rc = TRUE;
180       }
181
182    if (pStatus)
183       *pStatus = status;
184    return rc;
185 }
186
187
188 void CloseClientLibrary (void)
189 {
190    if ((--g_cReqClientLibrary) == 0)
191       {
192       if (g_hiClientLibrary)
193          FreeLibrary (g_hiClientLibrary);
194       g_hiClientLibrary = NULL;
195       }
196 }
197