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

-5

I only have a lib file and its header file (static library?) I want to use this in C#. From googling I have come to the conclusion I need to write a wrapper dll that C# will be able to use.

I have tried writing this both in C++ and C++/CLI but when I run my program I get a error stating that

Unable to load DLL 'foo.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

I have already used a dependency checker and my dependencies are fine so I'm assuming my dll setup is incorrect.

I am only trying to use functions from the lib not classes.

Here is an example of my cs file

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

For my DLL I just created a new DLL C++ project, added a class named FooWrapper. 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

#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

and Linker > Additional Library Directories > *Directory that FooLib.lib is in *

and Linker > Input > Additional Dependencies > *Directory that FooLib.lib is in * > FooLib.lib

So lastly when I call DoSomething the code stops and gives me the Exception from HRESULT: 0x8007007E error message.

Can someone maybe tell me if I skipped a step? Or am I supposed to use C++/CLI to create the library? If so I can try that again. Or is this not possible without seeing the implementation of the FooLib.lib code?

c#
c++
c++-cli
asked on Stack Overflow May 23, 2020 by JOV

1 Answer

-2

castro's comment solved the issue:

Try creating your C++ project as a CLR project. 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. Going from memory so I can't say for sure, but this might help you get started: docs.microsoft.com/en-us/cpp/dotnet/…

answered on Stack Overflow May 23, 2020 by JOV • edited May 23, 2020 by Wai Ha Lee

User contributions licensed under CC BY-SA 3.0