简体   繁体   English

如何为 static 库创建 C++ 或 C++/CLI 包装器以在 C# 中使用

[英]How to create a C++ or C++/CLI wrapper for a static lib to use in C#

I only have a lib file and its header file (static library?) I want to use this in C#.我只有一个 lib 文件及其 header 文件(静态库?)我想在 C# 中使用它。 From googling I have come to the conclusion I need to write a wrapper dll that C# will be able to use.通过谷歌搜索,我得出结论,我需要编写一个 C# 能够使用的包装器 dll。

I have tried writing this both in C++ and C++/CLI but when I run my program I get a error stating that我已经尝试在 C++ 和 C++/CLI 中写这个,但是当我运行我的程序时,我得到一个错误,指出

Unable to load DLL 'foo.dll': The specified module could not be found.无法加载 DLL 'foo.dll':找不到指定的模块。 (Exception from HRESULT: 0x8007007E) (来自 HRESULT 的异常:0x8007007E)

I have already used a dependency checker and my dependencies are fine so I'm assuming my dll setup is incorrect.我已经使用了依赖项检查器并且我的依赖项很好,所以我假设我的 dll 设置不正确。

I am only trying to use functions from the lib not classes.我只是想使用库中的函数而不是类。

Here is an example of my cs file这是我的cs文件的示例

using System;
using System.Runtime.InteropServices;
using System.Text;
public class FooWrapper
{
[DllImport("FooWrapper.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern short DoSomething(ushort var);
}

// More functions like the one above

I have added FooWrapper.dll to my project by going into VS and doing Solution Explorer > Add > Existing Item > Directory that my dll is in > FooWrapper.dll我已通过进入 VS 并执行解决方案资源管理器 > 添加 > 现有项目 >我的 dll 所在的目录> FooWrapper.Z06416233FE5EC4C5933122E4AB24 将 FooWrapper.dll 添加到我的项目中

For my DLL I just created a new DLL C++ project, added a class named FooWrapper.对于我的 DLL,我刚刚创建了一个新的 DLL C++ 项目,添加了一个名为 ZA2F2ED4F8EBC2CBB614C21A29DC4 的项目。 This resulted in having a project that has这导致了一个项目

Project 
|
-Refrences
|
-External Dependencies
|
-Header Files
-- FooWrapper.h
-- framework.h
-- pch.h
| 
-Resource Files
|
- Source Files
--FooWrapper.cpp
--dllmain.cpp
--pch.cpp

FooWrapper.h looks something like this FooWrapper.h 看起来像这样

#pragma once

#ifndef FOOWRAPPER_H
#define FOOWRAPPER_H

#include <string>
#include "Foolib.h" // header file that comes with lib file i originally had

using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

#ifdef FOOWRAPPER_EXPORTS
#define FOOWRAPPER_API __declspec(dllexport)
#else
#define FOOWRAPPER_API __declspec(dllimport)
#endif


FOOWRAPPER_API I16 DoSomething(const I16 var) // I16 is defined (typedef) as a short 

#ifdef __cplusplus
}
#endif
#endif


I have also linked the lib and header by adding the include directory under Properties>C/C++> Additional Include Directories我还通过在 Properties>C/C++> Additional Include Directories 下添加包含目录来链接 lib 和 header

and Linker > Additional Library Directories > *Directory that FooLib.lib is in *和 Linker > 其他库目录 > *FooLib.lib 所在的目录 *

and Linker > Input > Additional Dependencies > *Directory that FooLib.lib is in * > FooLib.lib和 Linker > 输入 > 附加依赖 > *FooLib.lib 所在的目录 * > FooLib.lib

So lastly when I call DoSomething the code stops and gives me the Exception from HRESULT: 0x8007007E error message.最后,当我调用 DoSomething 时,代码停止并给我 HRESULT 的异常:0x8007007E 错误消息。

Can someone maybe tell me if I skipped a step?如果我跳过了一步,有人可以告诉我吗? Or am I supposed to use C++/CLI to create the library?还是我应该使用 C++/CLI 创建库? If so I can try that again.如果是这样,我可以再试一次。 Or is this not possible without seeing the implementation of the FooLib.lib code?或者如果没有看到 FooLib.lib 代码的实现,这是不可能的吗?

castro 's comment solved the issue: castro评论解决了这个问题:

Try creating your C++ project as a CLR project.尝试将 C++ 项目创建为 CLR 项目。 So long as you follow the rules (only include native code in the cpp) it should then be as simple as adding your project as a reference to your C# project.只要您遵循规则(仅在 cpp 中包含本机代码),它就应该像添加您的项目作为对 C# 项目的引用一样简单。 Going from memory so I can't say for sure, but this might help you get started: docs.microsoft.com/en-us/cpp/dotnet/…从 memory 开始,所以我不能肯定,但这可能会帮助您入门: docs.microsoft.com/en-us/cpp/dotnet/...

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

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