PDA

View Full Version : Invoking dll function with argument crashed



moiit
5th March 2012, 08:32
My Qt app needs to invoke a function with arguments from my owned dll, which was created with VS2010.
It runs as expected if the imported function without any argument.
But with arguments it will crash, although there is no problem when compiling.

Any advise will be helpful!
Thanks.

//.pro

LIBS += -L ./ -lop

//op.h

#ifndef OP_H
#define OP_H
#include <string>
extern "C" __declspec(dllexport) void excute(std::wstring url);
#endif // OP_H

//invoke code

std::wstring url=qstr_url.toStdWString();
excute(url);

//dll in vs2010
// op.cpp

#include "stdafx.h"
extern "C" __declspec(dllexport) void excute(std::wstring url )
{
STARTUPINFO start_info;
PROCESS_INFORMATION proc_info;
::ZeroMemory(&start_info, sizeof(start_info));
start_info.cb = sizeof(start_info);
::ZeroMemory(&proc_info, sizeof(proc_info));

std::wstring browser = L"C:/Program Files/Internet Explorer/iexplore.exe";
//std::wstring url = L"http://google.com";
std::wstring command = browser + L" " + url;
LPWSTR command_line = new WCHAR[command.size() + 1];
wcscpy_s(command_line, command.size() + 1, command.c_str());
command_line[command.size()] = L'\0';
::CreateProcess(NULL, command_line, NULL, NULL, FALSE, 0, NULL, NULL, &start_info, &proc_info);
if (proc_info.hThread != NULL) {
::CloseHandle(proc_info.hThread);
}
if (proc_info.hProcess != NULL) {
::CloseHandle(proc_info.hProcess);
}

high_flyer
5th March 2012, 10:12
Run it in a debugger, and step through, and see on which line it crashes.
Then we can start looking with some idea on what to look for.
It doesn't sound like a problem with the lib, but more like a problem in the code in self, in execute() - probably a pointer issue or similar.

moiit
5th March 2012, 11:00
thanks for advise!

debug result point to the invoking code line:
excute(url);
but there is only this line doing the invoking from dll.

i don't know what's wrong with it.

i use the implicit invocation in qt codes.
i tried explicit invoking but the result is the same.

high_flyer
5th March 2012, 16:56
Add a check to make sure the url parameter is not null - before calling execute() - in in execute it self - it should be able to exit gracefully on null string url.

moiit
5th March 2012, 17:55
thanks for advise!
i added one line like you said:


if(url.length()!=0)
excute(url);

but it stiil crashed.

ChrisW67
6th March 2012, 04:59
Single step your code inside excute(), it's failing among that non-Qt code somewhere.

Does ieexplore.exe exist at that path? The space embedded in the path to iexplore.exe is possibly causing issues: see the CreateProcess (http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx) docs.

Or... use Qt because QDesktopServices::openUrl() is a much easier way to launch the system default browser.

moiit
6th March 2012, 13:32
yes, maybe merging browser path and link is the issue. so i modified the codes:


#include "stdafx.h"

extern "C" __declspec(dllexport) void excute() //removed arguments to open google page, but it opened a blank page
{
STARTUPINFO start_info;
PROCESS_INFORMATION proc_info;

::ZeroMemory(&start_info, sizeof(start_info));
start_info.cb = sizeof(start_info);
::ZeroMemory(&proc_info, sizeof(proc_info));

LPCTSTR browser = L"G:/Program Files/Internet Explorer/iexplore.exe";
LPTSTR command_line = L"http://google.com";

::CreateProcess(browser, command_line, NULL, NULL, FALSE, 0, NULL, NULL, &start_info, &proc_info);

if (proc_info.hThread != NULL) {
::CloseHandle(proc_info.hThread);
}

if (proc_info.hProcess != NULL) {
::CloseHandle(proc_info.hProcess);
}
this time, it did not crash, but ie opened with a blank page, not google.
i must be crazy...

high_flyer
6th March 2012, 13:53
Why don't you do what we suggested to you?
Step through exectue() - and follow your url variable and all other values - and you will see exactly what and when is going wrong.

moiit
6th March 2012, 14:19
sorry but i did the debug and it pointed to that the issue code line is "excute(url);" , and the url is right.
so what i think is the dll has some wrong codes.
in post #7, i modified some codes, it did not crash now, but it opened a blankpage instead of the assigned url.

high_flyer
6th March 2012, 14:33
maybe you don't understand what is meant:
by stepping through, you step one line at a time during execution in the debugger - IN your execute() function.
This way you can examine all the values of your variables, and see which ones do not match what you expect.
You might want to escape the slashes in the path to iexploere.

moiit
6th March 2012, 16:41
thanks for advise!
do you mean running vs2010's debugger to check the dll function?
then i will do.

high_flyer
6th March 2012, 16:47
Yes - what ever debugger you are using will be fine.

d_stranz
6th March 2012, 16:57
But remember that you must build a DEBUG version of your DLL in order to step into its execute() method and debug it. You can't link to a release version of the DLL and debug it.

moiit
7th March 2012, 14:34
thanks for all you guys's help!
i must learn more, more...