LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2006, 01:38 PM   #1
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Rep: Reputation: 30
Changing Windows Password Programmatically


Hi. As the name suggest, I want to change my windows password programmatically. I've tried using the API function NetUserChangePassword. However, it gives system error 86 when I try to change the password. The domain name I'm providing is NULL. Can anybody tell me why this is happening. I have administrative access on this system, but still I can't change the password programmatically. Thanks.
 
Old 05-27-2006, 02:19 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Error 86 is ERROR_INVALID_PASSWORD...
How are you calling NetUserChangePassword()?
 
Old 05-27-2006, 02:56 PM   #3
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
As I mentioned, I'm trying to run Microsoft's own sample code. But it doesn't work. Anyway, here's what I'm trying to do:

Code:
#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h> 
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwError = 0;
   NET_API_STATUS nStatus;
   //
   // All parameters are required.
   //
   if (argc != 5)
   {
      fwprintf(stderr, L"Usage: %s \\\\ServerName UserName OldPassword NewPassword\n", argv[0]);
      exit(1);
   }
   //
   // Call the NetUserChangePassword function.
   //
   nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]);
   //
   // If the call succeeds, inform the user.
   //
   if (nStatus == NERR_Success)
      fwprintf(stderr, L"User password has been changed successfully\n");
   //
   // Otherwise, print the system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);

   return 0;
}
 
Old 05-27-2006, 03:08 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
For the first argument just try 0 and see what happens, that shoudl default to teh local machine, hence:

nStatus = NetUserChangePassword(0, argv[2], argv[3], argv[4]);
 
Old 05-27-2006, 03:45 PM   #5
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
I tried what you asked, but I'm still getting the same error. Any other suggestions?
 
Old 05-27-2006, 03:53 PM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Double check that the old and new passwords are correct, and that they are being read in correctly (print them out just before you call NetUserChangePassword()
 
Old 05-27-2006, 04:13 PM   #7
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
Ok, I've figured out what the problem was. I was going through the API carefully, and I noticed that it required the arguments to be in Uniicode. So, I modified the code as follows:
Code:
#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h> 
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwError = 0;
   NET_API_STATUS nStatus;
   //
   // All parameters are required.
   //
   if (argc != 5)
   {
      fwprintf(stderr, L"Usage: %s \\\\ServerName UserName OldPassword NewPassword\n", argv[0]);
      exit(1);
   }
   //
   // Call the NetUserChangePassword function.
   //
   nStatus = NetUserChangePassword(0, L"user", L"oldpass", L"newpass");
   //
   // If the call succeeds, inform the user.
   //
   if (nStatus == NERR_Success)
      fwprintf(stderr, L"User password has been changed successfully\n");
   //
   // Otherwise, print the system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);

   return 0;
}
and it worked. Now I just need to know how to convert strings stored in variables to unicode, and I'm done [] Any suggestions, anyone?
 
Old 05-28-2006, 01:46 AM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Actually, my suggestion to your original question would have been the same as my answer to your latest question - I'd write it in WSH. VB is a sucky language - but, for better or for worse, it ("Windows Scripting Host") is really the language of choice for a job like this.

IMHO .. PSM

PS:
Why not just compile your code with /DUNICODE? Everything should work perfectly, without your having to do anything special. Anything you read from an ASCII file or any input you accept from an ASCII keyboard should automagically work fine...

Last edited by paulsm4; 05-28-2006 at 01:47 AM.
 
Old 05-28-2006, 09:16 AM   #9
kamransoomro84
Member
 
Registered: Feb 2004
Location: Pakistan
Distribution: OpenSUSE 10.2
Posts: 241

Original Poster
Rep: Reputation: 30
paulsm4, first of all, using VB is not an option. Firstly, I don't know VB , secondly, I have to integrate this program with a Java program through JNI. Hence, C/C++ is my language of choice. And thankyou everyone, but I figured out how to convert ASCII strings to Unicode all on my own.
 
Old 05-28-2006, 12:35 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
kamransoomro84 -

In general, if you deal with MS-Windows systems (especially if you have to deal with them as an administrator, a DBA, or a help desk technician), you're going to have to learn VB sooner or later. For precisely the same reason Unix and Linux admins usually learn at least a little bit shell, awk and Perl.

Second: just because your program happens to be Java, doesn't necessarily mean you have to use C or C++ (they're two different things, of course), and almost certainly doesn't mean you have to use JNI. You could just as easily "exec()" an external program ... that's written in any language you like.

IMHO .. PSM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gnome 2.10: How to raise windows programmatically? tetractis Linux - Software 3 02-19-2006 06:32 AM
password changing bshearer *BSD 3 04-26-2005 10:13 AM
Changing password macadam Linux - Software 3 03-30-2005 11:52 AM
Changing proxy server programmatically irfanhab Programming 1 10-19-2004 02:18 AM
Password keeps changing Capt Linux - Newbie 0 10-13-2004 06:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration