-1

I would like to change user password on my Windows 7 PC using C++.

But when I compile it gets error:

 undefined reference to 'NetUserChangePassword' 
 [Error] ld returned 1 exit status.`

How can I fix it?

Here is the MSDN page with the NetUserChangePassword function:

#ifndef UNICODE
    #define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <lm.h>

bool ChangeUserPassword(LPCWSTR OldPassword, LPCWSTR NewPassword)
{
    NET_API_STATUS nStatus;
    LPTSTR lp = new TCHAR[256];
    DWORD dw = 256;
    GetUserName(lp, &dw);
    nStatus = NetUserChangePassword(NULL, lp, OldPassword, NewPassword);
    delete[] lp;
    if (nStatus == NERR_Success)
        return true;
    return false;
}

int main(int argc, char** argv)
{
    LPCWSTR Old_P = L"C";
    LPCWSTR New_P = L"D";
    ChangeUserPassword(Old_P, New_P);
    return 0;
}

I tried to link to the project the winapi32.dll in two ways

  1. i tried to add using the project option
  2. i tried to add following line

    HINSTANCE hInst = LoadLibrary( L"C:\\Windows\\System32\\netapi32.dll ");
    

but i get always the same error

Michele
  • 149
  • 1
  • 10
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Apr 12 '14 at 21:30
  • ensure that the path to the Lib folder of your SDK installation is part of the library search pathes of your linker project settings – 4pie0 Apr 12 '14 at 21:32
  • http://support.microsoft.com/kb/151546 – 0x90 Apr 12 '14 at 21:36

1 Answers1

1

The requirements section of the MSDN topic you linked to states that you must link the Netapi32.lib library. That is the step that you have missed and explains the missing external error.

As to how to resolve the problem it is hard to say for sure. You are not using the MS compiler and so the #pragma approach won't work. Consult the docs for your compiler/linker to work out how to link this library.

It looks like you are using a GCC based compiler and so need to add -lnetapi32 to the options.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490