How to import DLL file instead of project to C++?

Using the DLL codes here https://msdn.microsoft.com/en-us/library/ms235636.aspx?f=255&MSPPError=-2147217396

Included here for convenience...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// MathLibrary.h - Contains declaration of Function class  
#pragma once  

#ifdef MATHLIBRARY_EXPORTS  
#define MATHLIBRARY_API __declspec(dllexport)   
#else  
#define MATHLIBRARY_API __declspec(dllimport)   
#endif  

namespace MathLibrary  
{  
    // This class is exported from the MathLibrary.dll  
    class Functions  
    {  
    public:  
        // Returns a + b  
        static MATHLIBRARY_API double Add(double a, double b);  

        // Returns a * b  
        static MATHLIBRARY_API double Multiply(double a, double b);  

        // Returns a + (a * b)  
        static MATHLIBRARY_API double AddMultiply(double a, double b);  
    };  
}  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// MathLibrary.cpp : Defines the exported functions for the DLL application.  
// Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp  

#include "stdafx.h"  
#include "MathLibrary.h"  

namespace MathLibrary  
{  
    double Functions::Add(double a, double b)  
    {  
        return a + b;  
    }  

    double Functions::Multiply(double a, double b)  
    {  
        return a * b;  
    }  

    double Functions::AddMultiply(double a, double b)  
    {  
        return a + (a * b);  
    }  
}  


How to make the application project import the dll file instead of from the project?

I gather that I need to generate a .lib file.

However, the command `TlbExp.exe MyDLL.dll` returns the following message:

TlbExp : error TX0000 : Could not load file or assembly 'file:///C:\...\MyDLL.dll' or one of its dependencies. The module was expected to contain an assembly manifest.

So how to resolve it?
When you build a DLL, it generates the DLL and an import library, a .lib file. You link with the .lib file.
as kbw says, that cpp code belongs in a DLL project which will build the dll.

a different project can reference it, either by specifying include/library paths to the build tools.

Or, if you are using MSVC you can use #import https://msdn.microsoft.com/en-us/library/8etzzkb6.aspx
.lib is a static library where functions and procedures can be placed and called as the application is being compiled. A DLL does the same function but is dynamic in a sense that the application can call these libraries during run-time and not during the compilation. You don't need .lib to to use the dll.
You just go to the project you want to use the dll and
right click -> add -> reference (then locate the dll from file chooser and you are good to go)

Once you get the dll in than the rest of your code should compile OK. Just make sure to use the same namespace.(I used to do this mistake a lot) Once again if you need static library at compile time then you can generate .lib file, then you have to compile it from the dll project.
Topic archived. No new replies allowed.