2012-09-13 54 views
0

我有一个.c和修改为CPP应用程序中使用.h文件中,其实他们有如何使用C DLL C#程序

#ifdef __cplusplus 
     extern "C"{ 
#endif 

prepocessor线。我想知道如何以及如何使用c#程序中定义的函数。也许我必须为这段代码创建一个dll?

+4

搜索词是“PInvoke”。 [启动DLL的使用C#程序]的 –

+0

可能重复(http://stackoverflow.com/questions/4877157/launch-dll-using-c-sharp-program) – rkosegi

回答

1

你们似乎并没有得到它 - 他没有一个DLL,他有C++源文件:

你有两个选择

  1. 翻译C++代码到C#,并直接将其纳入您的应用程序

  2. 使用C++编译器从源文件创建一个DLL和使用的PInvoke到CCESS它

至于是否需要修改C++代码来创建DLL或使之从C#accessable,我们无法知道不完整的代码的发布方式。

+0

我的答案假设他会尝试#2这并不罕见。 –

1

下面是从MSDN采取一个简单的例子:

using System; 
using System.Runtime.InteropServices; 

class PlatformInvokeTest 
{ 
    [DllImport("msvcrt.dll")] 
    public static extern int puts(string c); 
    [DllImport("msvcrt.dll")] 
    internal static extern int _flushall(); 

    public static void Main() 
    { 
     puts("Test"); 
     _flushall(); 
    } 
} 

假定你愿意编译你的现有代码到.DLL

+0

所以我要创建一个从.c和一个dll。 h文件?我有没有在创建dll之前修改代码? – Ant4res