0

I have C++ DLL as below

#include "stdafx.h"


extern "C" __declspec(dllexport)double Add(double a, double b);


extern double Add(double a, double b) 
{ 
    return a + b; 
} 

n here m trying to link this DLL with my C# app

using System.Text;

using System.Runtime.InteropServices; 

namespace test
{

    class Program

    {

        [DllImport("DLL.dll", CallingConvention = CallingConvention.Cdecl)]

        public static extern double Add(double a, double b);  


        static void Main(string[] args)

        {




            Console.WriteLine(Add(1.0, 3.0)); // error here


            Console.ReadLine(); 

        }
    }
}

m getting error:

"Unable to load DLL 'DLL.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"

please help me out ...how can i link c++ dll with c# ?

sloth
  • 99,095
  • 21
  • 171
  • 219
Balwinder SIngh
  • 1,831
  • 2
  • 24
  • 28
  • Just to avoid confusion The DLL is called "DLL.dll" right? – CloudyMarble Sep 10 '12 at 05:59
  • 1
    and also, is it in the path of built exe file? – Prasanth Sep 10 '12 at 06:00
  • yeah its just to avoid confusion.. – Balwinder SIngh Sep 10 '12 at 06:01
  • @goldenparrot- it is working if i place DLL.dll and DLL.lib file in DEBUG directory of c# app... – Balwinder SIngh Sep 10 '12 at 06:02
  • my question is what does CallingConvention = CallingConvention.Cdecl means?? and do i always need to manually place my DLL.dll and Dll.lib file into the DEBUG directory?? – Balwinder SIngh Sep 10 '12 at 06:03
  • 1
    @james, have you ensured via a load test externally (such as dependency walker) to ensure not that the .dll is present, but everything *it* depends on is likewise? The windows loader has a terrible tendency of reporting that very error message not only if LoadLibrary cannot load the .dll, but the same message if a .dll your .dll depends on is unfindable as well. You may want to check this. – WhozCraig Sep 10 '12 at 06:25
  • 1
    The reason you need to put your DLL in the debug dir is that you have not provided the path for the DLL. So, by default, it looks into the debug folder for its presence. – Shakti Prakash Singh Sep 10 '12 at 06:30
  • 1
    The application has to search for the dll somewhere, and the applications directory is one place, the system directory is the another. However, it won't start a file system wide search for something that might not even exists at all. Take a look here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx – Onkelborg Sep 10 '12 at 06:46

3 Answers3

1

The calling convention determines how function parameters are placed on the stack prior to a function invocation, and how they are removed (caller vs. callee) when the function returns. You can find out much more about this in about a million StackOverflow questions, or goto here and read up a little.

Regarding placement of the DLL within reach of the C# (aka .NET) application you're writing, I'm afraid I cannot comment on that except to say general DLL's must be in your lib-search path (PATH in Windows) the current directory, or the kernel's home directory (generally c:\windows\system32. Do NOT copy files to system32, btw. just setup your application to "run from" the directory where your DLL is residing and you should be fine. There are exceptions to this, and configuration settings that can radically alter this, but were I you i'd stick with simple for now. Complex can always come later.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
0

You will either need to plant the dll in the same location as the C# exe or pack the dll inside the exe. The first option is simple enough. For the second option, check out Embedding DLL's into .exe in in Visual C# 2010

Community
  • 1
  • 1
sohil
  • 508
  • 1
  • 3
  • 14
0

you got this error because DLL.dll wasn't in your Debug/Release Folder,

as far as i know visual studio doesn't know how to copy those files to your output folder manualy.

add the dll file to your C# solution

and then on files properties set build action to content and set copy to output directory to copy if newer this will automate the copying

Nahum
  • 6,959
  • 12
  • 48
  • 69