Solved

Getting server name/time

Posted on 1998-06-17
5
611 Views
Last Modified: 2010-04-04
I have function that gets the time from the server using net from the command line. I would like something that works in Windows 95 and NT, in NT it seems one can use NetRemoteTOD/Timeserv in the resource kit, should I implement this if NT is running or should I stick with net?

Anyone, the function used to work, but in porting to 32-bit I have now a problem getting the server name, my code is as follows:

function GetServerName(var serverName: string): boolean;
var
   x: integer;
   fullNetPath, driveLetter: string;
   slstDrives: TStringList;
   returnValue: word;
   i: integer;

   function TrimNetPath: string;
   var
     lengthStringToTrim: word;
     i : integer;
     nextChar, netPathToTrim: string;
   begin
      { the fully qualified network path is in the form
        \\serverName\networkPath (stored in fullNetPath)
        this function returns the server name }
      netPathToTrim := fullNetPath;
      lengthStringToTrim := Length(netPathToTrim);
      i := 3;
      Result := '\\';
      nextChar := Copy(netPathToTrim, i, 1);
      while ((nextChar <> '\') and (i <= lengthStringToTrim)) do begin
         Result := Result + nextChar;
         Inc(i);
         nextChar := Copy(netPathToTrim, i, 1)
      end;
   end;
begin
   { initialize variables }
   x := 256;

   { get DRIVE_REMOTE letters }
   slstDrives := TStringList.Create;
   try
      // other function of mine that returns remote drive letters
      GetDriveLettersForDriveType([DRIVE_REMOTE], slstDrives);
      i := -1;
      if slstDrives.Count > 0 then
         repeat
            inc(i);
            { get fully qualified network path - API call }
            driveLetter := Format('%s:', [Copy(slstDrives.Strings[i], 1, 1)]);
            returnValue := WNetGetconnection(PChar(driveLetter), PChar(fullNetPath), x);
         until ((returnValue = WN_SUCCESS) or (i = slstDrives.Count - 1));
   finally
      slstDrives.Free;
   end;

   Result := (returnValue = WN_SUCCESS);
   if result then
      serverName := TrimNetPath
   else
      serverName := '';
end;

The problem is with WNetGetconnection, the return value I get for say my f drive (driveLetter = f:) is 487, which is none of the errors defined (GetLastError returns the same). Can anyone see what I might be doing wrong? Or perhaps suggest an alternative?

Thanks, Tom.

0
Comment
Question by:tomcorcoran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
vladika earned 50 total points
ID: 1353672
ErrorCode 487 is ERROR_INVALID_ADDRESS.
You must allocate enough space for fullNetPath
Try
X := 0;
WNetGetconnection(PChar(driveLetter), '', x);
SetString(fullNetPath, nil, X);
 returnValue := WNetGetconnection(PChar(driveLetter), PChar(fullNetPath), x);
0
 

Author Comment

by:tomcorcoran
ID: 1353673
Vladika,

Thanks a lot, that worked great. Is the only way to alocate space for fullNetPath to run WNetGetConnection twice?

I also want this function to work for 16-bit, currently I have:

            {$IFDEF WIN32}
            // allocate space for fullNetPath
            WNetGetconnection(PChar(driveLetter), '', x);
            SetString(fullNetPath, nil, x);
            returnValue := WNetGetconnection(PChar(driveLetter), PChar(fullNetPath),x);
            {$ELSE}
            returnValue := WNetGetconnection(@DriveLetter[1], @FullNetPath[1], @x);
            {$ENDIF}

returnValue is returning 51 here. I can't find what that means, do you have a list of return values somewhere (you knew 487)? Any ideas why the 16-bit version is not working?

Thanks a lot, Tom.

0
 

Author Comment

by:tomcorcoran
ID: 1353674
Sorry, should have mentioned that I do:

   {$IFDEF WIN32}
   x: integer;
   {$ELSE}
   x: word;
  {$ENDIF}

   {$IFDEF WIN32}
   x := 0;
   {$ELSE}
   { make the size one less than the buffer }
   x := 255;
   {$ENDIF}

0
 
LVL 3

Expert Comment

by:vladika
ID: 1353675
Code 51 (in hex $33) is WN_BAD_LOCALNAME (see WinTypes.INT)
@DriveLetter[1] is NOT NULL-terminated string !!!

So, In Delphi 1

var
  PDriveLetter, PFullNetPath: PChar;
  X: Word;

// ......... code .......
  PDriveLetter := StrAlloc(Length(DriveLetter)+1);
  try
    PDriveLetter := StrPCopy(PDriveLetter, DriveLetter);
    X := 0;
    returnValue := WNetGetConnection(PDriveLetter, nil, @X);
    if X>256 then
      raise Exception.Create('FullNetPath too long !!!');
    if X <> 0 then
    begin
      PFullNetPath := StrAlloc(X);
      try
        returnValue := WNetGetConnection(PDriveLetter, PFullNetPath, @X);
        FullNetPath := StrPas(PFullNetPath);
      finally
        StrDispose(PFullNetPath);
      end;
    end;
  finally
    StrDispose(PDriveLetter);
  end;

// .....

The main strings difference in 16-bit and 32-bit Delphi is
In 16-bit Delphi string is SHORT string
In 32-bit Delphi string is LONG string

The short string type represents a statically-allocated string with maximum length between 1 and 255 characters, and a dynamic length between 0 and the maximum length. The short string type primarily exists for reasons of backward compatibility with earlier versions of Delphi and Borland Pascal.

The long string type represents a dynamically-allocated string with a maximum length limited only by available memory.

See Help in 32-bit Delphi.

Vladika.
0
 

Author Comment

by:tomcorcoran
ID: 1353676
Vladika,

Many thanks again, I'm sure that will work. I should have seen the problame was because of the different strings. Thanks, Tom.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question