2

I am trying to import a dll to a C# console application just to see if I can get a dll to work as a want, when trying this and exporting functions with C-code everything works fine and the functions can be imported in my C# application.

The problem starts when I try to add some kind of linkage to some QT methods in my unmanaged dll. I'm using DllImport to import the functions from the dll.

[DllImport("cDLL.dll", EntryPoint = "_Add@16")]
static extern double Add(double a, double b);

1 - This is how the unmanaged dll (don't look at the functionality of the code, this is just for testing purposes) looks like when it works fine.

main.cpp working

#include <stdexcept>
#include "Windows.h"

using namespace std;

extern "C" __declspec(dllexport) double __stdcall Add(double a, double b)
{
    return a + b;
}
extern "C" __declspec(dllexport) const char* getText()
{
    return "hello world";//returnBufferString.c_str();
}
BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {
    return TRUE;
}

2 - When I try to add a help function with some QT code, just an ordinary QString the DllImport starts throwing dllNotFoundException.dumpbin.exe shows all the exported functions as well after including the qt code...

main.cpp dllNotFoundException

#include <QString>
using namespace std;
class testa
{
public:
    static char* test()
    {
        QString a = "hejsan";
        return qString2Char(a);
    }
    static char* qString2Char(QString a)
    {
        return a.toUtf8().data();
    }
};

This is called from the getText() function like this:

string returnBufferString;
extern "C" __declspec(dllexport) const char* getText()
{
    returnBufferString = testa::test();
    return returnBufferString.c_str();
}

When I try to access the dll from DllImport I get dllNotFoundException in the 2:nd part. How do I solve this? have I missed any dependencies or anything. My dll is build using msvc2010 compiler and the .pro file looks like this:

cDLL.pro

TEMPLATE = lib
 CONFIG  += dll

QT += core

 # Input
 SOURCES += main.cpp

I'm stuck...

Simon Nielsen
  • 105
  • 1
  • 13

2 Answers2

2

It doesn't tell you exactly what DLL it cannot find. Which is almost surely not your DLL, it is one of the DLLs that QT requires. You'd have to copy them to the EXE folder as well. If you have no idea and can't find it in the Nokia documentation then you can find out with SysInternals' ProcMon utility.

However, in this scenario you surely want to link QT into your DLL since the odds that those DLLs can be shared are small. Use this SO question for guidance in setting up your QT project.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

You need to put the DLL in the same folder as your executable.

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586%28v=vs.85%29.aspx

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197