简体   繁体   English

C#中的C ++ dll

[英]C++ dll within c#

I have C++ DLL as below 我有如下的C ++ DLL

#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 n在这里我试图将此DLL与我的C#应用​​程序链接

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)" “无法加载DLL'DLL.dll':找不到指定的模块。(HRESULT异常:0x8007007E)”

please help me out ...how can i link c++ dll with c# ? 请帮帮我...如何将C ++ DLL与C#链接?

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. 您可以在大约一百万个StackOverflow问题中找到有关此问题的更多信息,或者转到此处阅读一些内容。

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. 关于您正在编写的C#(aka .NET)应用程序所能访问的DLL的位置,恐怕我无法对此发表评论,除非说通用DLL必须位于当前的lib-search路径(Windows中的PATH)中。目录,或内核的主目录(一般为C:\\ Windows \\ System32下不将文件复制到system32下,顺便说一句只安装您的应用程序到您的DLL驻留在目录“从运行”,你应该罚款也例外为此,以及可以从根本上改变这一点的配置设置,但是现在我还是坚持简单,复杂总是会在以后出现。

You will either need to plant the dll in the same location as the C# exe or pack the dll inside the exe. 您将需要将dll植入与C#exe相同的位置,或者将dll打包在exe中。 The first option is simple enough. 第一种选择很简单。 For the second option, check out Embedding DLL's into .exe in in Visual C# 2010 对于第二个选项,请在Visual C#2010中签出将DLL嵌入到.exe中。

you got this error because DLL.dll wasn't in your Debug/Release Folder, 您收到此错误,因为DLL.dll不在您的调试/发布文件夹中,

as far as i know visual studio doesn't know how to copy those files to your output folder manualy. 据我所知,Visual Studio不知道如何手动将这些文件复制到输出文件夹。

add the dll file to your C# solution 将dll文件添加到您的C#解决方案

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 然后在files properties上将build action设置为content ,并将copy to output directory设置copy to output directorycopy if newer copy to output directorycopy if newer则将自动进行复制)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM