question

hitbuyi-2738 avatar image
0 Votes"
hitbuyi-2738 asked hitbuyi-2738 commented

how to export class instance ?

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

c++vs-debugging
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered

@hitbuyi-2738 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GuidoFranzke avatar image
0 Votes"
GuidoFranzke answered hitbuyi-2738 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

it is my mistake here, I have corrected this error

1 Vote 1 ·