简体   繁体   English

在QT for Windows中使用Win32 API

[英]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: 我正在从.net C#迁移到QT C ++,我正在尝试使用此Win32函数在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? 上面的代码是在C#中,但是我不知道如何在QT中使用它们,有人可以给我一个例子如何做到这一点以及如何在QT中使用任何Win32 API?

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). 查看DefineDosDeviceQueryDosDevice的文档,您将在表格底部看到它们都在“kernel32.lib”库中定义,并在“Windows.h”(间接)中声明。

In your code, you should #include <Windows.h> : you'll then be able to make calls to them directly. 在你的代码中,你应该#include <Windows.h> :然后你就可以直接调用它们了。

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. 我不确定您的特定编译器/ IDE,但如果您在链接时遇到有关“未解析的引用”的错误,则可能需要将“kernel32.lib”(来自Windows SDK)添加到库路径中。

You can use win32 APIs as regular C functions. 您可以将win32 API用作常规C函数。 There is no difference in QT and other C++ programs. QT和其他C ++程序没有区别。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM