1

I tried to create a dll with Visual C++ 2012 by following this walkthrough, but I failed. When I tried to import built dll in a different project as a reference, I got this error: A reference to '***.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

I don't understand what my mistake is in code and why I get this error. Here is my header and cpp file:

DLLEXPORT.H

__declspec(dllexport) bool setMute();
__declspec(dllexport) bool setActive();

DLLEXPORT.CPP

#include "DLLEXPORT.H"
bool setMute(){
    //some stuff
}
bool setActive(){
    //some stuff
}

Moreover, I also tried to solve the problem by this solution, but I got this error: TlbImp : error TI1002 : The input file '****.dll' is not a valid type library.

Community
  • 1
  • 1
haitaka
  • 1,832
  • 12
  • 21
  • add also this names into file with exported definitions – 4pie0 Apr 03 '13 at 13:03
  • 3
    Your DLL is not a .NET assembly and not a COM server so adding it as a reference cannot work. You will need to use [DllImport] to use it. – Hans Passant Apr 03 '13 at 13:04
  • 2
    Dll created by such way should be added to a client C/C++ project using linker additional dependencies, and not as .NET or COM reference. You need to add .lib file to the linker list. – Alex F Apr 03 '13 at 13:05
  • 1
    Or you can add DLL project to the same solution and then add reference to DLL project from your target project. – Mladen Janković Apr 03 '13 at 13:09
  • Sounds to me like a more accurate title would be something like: "How do I use a DLL from managed code?" – Jerry Coffin Apr 03 '13 at 13:10
  • Even though you are making and using a DLL, when doing the link you give the linker the .LIB file. Also the using code needs to use __declspec(dllimport) for your symbols. – brian beuning Apr 03 '13 at 13:42
  • Thank you for your comments. As you said, I have to use [DllImport]. – haitaka Apr 03 '13 at 13:46

1 Answers1

3

There are different kinds of DLLs (and EXEs) in Microsoft world: native DLLs, prodoced by native C++ (or C) and assemblies, containing .NET executable stuff. The walkthrough you have read will give you a native DLL, while the referencing you try to do is meant only for .NET assemblies (and COM components).

So you either have to build a .NET DLL (which would be C++/CLI and not native C++), or link your native DLL to a native app (or importing it) instead of referencing it in a .NET project.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90