Board index » delphi » Help needed get API to work in Delphi4 - works ok in VB :-(

Help needed get API to work in Delphi4 - works ok in VB :-(

i,

I'm trying to use the NetRenameMachineInDomain API call from within Delphi
4.

(see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netm...
mapi2_8hm6.asp for details of the API structure)

The VB6 code snippet below works fine, but my poor attempt to convert this
to Delphi (below) isn't so sucessfull

The errors I get are in the returncode I get either a:

87 : ShowMessage('The parameter is incorrect');
1113 : ShowMessage('No mapping for the Unicode character exists in the
target multi-byte code page');
1210 : ShowMessage('The format of the specified computer name is invalid');
1326 : ShowMessage('Logon failure: unknown user name or bad password');

I did try the string parameters as PChars, on the function declaration but
no joy their either.

I've got a feeling the problem lies with the flag, in VB this is &H2, I'm
not sure if my attempt at using "2", and "$00000002" are correct.

I'd appreicate it if someone could point me in the right direction.

thanks

David

'VB6 - Works OK

Private Declare Function NetRenameMachineInDomain Lib "netapi32" _
(ByVal lpserver As Long, ByVal machinename As Long, ByVal lpaccount As Long,
_
ByVal passwrd As Long, ByVal foptions As Long) As Long

Private Const NETSETUP_ACCT_CREATE = &H2            'Do the server side
account creation/rename

Private Sub Command1_Click()
        Dim flags, res1 As Long
        flags = NETSETUP_ACCT_CREATE
        res1 = NetRenameMachineInDomain(0, StrPtr("NewName"),
StrPtr("administrator"), StrPtr("password"), flags)
        MsgBox (res1)
End Sub

//--------------------------------------------------------------------------
// My attempt a the Delphi Port
// -------------------------------------------------------------------------

Const
     NETSETUP_ACCT_CREATE  = 0x00000002;

Function NetRenameMachineInDomain(lpserver:string; machinename:string;
lpaccount:string; passwrd:string; foptions : LongInt):LongInt
stdcall;far;external 'netapi32.dll';

procedure TForm1.Button1Click(Sender: TObject);
var strNewComputerName, strUserID, strPassword : string;
    resultcode                                 : LongInt;
    flags                                      : LongInt;
begin
   flags:=NETSETUP_ACCT_CREATE;
   resultcode:= NetRenameMachineInDomain('', 'New Name', 'administrator',
'password', flags);
   Case resultcode of
          0 : ShowMessage('Success');
         87 : ShowMessage('The parameter is incorrect');
       1113 : ShowMessage('No mapping for the Unicode character exists in
the target multi-byte code page');
       1210 : ShowMessage('The format of the specified computer name is
invalid');
       1326 : ShowMessage('Logon failure: unknown user name or bad
password');
       Else
             ShowMessage('Result Code : ' + IntToStr(resultcode));
   End;

end;

 

Re:Help needed get API to work in Delphi4 - works ok in VB :-(


Tried PChars,  function returns 87 = "The parameter is incorrect"

Any other ideas?

Thanks

David

code snippett
//-----------------------------------

function NetRenameMachineInDomain(lpserver, machinename, lpaccount, passwrd
: PChar; foptions : LongInt) : LongInt stdcall; far; external
'netapi32.dll';

procedure TForm1.Button1Click(Sender: TObject);
var strNewComputerName, strUserID, strPassword : string;
    resultcode                                 : LongInt;
    flags                                      : LongInt;
begin
   resultcode:= NetRenameMachineInDomain(PChar(''), PChar("newname"),
PChar("administrator"), PChar("password"),2);
end;

//--------------------------------------

The working VB6 example:

'-----------------------------------------
Private Declare Function NetRenameMachineInDomain Lib "netapi32" _
(ByVal lpserver As Long, ByVal machinename As Long, ByVal lpaccount As
Long,_
ByVal passwrd As Long, ByVal foptions As Long) As Long

Private Const NETSETUP_ACCT_CREATE = &H2            'Do the server side
account creation/rename

Private Sub Command1_Click()
        Dim flags, res1 As Long
        flags = NETSETUP_ACCT_CREATE
        res1 = NetRenameMachineInDomain(0,
StrPtr("NewName"),StrPtr("administrator"), StrPtr("password"), flags)
        MsgBox (res1)
End Sub

Quote
"Quivis" <qui...@ask.me.first> wrote in message

news:MPG.16d62590b19ef9b98a0a7@207.14.113.10...
Quote
> On Thu, 14 Feb 2002 23:17:37 +1300, david had this to say...

> 8>< /snip

> -> Function NetRenameMachineInDomain(lpserver:string; machinename:string;
> -> lpaccount:string; passwrd:string; foptions : LongInt):LongInt
> -> stdcall;far;external 'netapi32.dll';

> 8>< /snip

> I'm sortta guessing about this, but you're using Delphi's Pascal
> strings here, which I doubt will work. Windows' APIs usually ask for
> PChar:s. Use PChar instead of string above, and then convert your
> string to PChar: PChar('Some String');  ...when calling the function.

> HTH

> Quivis.
> --
> SPAM goes here : root@localhost
> Freeware       : Revix (indexing, renaming, personalizing of images)
> Web Space      : http://www.bahnhof.se/~smars/quivis/index.html

Re:Help needed get API to work in Delphi4 - works ok in VB :-(


In article <8aMa8.2227$PS6.209...@news02.tsnz.net>, "david" <t...@is.not.real>
writes:

Quote
>   resultcode:= NetRenameMachineInDomain('', 'New Name', 'administrator',
>'password', flags);

MSDN says that the parameters should be PWChar (ie pointer to a wide char).

Try ...

   resultcode:= NetRenameMachineInDomain(StringToWideChar(''),
                                             StringToWideChar('New Name'),
                                             StringToWideChar('administrator'),
                                             StringToWideChar('password'),
                                             flags);

Of course the constant strings you specify must be appropriately valid (perhaps
'password' is _not_ the password).

Also search in the Delphi source directory for a *.pas file with 'n
NetRenameMachineInDomain(' in it, and check the declaration of the API call.

Alan Lloyd
alangll...@aol.com

Other Threads