Qt World Summit: Register Today!

How to lock/ unlock Windows PC using Qt pro-gramatically?

  • Hi,

    I need to lock/ unlock PC (Windows O/S) with Qt pro-grammatically. Is their any direct API/ functions available for this? Any possible way to do this?

  • There is no Qt API for that. Look into the windows API's to see if you can find anything platform specific.

  • Yes, I checked that and found the way to lock the PC. Now if I make an .exe which can lock the PC, then how can I launch that .exe from Qt application on some event?

  • You can use the native API directly from your Qt application. Qt apps are just C++, and you can use native API just fine. No need for a separate application, your app just won't be portable to other platforms such a Linux and Mac if you do this, but given the task, I guess that is not an issue for you. If you want to interact with external applications, you can look into QProcess though.

  • well, i found the link http://msdn.microsoft.com/en-us/library/aa376869(v=vs.85).aspx to lock the PC using VC++/ C#. So, you want to say that this code can be directly used in Qt?

  • Basically, yes. I would not put it in a main(), and I am not sure what the #pragma is supposed to do, but otherwise it should work like this.

  • Ok. Thanks a lot for your quick responses.

    I will try this code, not in main(), but on some particular event occurrence.

    I also don't know how #pragma comment will behave in this case.

  • To cite MSDN at http://msdn.microsoft.com/en-us/library/7f0aews7(VS.80).aspx

    bq. The comment-type is one of the predefined identifiers, described below, that specifies the type of comment record. The optional commentstring is a string literal that provides additional information for some comment types. Because commentstring is a string literal, it obeys all the rules for string literals with respect to escape characters, embedded quotation marks ("), and concatenation.
    Places a library-search record in the object file. This comment type must be accompanied by a commentstring parameter containing the name (and possibly the path) of the library that you want the linker to search. The library name follows the default library-search records in the object file; the linker searches for this library just as if you had named it on the command line provided that the library is not specified with /nodefaultlib. You can place multiple library-search records in the same source file; each record appears in the object file in the same order in which it is encountered in the source file.
    If the order of the default library and an added library is important, compiling with the /Zl switch will prevent the default library name from being placed in the object module. A second comment pragma then can be used to insert the name of the default library after the added library. The libraries listed with these pragmas will appear in the object module in the same order they are found in the source code.

    So it should be sufficient to add to your .pro:

    LIBS += user32.lib
    // or
    LIBS += -luser32

  • Thanks...

    I will try that soon.

Log in to reply