Board index » delphi » NETAPI.DLL-Username .C to Pascal Help

NETAPI.DLL-Username .C to Pascal Help

I am trying to get the username for the logged on user on a Lan Manager
network.  I have some .C code that does this and found the following
Pascal code in SWAG that should do exactly what I need, but as the
message states, there is a problem with the code and it gives a GPF in
NETAPI.DLL.  If anyone can help to fix this code, or help me retrieve the
current username, I would very much appreciate it.

Tom Kellen
------------------ Broken Code ----------------------------------------
(*
From: RO...@plato.ucsalf.ac.uk (Robin Bowes)

I'm trying to call a function in a Windows .dll from
Turbo Pascal for Windows v1.5.

The .dll in question is NETAPI.DLL.  The function I want to call is
defined as follows (in C format):

(from Microsoft LAN Manager Programmer's Reference, )

NetWkstaGetInfo ( const char far *  pszServer,
       short      sLevel,
       char far *    pbBuffer,
       unsigned short   cbBuffer,
       unsigned short far * pcbTotalAvail
      );

where

pszServer
 contains the name of the server on which to execute NetWkstGetInfo.

sLevel
 specfies the level of detail to be supplied in the return buffer

pbBuffer
 points to the buffer in which data is returned

cbBuffer
 specifies the size of the buffer pointed to by pbBuffer

pcbTotalAvail
 points to an unsigned integer in which the number of bytes of
 information available is returned.

The detail level I require is 10 which means that the buffer returned
will contain a wksta_info_10 structure which is defined as follows:

struct wksta_info_10 {
 char far *  wki10_computername;
 char far *  wki10_username;
 char far *  wki10_langroup;
 unsigned char wki10_ver_major;
 unsigned char wki10_ver_minor;
 char far *  wki10_logon_domain;
 char far *  wki10_oth_domains;

Quote
};

I am having trouble getting this function to work.  It will be a .dll
eventually but for now I'm jsut coding it as a program using WinCrt.

My code so far looks something like this:
*)
program Username;

uses WinTypes, WinCrt;

const
 NERR_BufTooSmall = 2123;
  NERR_Success   = 0;

type
 Wksta_info_10 =
  record
   wki10_computername : pChar;
   wki10_username   : pChar;
   wki10_langroup   : pChar;
   wki10_ver_major   : Byte;
   wki10_ver_minor   : Byte;
   wki10_logon_domain : pChar;
   wki10_oth_domains  : pChar;
  end;
 pWksta_info_10 = ^Wksta_info_10;

function NetWkstaGetInfo( pszServer     : pChar;
             sLevel      : Integer;
             var pbBuffer   : pWksta_info_10;
             cbBuffer     : Word;
             var pcbTotalAvail : pWord
            ) : Integer; far; external 'NETAPI';

function getUsername(var Username : pChar) : Integer;
var
 pWI        : pWksta_info_10;
 sWorkStationInfo : Word;
 pbBufLen     : pWord;
 pbTotalAvail   : pWord;
 uRetCode     : Integer;

begin
 {first call will fail but should return the size of the
 buffer needed to hold all the available data}
 getMem(pbBufLen, sizeOf(pbBufLen));
  pwI := nil;
 uRetCode := NetWkstaGetInfo(nil,   {Servername (nil -> local machine)}
               10,    {Reporting level}    
               pWI,   {target buffer for info}
               0,    {Size of target buffer}
               pbBufLen {Count of bytes available}
               );
 {check the return code from the function}
 if (uRetCode = NERR_BufTooSmall) then
  { check available memory }
  begin
  if maxAvail < pbBufLen^ then
   begin
   getUsername := -1;
      Exit
   end
    else
   {allocate memory for buffer to hold information}
   begin
   getMem(pWI, pbBufLen^)
   end
  end
 else
   {Unexpected error returned}
  begin
    {Pass return code back to calling program}
  getUsername := uRetCode;
  Exit
  end;

 {second call to get information}
 getMem(pbTotalAvail, sizeOf(pbTotalAvail));
 uRetCode := NetWkstaGetInfo(nil, 10, pWI,  pbBufLen^, pbTotalAvail);
 getUsername := uRetCode;
 if uRetCode = NERR_Success then
   begin
  Username := pWI^.wki10_username;
  end;
 freeMem(pbBufLen, sizeOf(pbBufLen));
 freeMem(pbTotalAvail, sizeOf(pbTotalAvail))
end;

{exports
 getUsername  index 1;}

var
 retVal : Integer;
 uName : pChar;

begin
getMem(uName, sizeOf(uName));
retVal := getUserName(uName);
if retVal = NERR_Success then
 writeln(uName)
else
 writeln('Error returned: ', retVal);
freeMem(uName, sizeOf(uName));
end.
{

This compiles OK but throws a GPF in NETAPI.DLL.

I'm fairly sure it's the conversion of the structure type that's causing
the problem.

Has anybody got any ideas ?

Quote
}

------------------------------ End Broken Code --------------------------
-
 

Re:NETAPI.DLL-Username .C to Pascal Help


Tom Kellen <tkel...@cednet.com> wrote in article
<MPG.cfe827a3e256212989...@news.mindspring.com>...

Quote
> NetWkstaGetInfo ( const char far *  pszServer,
>        short      sLevel,
>        char far *    pbBuffer,
>        unsigned short   cbBuffer,
>        unsigned short far * pcbTotalAvail
>       );
> function NetWkstaGetInfo( pszServer     : pChar;
>              sLevel      : Integer;
>              var pbBuffer   : pWksta_info_10;
>              cbBuffer     : Word;
>              var pcbTotalAvail : pWord       <<<<<< Here
>             ) : Integer; far; external 'NETAPI';

I haven't compiled this, and there may be other problems. However, I
believe that pcbTotalAvail should be a Word rather than a pWord, since
you have declared it as a var-parameter.

Chris.

Re:NETAPI.DLL-Username .C to Pascal Help


Tom,

As your interested in getting the userName of the Local machine, There is a
far simpler function you can use if you care to..

function WNetGetUser(lpszUserName: PChar;BufferSize: PWord): Word; far; external 'USER' index 516;

Var
  dummy:Word;
  Aname:Array[0..80] of char;
  bufferSize:Word;
  pBufferSize:Pword;

Begin
Buffersize:=80;
pBufferSize:=@bufferSize;
dummy:=WNetGetUser(Aname,pbuffersize);
messagebox(0,aName,'Network UserName',mb_OK);
End.

Quote
Tom Kellen wrote:

> I am trying to get the username for the logged on user on a Lan Manager
> network.  I have some .C code that does this and found the following
> Pascal code in SWAG that should do exactly what I need, but as the
> message states, there is a problem with the code and it gives a GPF in
> NETAPI.DLL.  If anyone can help to fix this code, or help me retrieve the
> current username, I would very much appreciate it.

> Tom Kellen
> ------------------ Broken Code ----------------------------------------
> (*
> From: RO...@plato.ucsalf.ac.uk (Robin Bowes)

> I'm trying to call a function in a Windows .dll from
> Turbo Pascal for Windows v1.5.

> The .dll in question is NETAPI.DLL.  The function I want to call is
> defined as follows (in C format):

> (from Microsoft LAN Manager Programmer's Reference, )

> NetWkstaGetInfo ( const char far *  pszServer,
>        short      sLevel,
>        char far *    pbBuffer,
>        unsigned short   cbBuffer,
>        unsigned short far * pcbTotalAvail
>       );

> where

> pszServer
>  contains the name of the server on which to execute NetWkstGetInfo.

> sLevel
>  specfies the level of detail to be supplied in the return buffer

> pbBuffer
>  points to the buffer in which data is returned

> cbBuffer
>  specifies the size of the buffer pointed to by pbBuffer

> pcbTotalAvail
>  points to an unsigned integer in which the number of bytes of
>  information available is returned.

> The detail level I require is 10 which means that the buffer returned
> will contain a wksta_info_10 structure which is defined as follows:

> struct wksta_info_10 {
>  char far *  wki10_computername;
>  char far *  wki10_username;
>  char far *  wki10_langroup;
>  unsigned char wki10_ver_major;
>  unsigned char wki10_ver_minor;
>  char far *  wki10_logon_domain;
>  char far *  wki10_oth_domains;
> };

> I am having trouble getting this function to work.  It will be a .dll
> eventually but for now I'm jsut coding it as a program using WinCrt.

> My code so far looks something like this:
> *)
> program Username;

> uses WinTypes, WinCrt;

> const
>  NERR_BufTooSmall = 2123;
>   NERR_Success   = 0;

> type
>  Wksta_info_10 =
>   record
>    wki10_computername : pChar;
>    wki10_username   : pChar;
>    wki10_langroup   : pChar;
>    wki10_ver_major   : Byte;
>    wki10_ver_minor   : Byte;
>    wki10_logon_domain : pChar;
>    wki10_oth_domains  : pChar;
>   end;
>  pWksta_info_10 = ^Wksta_info_10;

> function NetWkstaGetInfo( pszServer     : pChar;
>              sLevel      : Integer;
>              var pbBuffer   : pWksta_info_10;
>              cbBuffer     : Word;
>              var pcbTotalAvail : pWord
>             ) : Integer; far; external 'NETAPI';

> function getUsername(var Username : pChar) : Integer;
> var
>  pWI        : pWksta_info_10;
>  sWorkStationInfo : Word;
>  pbBufLen     : pWord;
>  pbTotalAvail   : pWord;
>  uRetCode     : Integer;

> begin
>  {first call will fail but should return the size of the
>  buffer needed to hold all the available data}
>  getMem(pbBufLen, sizeOf(pbBufLen));
>   pwI := nil;
>  uRetCode := NetWkstaGetInfo(nil,   {Servername (nil -> local machine)}
>                10,    {Reporting level}
>                pWI,   {target buffer for info}
>                0,    {Size of target buffer}
>                pbBufLen {Count of bytes available}
>                );
>  {check the return code from the function}
>  if (uRetCode = NERR_BufTooSmall) then
>   { check available memory }
>   begin
>   if maxAvail < pbBufLen^ then
>    begin
>    getUsername := -1;
>       Exit
>    end
>     else
>    {allocate memory for buffer to hold information}
>    begin
>    getMem(pWI, pbBufLen^)
>    end
>   end
>  else
>    {Unexpected error returned}
>   begin
>     {Pass return code back to calling program}
>   getUsername := uRetCode;
>   Exit
>   end;

>  {second call to get information}
>  getMem(pbTotalAvail, sizeOf(pbTotalAvail));
>  uRetCode := NetWkstaGetInfo(nil, 10, pWI,  pbBufLen^, pbTotalAvail);
>  getUsername := uRetCode;
>  if uRetCode = NERR_Success then
>    begin
>   Username := pWI^.wki10_username;
>   end;
>  freeMem(pbBufLen, sizeOf(pbBufLen));
>  freeMem(pbTotalAvail, sizeOf(pbTotalAvail))
> end;

> {exports
>  getUsername  index 1;}

> var
>  retVal : Integer;
>  uName : pChar;

> begin
> getMem(uName, sizeOf(uName));
> retVal := getUserName(uName);
> if retVal = NERR_Success then
>  writeln(uName)
> else
>  writeln('Error returned: ', retVal);
> freeMem(uName, sizeOf(uName));
> end.
> {

> This compiles OK but throws a GPF in NETAPI.DLL.

> I'm fairly sure it's the conversion of the structure type that's causing
> the problem.

> Has anybody got any ideas ?

> }

> ------------------------------ End Broken Code --------------------------
> -

Other Threads