Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Novell API with Delphi 2009 <> Novell API with Delphi 2007 1

Status
Not open for further replies.

MagicFrisbee

Programmer
Feb 22, 2006
74
US
I have a very simple procedure which has worked for the last few versions of Delphi (2007, 2006, 2005, and 7). All it does is grab the user name of whoever is logged into the Novell Network on that computer. The sample was pretty much taken off the Novell web site (which is outdated, but the code still worked). Now, in Delphi 2009, it does not work and I suspect it has something to do with UnicodeStrings all over the product. The Novell API merely redeclares functions in Novell DLL's without wrapping them (you basically see a lot of "StdCall; external" lines in the source). However, I'm using Novell-defined types so I'm confused. Here is the code, which will only work in Delphi if you have the Novell Client installed and you've also downloaded and installed the Novell API for Delphi, which is at . Click novell-delphilib-devel-2006.06.14-1windows.zip to download it, then unzip it to the Imports folder of your Delphi product.

So, in Delphi 2007 and ealier, the result of the following function is my Novell username. In Delphi 2009 the result is weird characters.

Code:
uses
  NetWin32;   // NetWare Libraries (32 bit)


procedure TForm1.FormCreate(Sender: TObject);
  procedure CallNetwareFunction( const ReturnCode: Integer; const FunctionName: String );
  const
    SUCCESS = 0;
  begin
    if ReturnCode <> SUCCESS then
      raise Exception.Create( 'The following Netware function returned an error: ' +
        FunctionName + ' (' + IntToStr( ReturnCode ) + ')' );
  end;
var
  NetwareContextHandle: NWDSContextHandle;
  MyName: array[0..MAX_DN_CHARS] of nstr8;
  pMyName: pnstr8;
  ContextFlags: Integer;
begin
  CallNetwareFunction( NWDSCreateContextHandle( NetwareContextHandle ),
    'NWDSCreateContextHandle' );
  try
    //Without these next three function calls, NWDSWhoAmI will return a string that looks
    //like this: CN=USERNAME.
    CallNetwareFunction( NWDSGetContext( NetwareContextHandle, DCK_FLAGS, @ContextFlags ),
      'NWDSGetContext' );
    ContextFlags := ContextFlags or DCV_TYPELESS_NAMES;
    CallNetwareFunction( NWDSSetContext( NetwareContextHandle, DCK_FLAGS, @ContextFlags ),
      'NWDSSetContext' );
    pMyName := @MyName;
    CallNetwareFunction( NWDSWhoAmI( NetwareContextHandle, pMyName ),
      'NWDSWhoAmI' );
    Edit1.Text := MyName;
  finally
    NWDSFreeContext( NetwareContextHandle );
  end;

GIS Programmer
City of Orem, UT
 
It seems likely that this is a Unicode issue.

Have you set a breakpoint on the line
Code:
Edit1.Text := MyName
to see if MyName contains valid data?



Andrew
Hampshire, UK
 
Hi,

For myself, I'm using environment variable to get the novell login (which is used for user's identification instead of another user/password dialog box),

sLogin := StrUpper(PChar(GetEnvironmentVariable('NWUSERNAME')));

BTW, I'm still using D2007...

Hope this helps,

Rej Cloutier
 
towerbase - I have put a breakpoint there and the text contains various Greek characters like boxes and various ASCII values that aren't even close to my username.

rcloutie - I remember looking at that when I first made my program, but all it takes is a clever, super-Windows user to change that environment variable in a DOS Window and suddenly he's impersonating his superiors and approving things he's not supposed to have authority to approve, like his own salary increase.

GIS Programmer
City of Orem, UT
 
neither SET command and changing registry key change the variable's value. It seems to be changed (typing SET again will show you the new value) but it is not (closing dos window and restarting it will recover the original value).

Again, using command SET, DOESN'T CHANGE the NWUSERNAME environment variable called from a Delphi Program.

Give it a try.

Rej Cloutier
 
Wow! How 'bout that! I never tried that before, bringing up another DOS Window. Also, after calling SET and leaving up the DOS Window, I wrote a simple program that displayed the value of NWUSERNAME in a TEdit and it showed my real username, not the one I pretended to be. I think I will go this route and I hope there are no bad repurcussions.

Incidentally, I was able to solve the problem just now by declaring the MyName variable to an array of type AnsiChar, instead of Char or nstr8. I can't find any help in Delphi about using Unicode with DLLs. You'd hope that the External Declarations section, in the Procedures and Functions would help out a little, but that's CodeGear help for ya.

Thanks, everyone. Star for you, Rej.

GIS Programmer
City of Orem, UT
 
Well, I tried logging out and logging back in with the Workstation Only box checked. I have a handful of other mapped drives that auto-connect upon login. Somehow, when I log in, there is no NWUSERNAME variable, and setting it does nothing. However, the code I posted above originally returns my Novell username, and grabbing NWUSERNAME returns a blank string.

So, I don't know exactly what settings gave me such privileges, but Novell knows I'm logged into the network, I have all of my usual permissions to folders and files, but I had Workstation Only checked! And NWUSERNAME is blank, so I have to go with the AnsiChar idea. [purpleface]

GIS Programmer
City of Orem, UT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top