-2

I keep getting these two errors in Visual Studio C++:

"Error LNK2019 unresolved external symbol __imp__CredUIPromptForCredentialsW@40 referenced in function _main ConsoleApplication1"

"Error LNK1120 1 unresolved externals ConsoleApplication1"

I am trying to get the part that opens up the enter credentials dialog of this example code to run: https://learn.microsoft.com/en-us/windows/desktop/taskschd/weekly-trigger-example--c---

I am new to C++ but I am pretty sure that all of the datatypes that I am passing in to the CredUIPromptForCredentials function are correct. I have already tried reading the links from the error but they are not helping me.

int main() {
    CREDUI_INFO cui;
    TCHAR credLoc[CREDUI_MAX_USERNAME_LENGTH] = _T("");
    TCHAR pszName[CREDUI_MAX_USERNAME_LENGTH] = _T("");
    TCHAR pszPwd[CREDUI_MAX_PASSWORD_LENGTH] = _T("");
    BOOL fSave;
    //DWORD dwErr;

    cui.cbSize = sizeof(CREDUI_INFO);
    cui.hwndParent = NULL;
    //  Ensure that MessageText and CaptionText identify
    //  what credentials to use and which application requires them.
    cui.pszMessageText = TEXT("Account information for task registration:");
    cui.pszCaptionText = TEXT("Enter Account Information for Task Registration");
    cui.hbmBanner = NULL;
    fSave = FALSE;

    //  Create the UI asking for the credentials.
    CredUIPromptForCredentialsW(
        &cui,                             //  CREDUI_INFO structure
        credLoc,                         //  Target for credentials
        NULL,                             //  Reserved
        0,                                //  Reason
        pszName,                          //  User name
        CREDUI_MAX_USERNAME_LENGTH,       //  Max number for user name
        pszPwd,                           //  Password
        CREDUI_MAX_PASSWORD_LENGTH,       //  Max number for password
        &fSave,                           //  State of save check box
        CREDUI_FLAGS_DO_NOT_PERSIST);

    return 0;
}

I am expecting to see a dialog box that prompts for a username and password but I only get the errors listed above and the program fails to build.

Ameen
  • 15
  • 4
  • @KenWhite "*The code you've posted cannot generate the error you've indicated*" - why? The errors shown are LINKER errors, not COMPILER errors. The code shown can certainly lead to those errors. – Remy Lebeau Apr 06 '19 at 06:23

1 Answers1

0

Your project needs to link to Credui.lib, as stated in the CredUIPromptForCredentialsW() documentation. Either add the .lib file to your project, or else use a #pragma comment(lib, "Credui.lib") statement in your code.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770