user1576869
user1576869

Reputation:

Using Win32 API in QT for Windows

i'm migrating from .net C# to QT C++, and I'm trying to use this Win32 function to emulate drive in QT:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DefineDosDevice(int flags, string devname, string path);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int QueryDosDevice(string devname, StringBuilder buffer, int bufSize);

The code above is in C# but i'm have no idea how to use them in QT, someone can give me an example how to do this and how to use any Win32 API in QT?

Upvotes: 5

Views: 12744

Answers (4)

ytfrdfiw
ytfrdfiw

Reputation: 29

add #pragma comment(lib,"user32") in .h or .cpp files

Upvotes: 2

user1576869
user1576869

Reputation:

Thanks for any Answer! The answer for my question is:

#include <Windows.h>

void MainWindow::on_pushButton_clicked()
{
    QString qstr1 = "Z:";
    QString qstr2 = getenv("tmp");
    DefineDosDevice(0, (LPCTSTR)qstr1.utf16(), (LPCTSTR)qstr2.utf16());
}

void MainWindow::on_pushButton_2_clicked()
{
    QString qstr = "Z:";
    DefineDosDevice(2, (LPCTSTR)qstr.utf16(), 0);
}

Upvotes: 2

Martin
Martin

Reputation: 3753

Looking at the documentation for DefineDosDevice and QueryDosDevice, you'll see in the table down the bottom that they are both defined in the "kernel32.lib" library, and declared in "Windows.h" (indirectly).

In your code, you should #include <Windows.h>: you'll then be able to make calls to them directly.

I'm not sure about your specific compiler/IDE, but if you get errors at link time about "Unresolved references", you may need to add "kernel32.lib" (from the Windows SDK) to your library path.

Upvotes: 4

Yuan
Yuan

Reputation: 1157

You can use win32 APIs as regular C functions. There is no difference in QT and other C++ programs.

Upvotes: 1

Related Questions