1

I have created an MFC DLL and have exported the functions for example in the file SerialPort.h:

class CSerialPortApp : public CWinApp
{
public:
    CSerialPortApp();

    __declspec(dllexport) int SWrite(unsigned char* toSend, int len);
};

and in my MFC application I want to call the function in SerialInterface.h I have included the "SerialPort.h" from the DLL and called:

__declspec(dllimport) int SWrite(unsigned char* toSend, int len);

class SerialInterface
{
public:

};

for example.

I have added the SerialPort.lib file to my linker includes but when I try to compile I get

error LNK2001: unresolved external symbol "__declspec(dllimport) int __cdecl SWrite(unsigned char*, int)" (__imp_?SWrite@@YAHPAEH@Z)

I am stuck as to the cause of this, I have tried rebuilding everything but nothing seems to help?

Thank you for any help!

Lee Worbey
  • 85
  • 1
  • 10

2 Answers2

1

You are using __declspec(dllexport) inside a class?

You either export global functions from the dll or whole class which may contain any functions. You don't have to export selected member functions from a class, I don't even know how that works.

It is a little strange that you are not properly exporting the SerialPort class from dll (as per your code) yet you can use it in your application and call its member function!? I am a little confused.

zar
  • 11,361
  • 14
  • 96
  • 178
  • Yes true however I do not want to export all the variables in the class so this way only allows access to the functions I want... or at least I think that is how it works! – Lee Worbey Oct 07 '11 at 10:46
0

Well I found an alternative that works, I believe I was implementing it incorrectly.

I added a new class to my DLL that was not a CWinApp class:

class SerialPort
{
public:
    __declspec(dllexport) SerialPort(void);
    __declspec(dllexport) virtual ~SerialPort(void);

    __declspec(dllexport) int SWrite(unsigned char* toSend, int len);
};

then included the header for this in my application and the lib and dll etc.

I then placed the included header file in the main CDialog header but importantly didn't need to import any of the functions:

#include "SerialPort.h"

class CPPUDlg : public CDialog
{
public:
    CPPUDlg(CWnd* pParent = NULL); // standard constructor

    SerialPort objSerialPort;

and then in my code I simply call

objSerialPort.SWrite(toSend, len);

I have not used dllimport to import the functions which I assumed I would need to but it now works!

Hope this helps anyone who may have a similar problem.

Lee Worbey
  • 85
  • 1
  • 10
  • I know this a really old answer and chances are you already know this, but you don't have to add ```__declspec(dllexport)``` to each method, you could just add this after the ```class``` keyword and then it automatically exports all of its methods. – Jakub Cieślak Sep 12 '22 at 13:46