M
M
MasterCopipaster2021-06-01 00:46:20
C++ / C#
MasterCopipaster, 2021-06-01 00:46:20

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);


project.cpp
// 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
// 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;
}

Example for quick reproduction for Visual St...

I want to write a class for dll so that I can call methods in this class.

Project2.h

#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;
  }
};


Methods for dll are called sequentially from the beginning of printing then printing_int property abc should not change between these calls (in general, everything is as usual) printing_int is expected to return 5.

I don't know how to export class methods to dll - but I found from this link that you can ... compilation with the new Project2.h was successful, however, when you try to call printing from the dll, it cannot be found (there is no such function). Can anyone write an example of how I can export class methods to dll?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2021-06-01
@MasterCopipaster

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;
}

PS I'm very interested in how your code will work in this case.
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 question

Ask a Question

731 491 924 answers to any question