Calling classes from DLLs
-
I have been having problems trying to set up a plugin for my program. The program loads a DLL dynamically, and then calls a member function:
__declspec(dllexport) int Start(CDervDlg *dlg)
{
dlg->m_SomeVar = TRUE; //Works fine
dlg->SomeFunc("Blah", "Blah"); //Link error: unresolved external symbol.
}I've tried using both regular DLLs and Extension DLLs, but neither work. This is probably just something obvious that I just can't see, but can anyone help?
-
I have been having problems trying to set up a plugin for my program. The program loads a DLL dynamically, and then calls a member function:
__declspec(dllexport) int Start(CDervDlg *dlg)
{
dlg->m_SomeVar = TRUE; //Works fine
dlg->SomeFunc("Blah", "Blah"); //Link error: unresolved external symbol.
}I've tried using both regular DLLs and Extension DLLs, but neither work. This is probably just something obvious that I just can't see, but can anyone help?
if SomeFunc is on the DLL, you have to dllexport it when generating the DLL and dllimport when generating the EXE. If it is an MFC extension DLL, you can use the AFX_EXT_CLASS macro, for example: class AFX_EXT_CLASS CMyDlg : public CDialog { }; Jaime
-
if SomeFunc is on the DLL, you have to dllexport it when generating the DLL and dllimport when generating the EXE. If it is an MFC extension DLL, you can use the AFX_EXT_CLASS macro, for example: class AFX_EXT_CLASS CMyDlg : public CDialog { }; Jaime
Somefunc is in the EXE.
///EXE code
class CDervDlg : public CDialog
{
void TestLoad();
void SomeFunc(CString, CString);BOOL m\_SomeVar;
};
typedef int (*SCGptr)(CDervDlg *);
void TestLoad()
{
HINSTANCE inst = AfxLoadLibrary("testlib.dll");
SCGptr Startfunc = (SCGptr)GetProcAddress(inst, "?Start@@YAHPAVCDervDlg@@@Z");
if (Startfunc != NULL)
Startfunc(this);
}///DLL code
#include "DervDlg.h"__declspec(dllexport) int Start(CDervDlg *dlg)
{
dlg->m_SomeVar = TRUE; //Works fine
dlg->SomeFunc("Blah", "Blah"); //Link error: unresolved external symbol.
}Thank you for helping.
-
Somefunc is in the EXE.
///EXE code
class CDervDlg : public CDialog
{
void TestLoad();
void SomeFunc(CString, CString);BOOL m\_SomeVar;
};
typedef int (*SCGptr)(CDervDlg *);
void TestLoad()
{
HINSTANCE inst = AfxLoadLibrary("testlib.dll");
SCGptr Startfunc = (SCGptr)GetProcAddress(inst, "?Start@@YAHPAVCDervDlg@@@Z");
if (Startfunc != NULL)
Startfunc(this);
}///DLL code
#include "DervDlg.h"__declspec(dllexport) int Start(CDervDlg *dlg)
{
dlg->m_SomeVar = TRUE; //Works fine
dlg->SomeFunc("Blah", "Blah"); //Link error: unresolved external symbol.
}Thank you for helping.
In DLL you have to declare: class __declspect(dllimport) CDervDlg : public CDialog { void TestLoad(); void SomeFunc(CString, CString); BOOL m_SomeVar; }; and in EXE: class __declspect(dllexport) CDervDlg : public CDialog { void TestLoad(); void SomeFunc(CString, CString); BOOL m_SomeVar; }; In your DLL you are using a function declared in the EXE, that's why you need to export from the EXE, and import it into the DLL. Jaime
-
In DLL you have to declare: class __declspect(dllimport) CDervDlg : public CDialog { void TestLoad(); void SomeFunc(CString, CString); BOOL m_SomeVar; }; and in EXE: class __declspect(dllexport) CDervDlg : public CDialog { void TestLoad(); void SomeFunc(CString, CString); BOOL m_SomeVar; }; In your DLL you are using a function declared in the EXE, that's why you need to export from the EXE, and import it into the DLL. Jaime