I Want to use MediaInfo.dll downloaded from here [DLL v0.7.94][1]
[1]: https://mediaarea.net/bg/MediaInfo/Download/Windows. My question is how to call some function in this .dll using Qt framework
#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
QLibrary lib("MediaInfo.dll");
lib.load();
if (!lib.isLoaded()) {
qDebug() << lib.errorString();
}
if (lib.isLoaded()) {
qDebug() << "success";
}
}
return a.exec();
}
You have a good example in QLibrary documentation. Basically you have to know the function name (the symbol) and it's prototype.
#include <QCoreApplication>
#include <QLibrary>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if (QLibrary::isLibrary("MediaInfo.dll")) { // C:/MediaInfo.dll
QLibrary lib("MediaInfo.dll");
lib.load();
if (!lib.isLoaded()) {
qDebug() << lib.errorString();
}
if (lib.isLoaded()) {
qDebug() << "success";
// Resolves symbol to
// void the_function_name()
typedef void (*FunctionPrototype)();
auto function1 = (FunctionPrototype)lib.resolve("the_function_name");
// Resolves symbol to
// int another_function_name(int, const char*)
typedef int (*AnotherPrototypeExample)(int, const char*);
auto function2 = (AnotherPrototypeExample)lib.resolve("another_function_name");
// if null means the symbol was not loaded
if (function1) function1();
if (function2) int result = function2(0, "hello world!");
}
}
return a.exec();
}
You need to declare a function prototype and get a pointer to a function in DLL.
QLibrary myLib("mylib");
typedef void (*MyPrototype)();
MyPrototype myFunction = (MyPrototype) myLib.resolve("mysymbol");
if (myFunction)
myFunction();
See more on QLibrary.
Why do you want to use QLibrary when there is a C/C++ binding?
Include file with functions prototypes.
Example with dynamic call of the DLL.
A bit hidden, but all is included in the the DLL zip package in the link you provided in your question.
Jérôme, developer of MediaInfo
Related
I have a weird problem, this is my code :
test.h
#ifndef _test_
#define _test_
#include <iostream>
#include <string>
class Test {
public:
Test();
~Test();
void addName(std::string _Name);
private:
std::string Name;
};
#endif // _test_
test.cpp
#include "test.h"
Test::Test() {}
Test::~Test() {}
void Test::addName(std::string _Name) {
std::cout << _Name << std::endl;
Name = _Name;
std::cout << _Name << std::endl;
}
main.cpp
#include "test.h"
int main(int argc, char* argv[]) {
Test* project;
project->addName("abc");
return 0;
}
Results :
abc
The program has unexpectedly finished.
That's because you have a pointer to a Test object, but it doesn't actually point anywhere. That leads to undefined behavior when you try to dereference the pointer.
Declare the object as an actual object, not a pointer:
Test project;
project.addName("abc");
The pointer project is default-initialized and has indeterminate value, so dereferencing it has a big chance to cause abnromal termination of the program.
Try creating an object and assigning it before dereferencing like this:
#include "test.h"
int main(int argc, char* argv[]) {
Test* project = new Test;
project->addName("abc");
delete project;
return 0;
}
I'm trying to create tests for a c++ application with QtTest. The three relevent files that I have are: GuiTests.cpp which contains my main function, testsuite1.cpp which contains my tests and testsuite1.h which contains the definitions of my tests. I created these files with help from different guides, for example this one.
When I try to build I get this error:
no matching function for call to 'qExec(TestSuite1 (*)(), int&, char**&)'
no known conversion for argument 1 from 'TestSuite1 (*)()' to 'QObject*'
I don't understand why, as you can see in testsuite.h below TestSuite1 is a QObject. The funny thing is this exact code (I am pretty sure) worked before but then I fiddled around with passing argc and argv to guiTest() for a while, and after I removed argc and argv and went back to what I had before (what I currently have, please see the files below) I got this error.
I've been trying to solve this problem for a long time and I can't find any answers online, so please help me, any help is appreciated. Thanks!
GuiTests.cpp
#include "testsuite1.h"
#include <QtTest>
#include <QCoreApplication>
int main(int argc, char** argv) {
TestSuite1 testSuite1();
return QTest::qExec(&testSuite1, argc, argv);
}
testsuite1.h
#ifndef TESTSUIT1_H
#define TESTSUIT1_H
#include <QMainWindow>
#include <QObject>
#include <QWidget>
#include <QtTest>
class TestSuite1 : public QObject {
Q_OBJECT
public:
TestSuite1();
~TestSuite1();
private slots:
// functions executed by QtTest before and after test suite
void initTestCase();
void cleanupTestCase();
// functions executed by QtTest before and after each test
//void init();
//void cleanup();
// test functions
void testSomething();
void guiTest();
};
#endif // TESTSUIT1_H
testsuite1.cpp
#include "testsuite1.h"
#include <QtWidgets>
#include <QtCore>
#include <QtTest>
TestSuite1::TestSuite1()
{
}
TestSuite1::~TestSuite1()
{
}
void TestSuite1::initTestCase()
{
}
void TestSuite1::cleanupTestCase()
{
}
void TestSuite1::guiTest()
{
QVERIFY(1+1 == 2);
}
void TestSuite1::testSomething()
{
QLineEdit lineEdit;
QTest::keyClicks(&lineEdit, "hello world");
QCOMPARE(lineEdit.text(), QString("hello world"));
//QVERIFY(1+1 == 2);
}
//QTEST_MAIN(TestSuite1)
//#include "TestSuite1.moc"
TestSuite1 testSuite1();
declares a function named testSuite1 returning TestSuite1. Taking address of it gives you TestSuite1 (*)() (a function pointer) instead of TestSuite1* that would convert to QObject*.
Use one of the following:
TestSuite1 testSuite1;
TestSuite1 testSuite1{};
auto testSuite1 = TestSuite();
auto testSuite1 = TestSuite{};
to declare a variable.
I am starting to learn threads in the C++11 standard in Qt.I can't include library ,no such of directory For example, I have the following simple code:
#include <QCoreApplication>
#include <thread>
#include <iostream>
using namespace std;
void func();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread th1;
th1.run();
return a.exec();
}
void func()
{
cout << "This string from thread!"<<endl;
}
On the second string of the code, I have an error. The compiler doesn't "see" , I know that i must "include" 11 standard, so i go to .pro and paste CONFIG += c++11, but it isn't helping me :C
Please, I need your help!
You try to use QThread subclass, but you said that you want to use C++11 thread so use this:
#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>
void foo()
{
std::cout << "This string from thread!"<<endl;
//while(true)
//{
// qDebug() <<"works";
// Sleep(500);
//}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::thread t(foo);
t.join();
return a.exec();
}
Also next is wrong:
MyThread th1;
th1.run();
Should be:
MyThread th1;
th1.start();
Details about threading with Qt classes:
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
http://qt-project.org/doc/qt-5/thread-basics.html
http://qt-project.org/doc/qt-5/qtconcurrent-index.html
Here is my implementation
Film.h //header
#ifndef FILM_H
#define FILM_H
#include <QString>
class Film {
protected:
QString title;
double dailyRate;
public:
Film(QString ti,double dr);
virtual double calculateRental(int num)const;
};
#endif // FILM_H
Film.cpp
#include "film.h"
#include <QString>
Film::Film(QString ti,double dr){
title=ti;
dailyRate=dr;
}
double Film::calculateRental(int num)const {
return dailyRate*num;
}
main.cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QTextStream>
#include "film.h"
using namespace std;
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);
Film f("Top Gun", 10.00); //create an instance of a film
cout <<f.calculateRental(2);
return a.exec();
}
how do I count the number of Film instances created? I know is something like that:
static int numOfFilms;
numOfFilms++;
how do I use the code?
It depends. If you replace Class with class, and QString has a conversion constructor from const char*, then yes.
i'm trying to learn c++, but i can not find if it's possible to extend a class in this way:
main.cc
#include "mWindow.h"
using namespace std;
int main( int argc, char* argv[] ) {
gtk_init( &argc, &argv );
mWindow win = mWindow();
gtk_main();
return 0;
}
mWindow.cc
#include "mWindow.h"
mWindow::mWindow() {
gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (this, "my window");
gtk_widget_show_all (GTK_WIDGET(this));
}
mWindow.h
#ifndef MWINDOW_H_INCLUDED
#define MWINDOW_H_INCLUDED
#include <gtk/gtk.h>
using namespace std;
class mWindow : public GtkWindow {
public:
mWindow();
};
#endif
I suggest you take a look at gtkmm (http://www.gtkmm.org/) if you want to use GTK+ in conjunction with C++, i.e. there is no need to try to reinvent the wheel and write your own C++ interface for GTK+ (which is a C library).
thanks,
I was trying to use C libraries as if they were C++.
This is how I solved with gtkmm:
main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window);
return 0;
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm-2.4/gtkmm.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
show_all_children();
}
also add the command to complete successfully, at least on Arch Linux:
g++ $(pkg-config --cflags --libs gtkmm-2.4) main.cc examplewindow.cc examplewindow.h -o executable
another small indication, what i shouldl use as dynamic arrays or vectors and for hashmap?