Kiva
Kiva

Reputation: 53

How to create a DLL library in C++ and then use it in C project VisualStudio?

How to create a DLL library in C++ and then use it in C project VisualStudio(2015) ?

I've seen only 1 question similar to my problem ,but I couldn't understand too much from it.

I've seen lots of tutorials on how to use .dll written in C++ into another C++ project, a C .dll used in C#, but no example of how to use a C++ .dll into a C VS project. I really need help, I've searched all over the internet, tried all kind of 'solutions' to my problem, still, without any solution.

I really need your help on this one.

The C++ dll Project has the following content :

//C++ dll Header Library, having the name "dllDelay.h":

#include <iostream>
#include <stdio.h>
#include <windows.h>

extern "C" __declspec(dllexport) void dllDelay(DWORD dwMsec);

#endif

//C++ .cpp file named "dllDelay.cpp":

#include "dllDelay.h"
#include "stdafx.h"

extern "C" __declspec(dllexport)

void dllDelay(DWORD dwMsec) {
Sleep(dwMsec);  
}

The C VisualStudio(2015) Project has the following content :

/*This function is intended to Delay 10 seconds, measure that elapsed time
and write it into a file. I've checked this function using Sleep() instead of
dllDelay() and it worked fine, so this function has no errors.*/

#include "dllDelay.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>


int main(void)
{
 FILE *write1;
 // measure elapsed time on windows with QueryPerformanceCounter() : 
 LARGE_INTEGER frequency;        // ticks per second
 LARGE_INTEGER t1, t2;           // ticks
 double elapsedTime;
 write1 = fopen("write_difftime_1Test.txt", "w");
 fprintf(write1, "\n Sleep : 10000ms = 10s");

 time_t start, stop;
 clock_t ticks;
 long count;
 double i = 0, v = 0, j = 0;

  //make 10 such measurements and write them into file
 while ((j < 10) && ((write1 != NULL) && (TRUE != fseek(write1, 0L, SEEK_END))))
 {
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&t1);
    time(&start);

    //The function from dll
    dllDelay(10000);        

    time(&stop);
    QueryPerformanceCounter(&t2);

    // compute and print the elapsed time in millisec
    elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
    fprintf(write1, "\n Elapsed time : %lf s.  timeDiff time: %f in seconds\n\n", elapsedTime / 1000, difftime(stop, start));

    j++;        
}
fclose(write1);

return 0;

} `

This function is intended to Delay 10 seconds, measure that elapsed time and write it into a file. I've checked this function using Sleep() instead of dllDelay() and it worked fine, so this function has no errors.

But when I use #include "dllDelay.h" I get 3111 Errors such as :

I built the dll (in a dll project, of course), copied the .dll file into the C Project folder where the exe is found, I added to the solution Explorer the .lib file and got these errors.

I really neeed your help, I've looked everywhere and did not found a guide or anything regarding the usage of a C++ .dll used in a C project. :|

Thank you for your time.

Upvotes: 2

Views: 4369

Answers (1)

Matthias Gr&#252;n
Matthias Gr&#252;n

Reputation: 1496

In your C++ DLL project open the project properties and define a preprocessor symbol:

Project properties screenshot

Then in your header file, define another symbol based on whether the preprocessor symbol circled in red is defined or not:

#ifdef CPPDLL_EXPORTS
#define DLLIMPORT_EXPORT __declspec(dllexport)
#else
#define DLLIMPORT_EXPORT __declspec(dllimport)
#endif

Then use that defined symbol in front of your function declaration:

DLLIMPORT_EXPORT void dllDelay(DWORD dwMsec);

This has the following effect:

In your DLL project, the symbol (DLLIMPORT_EXPORT) is defined. Thus, DLLIMPORT_EXPORT will evaluate to __declspec(dllexport). In your C project, which consumes the DLL, the symbol will not be defined. Ergo, DLLIMPORT_EXPORT evaluates to __declspec(dllimport) when the header file is included. Doing this will import the function and you will be able to use it. Failure to do so will result in a Linker error (unresolved external symbol) when trying to call the function.

Hope this helps!

PS: You should move all #includes that aren't needed in your header file to your implementation (CPP) file :-)

Upvotes: 1

Related Questions