PDA

View Full Version : What other changes should I make?



srohit24
4th March 2009, 06:25
I am trying to retrieve data from a dll and show it on a console window.

I first tried it out with VC++ and this was the main code


#include <iostream>
#include<windows.h>

using namespace std;


HMODULE hDll = LoadLibrary(L"dllprog.dll");
FARPROC fpTestDll = GetProcAddress(hDll, "TestDll");
typedef char* (__stdcall *fpTestDllT)();
fpTestDllT TestDll = (fpTestDllT)fpTestDll;

int main()
{
char* a;
a = TestDll();
cout <<endl << a;
Sleep(3000);
}

this is working perfectly fine.

Now i am trying to write the same thing with Qt using QtCreator to make it platform independent

this is my code


#include <QtCore/QCoreApplication>
#include <QLibrary>
#include <iostream>
#include <windows.h>


QLibrary myLib("dllprog");
typedef void (*MyPrototype)();
MyPrototype myFunction =
(MyPrototype) QLibrary::resolve("dllprog", "TestDll");

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

char b;
b = TestDll();
cout <<endl << b;

return a.exec();
}

but the compiler shows some errors. what changes should I make to get the app working??

One more question, i tried a helloworld dialog app. It was working fine when i used the creator to build and run, but when i went to the .exe file and double clicked it, it says mingwm10.dll is missing. I have given the path os MINGW in the environmental variables. what should I do to avoid that?

Will the created app be able to run in systems that dont have MINGW?

talk2amulya
4th March 2009, 07:00
please show the errors it gave

srohit24
4th March 2009, 08:07
here are the errors. 3 of them


/main.cpp:21: error: `TestDll' was not declared in this scope
main.cpp:22: error: `cout' was not declared in this scope
/main.cpp:22: error: `endl' was not declared in this scope

and 3 warning for unused cout, endl and TestDll.

hopefully you can help

caduel
4th March 2009, 08:28
for the cerr error

using namespace std;

for the TestDll() error, probably:
you defined a function (varible) myFunction, call that instead?
and you should probably also declare it to return a char* instead of void?


#include <QCoreApplication>
#include <QLibrary>
#include <iostream>
using namespace std;

QLibrary myLib("dllprog");
typedef char* (*MyPrototype)();
MyPrototype myFunction =
(MyPrototype) QLibrary::resolve("dllprog", "TestDll");

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

char *b = TestDll();
cout << endl << b;

return 0;
}
(all untested, of course...)

HTH

srohit24
4th March 2009, 09:02
thanks for replying mate.

I just corrected cout and endl using namespace std (silly error :( )

thanks mate, it worked :)

Can you explain your reasoning?

Now I need to create a GUI appliaction. I want to use a pushbutton to paste the contents in variable b into a text box, how can that be done? whats the code for it? I am new to GUI based coding.

this is the working code


#include <QtCore/QCoreApplication>
#include <QLibrary>
#include <iostream>
#include <windows.h>

using namespace std;
/*HMODULE hDll = LoadLibrary(L"dllprog.dll");
FARPROC fpTestDll = GetProcAddress(hDll, "TestDll");
typedef char* (__stdcall *fpTestDllT)();
fpTestDllT TestDll = (fpTestDllT)fpTestDll; */

QLibrary myLib("dllprog");
typedef char* (*MyPrototype)();
MyPrototype myFunction =
(MyPrototype) QLibrary::resolve("dllprog", "TestDll");

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

char *b = myFunction();
cout <<endl << b;
//Sleep(3000);
return a.exec();
}

caduel
4th March 2009, 09:34
reasoning? what reasoning?
well, if you have a function that should return "text" its return type probably is not void :)
(and the return type in the windows example was char*, so I figured "same dll, same type").

GUI:
you need to create a little dialog (by code or with designer).
put a QPushButton inside, and some text widget (e.g. QTextEdit).
Connect a slot to the QPushButton's clicked() signal. In the slot call your dll stuff.
With the result, call QTextEdit::setText()

See the Qt docs for examples on how to do that: tutorials

HTH

srohit24
11th March 2009, 15:14
how can i call my dll function in the slot?

When i add a slot i dont get my dll function in the textedit window in slots.

what should i do?