tags:

views:

107

answers:

1

I have some code defined as follows:

typedef long (*ShellExecFunct)(long, const char*, const char*, long, long, long);

void some_funct()
{
    QLibrary shell32(QString("shell32.dll"));
    ShellExecFunct viewfile = (ShellExecFunct) shell32.resolve("ShellExecuteA");

    if(viewfile)
    {
        int res = viewfile(NULL, "open", "c:\\eula.1028.txt", NULL, NULL, 5);
    }
}

This code launches the file as I want it to, but blows up immediately. The new window stays open.

I have another version like so:

typedef long (*ShellExecFunct)(long, const char*, const char*, long, long, long);

void some_funct()
{
    QLibrary shell32(QString("shell32.dll"));
    ShellExecFunct viewfile = (ShellExecFunct) shell32.resolve("ShellExecuteA");

    if(viewfile)
    {
        int res = viewfile(NULL, "open", "c:\\eula.1028.txt", NULL, NULL, 5);

        QMessageBox b;
        b.setText(QString::number(res,10));
    }
}

This code does not blow up. Notice I don't even need to call b.exec(). However, if I do call b.exec(), the value 42 is displayed.

Can someone clue me in as to what is going wrong here and what I can do to fix it?

Thanks.

Edit:

For posterity, the premise here is wrong. Qt provides the exact functionality I need without platform-specific code. Please see accepted answer.

+1  A: 

Why not just use Qt for the whole thing like this?

QDesktopServices::openUrl(QUrl("file:///c:/eula.1028.txt", QUrl::TolerantMode));

As for why you get a crash, it could be a calling convention issue. This is just a guess, but you may want to try adding WINAPI to the function pointers type. But like I said, I would just use QDesktopServices and not deal with these things directly.

Something like this:

typedef HINSTANCE WINAPI (*ShellExecutePtr)(HWND,LPCSTR,LPCSTR,LPCSTR,LPCSTR,INT);
Evan Teran
This is definitely the way to do it. Thanks. I don't know Qt very expansively yet.
San Jacinto
Thanks for your edit. It may help someone in the future.
San Jacinto
No problem, Qt offers a **lot** of functionality. It takes a long time to find all of the useful little classes i there ;).
Evan Teran