0

Update 1:

I am wondering whether I can reference to a .lib file, but it seems that I cannot. If this is true, and I have no source code of the C++ project, How can I use its methods? Btw, I'm using FastCV library.


I come across a situation that I need to call C++ methods from C# code. The C++ generated files structure:

 lib
 --libfastcv.lib
 --vc120.pdb inc
 --fastcv.h
 --fastcv.inl
 --stdint.h

I know how to call C++ methods from C# :

[DllImport("libfastcv.lib",CallingConvention=CallingConvention.Cdecl)]
public static extern <ReturnType> <MethodName>(<Parameters>);

But I think the .h and .inl files need to be included in my C# project as well. So how to include them?

Thank you very much.

Franva
  • 6,565
  • 23
  • 79
  • 144
  • 1
    Sounds to me that you are completely mixing up the concept of C++ and its compiler/linker parts and the .NET world. – Uwe Keim Mar 05 '14 at 14:27
  • 2
    That is not possible, the C# compiler doesn't know how to parse C or C++ declarations. You can use a C++/CLI project instead. – Hans Passant Mar 05 '14 at 14:27
  • Hi guys,thank you for your replies. Can I reference to a .lib file? – Franva Mar 05 '14 at 14:35

2 Answers2

2

They don't. Instead, you need to build/use binary-compatible types in your own code, and use them. (And, you importing a method from dll, not from lib).

Shlomi Borovitz
  • 1,700
  • 9
  • 9
  • Hi Shioml, Yes, I was wondering can I really reference to a .lib file. However, I do not have the C++ code, so how can I use the C++ methods? The framework I'm using is FastCV which is written in C++. – Franva Mar 05 '14 at 14:32
  • There are two options here: 1. The framework you're using has those functions in dlls, and the lib just contain code to link those functions. in this case, you just need to know the names and the signatures for those functions, and in which dlls they resides in. – Shlomi Borovitz Mar 05 '14 at 14:55
  • 2. The framework you're using has those functions in the libs themselves. in this case, you'll have to write C++ dll, which will expose them (then you'll be able to import that dll), or, write C++/CLI dll, which will wrap the calls to those functions in managed code (then you'll be able to add that dll as a reference to your C# project, and use it directly). – Shlomi Borovitz Mar 05 '14 at 14:56
  • Hi Shiomi, thank you for your reply. Your case 2 sounds the right situation for me. However, this lib file is more than 45 MB which has thousands of methods, how could I possible to write a C++ project by my own to wrap the calls to those functions. I am grateful if you could explain this to me in detail and I also happy to upload the files for you to examine. – Franva Mar 05 '14 at 15:06
  • You don't need to expose every method in the lib. just those you need in you're C# project. In this case, I would recommend to write the DLL in C++/CLI, although it will require you to learn some new syntax. the reason for that recommendation, is that if referring a method in unmanaged dll, and that method doesn't exists (or have different signature) - you'll find it out only in run time. by using C++/CLI, you won't need to use `DllImport`, and when you try to refer to non-existent function, you'll get compilation error. – Shlomi Borovitz Mar 05 '14 at 15:14
  • Hi Shiomi, excellent answer. To be honest, I'd like to do it in C++/CLI way. Could you please write just one function in an example for me? Here is the API document https://developer.qualcomm.com/docs/fastcv/api/group__object__detection.html – Franva Mar 05 '14 at 15:20
  • 1
    [gist example](https://gist.github.com/anonymous/9369507) this example creates static managed class (which I'm not sure would compile... but you get the idea), which expose that method. It assumes that you declared the proper `#include` directives, and namespaces. Here I just used static class, to expose function directly, but you may want to create class, and object model to reflect this API in more object oriented manner. – Shlomi Borovitz Mar 05 '14 at 15:36
  • 1
    excellent. I am trying to tune your code to suit my case. I'll keep you posted. Thx a lot mate :) – Franva Mar 05 '14 at 15:55
  • Learn C++/CLI first! (not too hard, if you know C++... just little more syntax) – Shlomi Borovitz Mar 05 '14 at 15:56
  • learnt C++ a little bit 8 years ago and since then never used it...I'll try to see how much I can pick up – Franva Mar 05 '14 at 16:20
  • Hi Shiomi, I found a great article about how to do the C++/CLI Wrapper. In this case, I have to find the class which I want to wrap and have an instance of a native C++ class(let's call it NCC) in my wrapper. So my question, in my case(which I sent you the doc link previously), what is my NCC in my case? Here is the section, I'd like to refer to http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-minutes#A8 – Franva Mar 05 '14 at 17:01
  • 1
    As it seems, there is no NCC. This API is a C API, not C++ (there are no classes in C). But C++/CLI is superset of C++ (which means, it, by proxy, a superset of C) - so you'll have to be familiar with the syntax which is based on. – Shlomi Borovitz Mar 05 '14 at 17:05
  • 1
    got it, then I'd be better to look into the wrap around C methods section. – Franva Mar 05 '14 at 17:14
0

You dont have to do any includes. The DLLImport should be enough.

To see the Methods you can import you can use DependencyWalker or my favourite Tool CFF Explorer

I often used any WINAPI functions where i need some constants defined in headers. I always had to define them in my C# code too, theres no way to "import" them.

SaschaW
  • 58
  • 1
  • 9
  • Hi thank you for your reply. When I tried to include the .lib file, the Dialog prevented me from doing so by only showing .dll files. – Franva Mar 05 '14 at 14:37