从C#程序的DLL访问一个方法 [英] Access a method from a DLL from C# program

查看:89
本文介绍了从C#程序的DLL访问一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C程序,我已经创建了一个DLL文件。我正在使用Windows Vista和Visual C ++。



现在我需要从C#代码的Main()方法访问该DLL的方法。这样做的步骤是什么?



到目前为止,我已经添加了DLL文件作为参考,之后该怎么办?



这只是一个例子:

  int main1(void){
prinf(Hello World );
}

请注意,这个课程也使我们有其他.lib功能,但我能够成功地创建一个DLL。 (我不知道这是否相关)



现在我需要从我的C#Main();

  [STAThread] 
static void Main()
{
//我需要在这里调用main1()方法

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}


解决方案

请参阅使用c#代码中的c ++ dll中定义的类,它具有一个很好的接受答案。正如Hans Passant在评论中写道,你不能添加一个本机DLL作为C#项目的参考。



当我引用我自己的一个本机DLL时,我通常在C#项目和生成本机DLL的项目之间添加一个依赖关系,或者我将这个DLL作为链接的内容文件添加到C#项目中,如下所示:


  1. 右键单击项目,然后选择添加>现有项目。

  2. 浏览并选择所需的DLL,但不要单击添加

  3. 点击添加按钮右侧的小箭头,然后选择添加为链接。 li>
  4. 选择现在出现在C#项目中的DLL,然后转到其属性。

  5. 确保您具有构建操作设置到内容

这将把DLL复制到 bin \Debug 文件夹的C#项目,并确保,如果你曾经决定创建一个S etup项目,您可以轻松地引用所有的内容文件并将它们包含在Microsoft Installer程序包中。



现在,可以看到函数写在你的本机DLL中,你必须照顾它们的导出(参见导出用于C或C ++语言可执行文件的C函数使用__declspec(dllexport)从DLL导出)。所以你必须在函数声明周围添加一个 externC块(我假设你在一个 .cpp 源代码中编写了你的​​代码文件,这意味着如果您没有将它们声明为 externC),那么编译器将会发出 mangled 函数名:

  externC
{
__declspec(dllexport)void __cdecl Foo(const char * arg1);
}

...

void Foo(const char * arg1)
{
printf(Hello%s!, ARG1);
}

__ declspec(dllexport)装饰意味着编译器/链接器必须使该函数从DLL外部可见。而 __ cdecl 定义了参数将如何传递给函数(标准的C方式)。



在您的C#代码中,您必须参考您的DLL的导出方法:

  class Program 
{
[DllImport(mydll.dll)]
内部静态extern void Foo(string arg1);

static void Main()
{
Program.Foo(Pierre);
}
}

您应该阅读平台调用教程,它提供了所有的血清细节。


I have a C program and i have created a DLL file. I am using Windows Vista, and Visual C++.

Now I need to access a method from that DLL, from the Main() method of a C# code. What are steps of doing so?

So far I have added the DLL file as reference, thereafter what should I do?

This is only an example:

int main1( void ) {
  prinf("Hello World");
}

Please note that this class also makes us of other .lib functions, but i was able to successfully create a DLL out of it. (I don't know if this is relevant)

Now i need to access this method from my C# Main();

[STAThread]
static void Main()
{
  // I need to call that main1() method here

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

解决方案

See using a class defined in a c++ dll in c# code which has a great accepted answer. And as Hans Passant wrote in the comments, you cannot add a native DLL as a reference to a C# project.

When I refer to one of my own native DLLs, I usually either add a dependency between the C# project and the project which generates the native DLL, or I add the DLL as a linked content file in the C# project, like so:

  1. Right-click on the project and choose Add > Existing Item.
  2. Browse and select the DLL you want, but don't click Add yet.
  3. Click on the tiny arrow at the right of the Add button and select Add As Link.
  4. Select the DLL that now appears in your C# project and go to its properties.
  5. Make sure you have Build Action set to Content.

This will copy the DLL to the bin\Debug folder of the C# project and make sure that, if you once decide to create a Setup project, you can easily reference all content files and include them in the Microsoft Installer package.

Now, to be able to see the functions written in your native DLL, you have to take care of exporting them (see Exporting C Functions for Use in C or C++ Language Executables and Exporting from a DLL Using __declspec(dllexport)). So you'd have to add an extern "C" block around your function declaration (I am assuming you wrote your code in a .cpp source file and this means that the compiler will emit mangled function names if you do not declare them as being extern "C"):

extern "C"
{
    __declspec (dllexport) void __cdecl Foo(const char* arg1);
}

...

void Foo(const char* arg1)
{
    printf ("Hello %s !", arg1);
}

The __declspec (dllexport) decoration means that the compiler/linker will have to make the function visible from outside of the DLL. And the __cdecl defines how parameters will be passed to the function (the standard "C" way of doing this).

In your C# code, you'll have to refer to your DLL's exported methods:

class Program
{
    [DllImport("mydll.dll")]
    internal static extern void Foo(string arg1);

    static void Main()
    {
        Program.Foo ("Pierre");
    }
}

You should read the Platform Invoke Tutorial which gives all the gory details.

这篇关于从C#程序的DLL访问一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆