Share via

how to export class instance ?

hitbuyi 46 Reputation points
2021-01-08T02:58:55.143+00:00

I create a dll project in vs2015, include 2 modules (2 projects)

project 1

in testDLLint.h

ifdef TESTDLLINT_EXPORTS

define TESTDLLINT_API __declspec(dllexport)

else

define TESTDLLINT_API __declspec(dllimport)

endif

class TESTDLLINT_API CtestDLLint {
public:
CtestDLLint(void);
public:
int m;
};
extern __declspec(dllexport) int myInt;
extern __declspec(dllexport) CtestDLLint myInst;

in file1.c

include "testDLLint.h"

__declspec(dllexport) int myInt; = 0;
__declspec(dllexport) CtestDLLint myInst;

TESTDLLINT_API int fntestDLLint(void) {
return 42;
}

CtestDLLint::CtestDLLint() {
return;
}

project 2

file1.c

pragma comment(lib,"../../Debug/testDLLint.lib")

extern __declspec(dllimport) int myInt; // good
extern __declspec(dllexport) CtestDLLint myInst;

int main(){
myInt = 10; // access myInt from module 1 is ok
myInst.m = 10; // wrong
}

my problem is

  1. accessing myInt from dll of module 1 is ok
  2. acessing class instance myInst from dll of module 1 is not ok

for accessing class's instance from dll, is it possible? if possible, how to do it ?

thanks a lot

Developer technologies | Visual Studio | Debugging
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

RLWA32 52,571 Reputation points
2021-01-08T13:30:23.523+00:00

@hitbuyi You have totally missed the purpose of the conditional compilation statements

#ifdef TESTDLLINT_EXPORTS  
#define TESTDLLINT_API __declspec(dllexport)  
#else  
#define TESTDLLINT_API __declspec(dllimport)  
#endif  

In your DLL project in which TESTDLLINT_EXPORTS is defined the preprocessor resolves TESTDLLINT_API to __declspec(dllexport).

In other projects that consume the dll and for which TESTDLLINT_EXPORTS is NOT defined the preprocessor resolves TESTDLLINT_API to __declspec(dllimport).

So the header that declares exported classes, functions and variables should use TESTDLLINT_API, instead of __declspec(dllexport).

For example,

class TESTDLLINT_API CtestDLLint {  
public:  
CtestDLLint(void);  
public:  
int m;  
};  
extern TESTDLLINT_API int myInt;  
extern TESTDLLINT_API CtestDLLint myInst;  

So the same header can be used for the DLL project and other projects that consume DLL exports.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Guido Franzke 2,191 Reputation points
    2021-01-08T07:06:26.03+00:00

    Hello,
    in file1.c you import myInt but use dllexport with class myInst:

    pragma comment(lib,"../../Debug/testDLLint.lib")
    
    extern __declspec(dllimport) int myInt; // good
    extern __declspec(dllexport) CtestDLLint myInst;     // <- why dllexport?
    
    int main(){
    

    I think, you must use dllimport with the class instance.

    Read this: 399465.aspx

    Regards, Guido

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.