Answer the question
In order to leave comments, you need to log in
How do I import class methods when compiling a C++11 dll?
Hello, please show me how to write the following problem. I successfully assembled the dll file and called the printing function from it. At this stage, everything works, the function is called, everything is ok.
Code:
Project2.h
#ifdef PROJECT2_EXPORTS
#define PROJECT2_API /*extern "C"*/ __declspec(dllexport)
#else
#define PROJECT2_API __declspec(dllimport)
#endif
PROJECT2_API void printing(char* pChar);
// Project2.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "Project2.h"
#include "Header1.h"
PROJECT2_API void printing(char* pChar) {
char a[] = "Example String";
strcpy_s(pChar, strlen(a) + 1, a);
}
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "Header1.h"
int iParam;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
iParam = 7;
break;
case DLL_THREAD_ATTACH:
iParam += 1;
break;
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
const int GetSomeParam() {
return iParam;
}
#ifdef PROJECT2_EXPORTS
#define PROJECT2_API /*extern "C"*/ __declspec(dllexport)
#else
#define PROJECT2_API __declspec(dllimport)
#endif
class xyz
{
private:
int abc;
public:
PROJECT2_API void printing(char* pChar) {
char a[] = "Example String";
strcpy_s(pChar, strlen(a) + 1, a);
abc = 5;
}
PROJECT2_API int printing_int() { // return 5
return abc;
}
};
Answer the question
In order to leave comments, you need to log in
Why, when working with procedures, did you put only the function declaration in the .h file, but in the case of classes, you stuffed the entire implementation? It is necessary to separate the flies from the cutlets. Methods can also be implemented separately from the class declaration by specifying their fully qualified name, for example
// xyz.h
class xyz
{
private:
int abc;
public:
PROJECT2_API int printing_int();
};
//xyz.cpp
PROJECT2_API int hyz::printing_int() { // return 5
return abc;
}
void printing(char* pChar) {
char a[] = "Example String";
strcpy_s(pChar, strlen(a) + 1, a);
}
int main() {
char[4] buff = {0};
printing(buff);
printf("%s\n", buff);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question