简体   繁体   English

在C#程序中使用.lib文件

[英]Using a .lib file in a C# program

I have a header file and a .lib file created using C++. 我有一个头文件和一个使用C ++创建的.lib文件。

Now I want to use it in a C# program. 现在我想在C#程序中使用它。 Is this possible? 这可能吗?

You can create a managed wrapper , see step by step instruction here: 您可以创建托管包装器 ,请参阅此处的分步说明:

http://tom-shelton.net/?p=95 http://tom-shelton.net/?p=95

There's not a traditional linker to let you import the lib. 没有传统的链接器可以导入lib。 Your best bet is to compile to a COM library and use interop to use it. 最好的办法是编译到COM库并使用interop来使用它。

Not directly. 不是直接的。 You can interoperate with unmanaged DLLs via P/Invoke, or mixed-mode assemblies using C++/CLI. 您可以通过P / Invoke或使用C ++ / CLI的混合模式程序集与非托管DLL进行互操作。 Either way, you'll have to create a wrapper project, or recompile the original .lib (if you have the sources) into DLL. 无论哪种方式,您都必须创建一个包装器项目,或者将原始的.lib(如果您有源代码)重新编译到DLL中。

I don't know about a .lib file. 我不知道.lib文件。 But I do know if you compile your code as a DLL you can consume it as unmanaged code. 但我知道如果您将代码编译为DLL,则可以将其作为非托管代码使用。

To do this you'll need to reference 要做到这一点,你需要参考

System.Runtime.InteropServices

and you will need to define the method you want to use and give it the DllImport attribute. 您需要定义要使用的方法并为其指定DllImport属性。 Something like this: 像这样的东西:

[DllImport("MyCPPDll.dll")]
public void SomeMethod(int someParameter);

Here are a few links that should help point you in the right direction: 以下是一些有助于指明正确方向的链接:

http://msdn.microsoft.com/en-us/library/26thfadc(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/26thfadc(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.100).aspx

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

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