54
votes

I am trying to create an application in visual studio that will be able to access a .dll file that already exists. I need the application to call up routines. I also have a header file that already exists.

I have been researching on the internet and have found that I need to create a .lib file. Looking at similar questions on here I found a link: http://support.microsoft.com/kb/131313 I cannot however follow the directions.

The information in the link says to make a DEF file ( I read elsewhere that this needs to be compiled as a DLL with the same name, but not sure what that name is, the same name as the .dll file?). But I do not understand the first direction, to 'Use DUMPBIN /EXPORTS'. I then need to 'stub out' functions, and then something to do with .OBJ files (I do not know what these files are).

Are there any step-by-step directions, similar to the link above, that are easy to follow?

4
With what compiler do you want to use the resulting lib with? Visual Studio? Which version?David Feurle
I am aiming to use Microsoft Visual studio 2010 to be the application to use between the DLL and Matlab. The DLL is called Wintab32, it is found when you use a graphics tablet.hde

4 Answers

80
votes

You're going to need Microsoft Visual C++ 2010 Express (or any other source of MSVC command line tools), and your DLL.

Steps:

  1. dumpbin /EXPORTS yourfile.dll > yourfile.exports
  2. Paste the names of the needed functions from yourfile.exports into a new yourfile.def file. Add a line with the word EXPORTS at the top of this file.
  3. Run the following commands from VC\bin directory (the one where lib.exe and other compile tools reside).

 

 vcvars32.bat

 lib /def:yourfile.def /out:yourfile.lib

or for x64 builds

 lib /def:yourfile.def /machine:x64 /out:yourfile64.lib

You should get two files generated: yourfile.lib and yourfile.exp

21
votes

You can use Digital Mars's IMPLIB tool. It can create a lib file using only the dll, without any need for a .def file.

The download link is http://ftp.digitalmars.com/bup.zip.

The command line is:

implib.exe /s mydll.lib mydll.dll
-1
votes

I might have the answer. I did this when I was creating a .exe console application that needed a .dll file. I ran into the problem as well. When I tried the IMPLIB application, it couldn't find any export files. You need to add an #ifdef FILENAME_EXPORTS (replace FILENAME with your .dll file name) and create an _API. Here is the code for the #ifdef export api commands:

#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

Now that you have the Export API defined, you need to apply it to all the functions in your header file in the .dll project. For example:

void FILENAME_API function();

Declare your export functions normally, but include the API between the declarer type and the function name.

For defining the function in the .cpp file in the .dll project, you don't need the API in the declaration.

Here is an example of filename.h and filename.cpp with all the code.

// Code for filename.h
#pragma once

// Define your Export API
#ifdef FILENAME_EXPORTS
#define FILENAME_API __declspec(dllexport)
#else
#define FILENAME_API __declspec(dllimport)
#endif

// Declare your functions with your API

void FILENAME_API function1();
void FILENAME_API function2();
void FILENAME_API function3();

-------------------------------------------------------------------------------------------
// Code for filename.cpp
#include <iostream>
#include "pch.h"
#include "filename.h"
using namespace std;

void function1()
{
    cout << "Hello Function1!";
}

void function2()
{
    cout << "Hello Function2!";
}

void function3()
{
    cout << "Hello Function3!";
}

Now when you compile the project, you should see the .dll, .lib, and .exp files in the folder where the compiled files are saved to. Now you can link the .exe file with the .lib file. You're Welcome!

-4
votes

Instead of creating .def, you can create .lib file from .dll file by exporting the functions / classes defined in the .dll file by __declspec(dllexport) which were referred in the application code.

For example (Pseudo code)

PROJECT for creating X.dll file (say, X is a dll name):

A.h:

// Function declaration
__declspec(dllexport) void  foo(void);

A.cpp:

// Function definition 
#include <A.h>
void foo(void) {
; // definition
}

If you build the above dll project in Visual Studio then the compiler will generate X.dll and also X.lib [which has exported function foo by __declspec(dllexport) ].

App.cpp:

// Load time dynamic linking:
// Application should include X.lib (not X.dll) in the project setting
 #include <A.h>
 int main() {
 foo();
 return 0;
}

For further study please refer the following links for better understanding:

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL#CppMatureApproach

http://msdn.microsoft.com/en-us/library/ms686923(v=vs.85).aspx