/* name of DLL */

static char* ADVAPI_DLL = "ADVAPI32.DLL";

 

/*======================================================================

GetNovellUserId performs the sequence of NetWare 3.x calls to determine

the Id of the logged-in user.

Returns :

NW_SUCCESS : all functions successful, User ID in pUserId

NetWare Error code : pUserId contains the value OLT_ERR_USERID

======================================================================*/

static int GetNovellUserId(LPSTR pUserId)

{

WORD wConnHandle;

WORD wObjectType;

WORD wConnNumber;

DWORD dwObjectId;

BYTE byLoginTime[NOVELL_LOGINTIME_SIZE];

int iResult;

 

iResult = (*lpfnNWGetPrimaryConnectionID)(&wConnHandle);

if (iResult == NW_SUCCESS)

{

iResult = (*lpfnNWGetConnectionNumber)(wConnHandle, &wConnNumber);

if (iResult == NW_SUCCESS)

{

iResult = (*lpfnNWGetConnectionInformation)(

wConnHandle,

wConnNumber,

pUserId,

&wObjectType,

&dwObjectId,

byLoginTime);

}

}

return iResult;

} /* GetNovellUserId */

 

/*======================================================================

WOW type declarations for thunking functions from wownt16

======================================================================*/

typedef DWORD (FAR PASCAL* PFNLOADLIBRARYEX32W) (

LPCSTR lpszLibFile,

DWORD hFile,

DWORD dwFlags);

typedef DWORD (FAR PASCAL* PFNGETPROCADDRESS32W) (

DWORD hModule,

LPCSTR lpszProc);

typedef DWORD (FAR PASCAL* PFNFREELIBRARY32W) (

DWORD hLibModule);

typedef DWORD (FAR* PFNCALLPROCEX32W) (

DWORD, DWORD, DWORD, ... );

 

/*======================================================================

DoesThunking determines whether the generic thunking calls are available

in the KERNEL DLL. Otherwise we cannot call from 16 to 32

bits.

======================================================================*/

static BOOL DoesThunking(

PFNLOADLIBRARYEX32W* ppLoadLibraryEx32W, /* output */

PFNGETPROCADDRESS32W* ppGetProcAddress32W, /* output */

PFNCALLPROCEX32W* ppCallProcEx32W, /* output */

PFNFREELIBRARY32W* ppFreeLibrary32W /* output */

)

{

HMODULE hmodKernel;

hmodKernel = GetModuleHandle("KERNEL");

*ppLoadLibraryEx32W = (PFNLOADLIBRARYEX32W)GetProcAddress(

hmodKernel,

"LoadLibraryEx32W");

*ppGetProcAddress32W = (PFNGETPROCADDRESS32W) GetProcAddress(

hmodKernel,

"GetProcAddress32W");

*ppCallProcEx32W = (PFNCALLPROCEX32W) GetProcAddress(

hmodKernel,

"_CallProcEx32W");

*ppFreeLibrary32W = (PFNFREELIBRARY32W) GetProcAddress(

hmodKernel,

"FreeLibrary32W");

return ((*ppLoadLibraryEx32W != NULL)

&& (*ppCallProcEx32W != NULL)

&& (*ppGetProcAddress32W != NULL)

&& (*ppFreeLibrary32W != NULL));

} /* DoesThunking */

 

/*======================================================================

GetWin32UserId fills the buffer with the active Win32 userid if the

user was logged in. It returns a userid of OLT_UNKNOWN_USERID

if the Win32 environment is not available on the PC.

If an error occurred when consulting the bindery then

the return code OLT_ERROR is returned and the userid

is set to OLT_ERROR_USERID. Otherwise OLT_OK is returned.

======================================================================*/

static int GetWin32UserId(

PFNLOADLIBRARYEX32W pLoadLibraryEx32W, /* input */

PFNGETPROCADDRESS32W pGetProcAddress32W, /* input */

PFNCALLPROCEX32W pCallProcEx32W, /* input */

PFNFREELIBRARY32W pFreeLibrary32W, /* input */

LPSTR pUserId /* output */

)

{

int iResult;

UINT uOldErrorMode;

DWORD hinstAdvApi;

DWORD lpGetUserName;

DWORD dwSize;

 

/* does the library exist ? */

uOldErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX);

hinstAdvApi = (*pLoadLibraryEx32W)(ADVAPI_DLL,0,0);

SetErrorMode(uOldErrorMode);

if (hinstAdvApi != 0)

{

iResult = OLT_ERROR;

strcpy(pUserId,OLT_ERR_USERID);

/* does it have the functions ? */

lpGetUserName = (*pGetProcAddress32W)(hinstAdvApi,"GetUserNameA");

if (lpGetUserName != 0)

{

dwSize = NW_MAX_USER_NAME_LEN;

if ((*pCallProcEx32W)(2,3,lpGetUserName,pUserId,&dwSize) != 0)

iResult = OLT_OK;

}

/* free the library that you loaded! */

(*pFreeLibrary32W)(hinstAdvApi);

}

else

{

iResult = OLT_OK;

strcpy(pUserId,OLT_ANO_USERID);

}

return iResult;

} /* GetWin32UserId */

 

/*======================================================================

OLT_GetUserId

======================================================================*/

int OLT_GetUserId(

LPSTR pUserId /* output */

)

{

int iResult;

PFNLOADLIBRARYEX32W pLoadLibraryEx32W;

PFNGETPROCADDRESS32W pGetProcAddress32W;

PFNCALLPROCEX32W pCallProcEx32W;

PFNFREELIBRARY32W pFreeLibrary32W;

 

iResult = OLT_ERROR;

if (DoesThunking(&pLoadLibraryEx32W,&pGetProcAddress32W,&pCallProcEx32W,&pFreeLibrary32W))

iResult = GetWin32UserId(pLoadLibraryEx32W,pGetProcAddress32W,pCallProcEx32W,pFreeLibrary32W,

pUserId);

else

iResult = GetNovellUserId(pUserId);

return iResult;

} /* OLT_GetUserId */