0

I'm creating a DLL.

my DLL is calling a function passDTMFToClient(int id,DTMF_01 packet) where DTMF_01 is a structure;

and that function definition is present in the other file, outside of DLL where I'm going to reference this dll.

I do not want to move that function to my dll. Thus how can I call it from dll?

For e.g.

  • DLL :
void DTMF() { passDTMFToclient(int id,DTMF_01 packet); } 

Another Project's file Where the definition is available and where the dll will be referenced.

  • Demo.cpp :
void passDTMFToClient(int id,DTMF_01 packet) { . . } 
1
  • Take a look at exporting functions from a dll ( declspec dllexport ). If you have done that you can use the dll vie loadlibrary or by linking to the lib created together with the dll as you would do with a static lib Commented Jul 9, 2019 at 6:30

1 Answer 1

2

DLL.h

#pragma once #ifdef _WIN32 # ifdef BUILDING_MY_LIB # define MY_EXPORT __declspec(dllexport) # else # define MY_EXPORT __declspec(dllimport) # endif #else # define MY_EXPORT __attribute__ ((visibility ("default"))) #endif // declare as dll method MY_EXPORT void passDTMFToClient(int id, DTMF_01 packet); 

Ensure that header is included by both you dll.cpp and exe.cpp. Ensure in your dll build you define 'BUILDING_MY_LIB' when compiling on windows. When compiling the dll you should also get a link library (e.g. you'll have mydll.dll and mydll.lib on windows).

Your exe now just need to include the dll.h, and link to the mydll.lib (or mydll.so on linux). If your app can find the dll, it will automatically be loaded.

If you wish to go one step further and load the dll on the fly into your exe, you'll probably want to change the function prototype to:

MY_EXPORT extern "C" void passDTMFToClient(int id, DTMF_01 packet); 

Which will disable name mangling. You should be able to open the DLL using LoadLibrary on windows, or dlopen on linux. e.g.

// on windows HMODULE dllHandle = LoadLibrary("mydll.dll"); // on linux void* dllHandle = dlopen("mydll.dll", RTLD_NOW); 

You will need a function pointer, e.g.

// typedef func type typedef void (*passDTMFToClient_func)(int, DTMF_01); // the function pointer passDTMFToClient_func passDTMFToClient = 0; 

You should then just need to get the symbol address, which you do like so on windows:

passDTMFToClient = (passDTMFToClient_func)GetProcAddress(dllHandle, "passDTMFToClient"); 

And like so on linux:

passDTMFToClient = (passDTMFToClient_func)dlsym(dllHandle, "passDTMFToClient"); 

And when you're done with the DLL, you can close it again:

// windows FreeModule(dllHandle); // linux dlclose(dllHandle); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.