I have implemented image recognition algorithm as OpenCV c++ project and also i have C# web service.I want to call my c++ Opencv project from my c# web service. this is how my project should work.from mobile it send image to the c# web service.Then i need to call the Opencv c++ project in order to do the image recognition. I tried to do this by using DLL.If you think using the DLL is best method can you please tell how i convert my Opencv c++ projetc into DLL OR are there any other method.
Thank you very much
I think that you should use PInvoke to do your job. Haven't used it, but from what I know, this is the way to do it:
http://msdn.microsoft.com/en-us/library/aa446536.aspx
You need to create a C++ DLL project where you will write the image recognition part of the project. You can check out this tutorial on how to create a C++ DLL project with Visual Studio: Walkthrough: Creating and Using a Dynamic Link Library
You should create a class in that DLL file, and you will be able to use that class to process an image. By using PInvoke, you will be able to use that class in a C# project. Here is a great tutorial on how to use a C++ class from a DLL in a C# project: How to Marshal a C++ Class. I have used solution A from that article in many projects to call C++ code from C#.
In order to convert an existing C++ project to a DLL, you can change its output type. Go to Project->Properties, and in that window you need to go to Configuration Properties->General, and change Configuration type to Dynamic Library (.dll), like in this image:
After that, you need to expose your class or functions to be exported from the dll. I strongly suggest you start a new dll project (folow the first link) and work from there, because the AppWizard will create some code for you and it will be easier.
Related
I'm noob in C# and I already searched on the web. Anyway I'm still not sure about this and I do not have the total control of the code I have to implement, this is why I would like to be sure I needn't make any dll. I have a C++ file with a method, and I want to call this method from C# code. I just added "extern C" to the function.
When I just add the .h and .cpp files to the C# project they aren't detected. And of course, when I try to add it as reference, it doesn't work.
So do I absolutly have to make a dll ?
Your options for accessing the C++ code from C#:
Compile C++ as unmanaged DLL and access using p/invoke. This requires the C++ code be exposed using a C style API.
Compile C++ as unmanaged DLL and access using COM. This requires that you wrap your C++ in as COM objects.
Compile C++ as mixed/mode C++/CLI assembly and access the assembly as a managed reference. This requires that you wrap the original C++ as managed C++ ref classes.
All of these options, by necessity, involve the creation of another module/assembly. You cannot link the C++ code directly into your C# assembly.
You might like to try using the PInvoke Interop Assistant to generate the C# necessary to interact with the DLL via Platform Invoke. Be aware that this is imperfect though so YMMV.
Another alternative, if you have the knowledge and patience, is to make a COM component out of your native C++ DLL and consume that from C# by using the Type Library Importer to create a managed wrapper.
You won't be able to interact with .cpp/.h files since you need at least a binary object (assembly) for C# to interact with and C# won't generate any binaries from .cpp/.h. That's on the subject about adding these files as references to the project.
As for the argument that you don't have control over the code - well, don't make a DLL out of the actual .cpp/.h, but make your own thin DLL that has a wrapper object that just includes the headers, calls whatever method you would be calling and links to the appropriate .o files or .lib or whatever you have. If the interface changes you would just changed your thing wrapper which should be easy.
I am doing mine FYP by using the
Visual C#
Agilent VEE
I am try to import the DLL file which created from C# into Agilent VEE but VEE required the header file and library file for the importing.
Please kindly help me.
In C#, there is no header file, this is different from C++! To use old C/C++ code, use PInvoke.
You cannot consume a c# dll in your C++ project and vice versa (normally). You should either create a com aware dll in C# to consume it in C++ or on the other hand, you need to declare all the functions in C# to use from a c++ dll.
Here I am talking about standard dlls (not activex or com all). They require a different methodology to work with.
Perhaps you can avoid the "Import Library" primitive in VEE, and not need a header file.
Use Device --> .NET Assembly References... and browse to your DLL files. Then you have to import or select a namespace. Finally, functions of the DLL appear in the function browser of VEE.
Not all DLL files can be used. I have two similar from a hardware vendor. The one named somename_net.dll works.
If you find an answer to your original question, I'd like to know. My method
I am trying to read GUID attribute of a C#.net assembly from c++ (VC++ 10).
I should mention that, I don't want to use .net or .net reflection. looking for a pure c++ way.
what is the solution ?
Thanks in advance.
You need to use the Unamanaged Metadata API
and especially the IMetaDataImport::EnumCustomAttributes Method
That's going to be quite a work... Here is a link that gives a good starting point on this (it's C#, but the ideas are exactly the same, and is in fact easier to program in C++): Reading types from assembly
Check out the The .NET File Format MetaSection over at CodeProject.
i understand you don't want to use reflection or .net.
You do however want to get the GUID from a C# dll you have, that was built with .NET.
The common way C# developers make their class libraries available to COM-based developers is to use the tlbexp.exe (type library export) tool to export a type library file.
The COM-developer can then use the .tlb file in their COM code.
When the C# developer builds the dll they either put the Guid manually in the AssemblyInfo level (in .NET) or the compiler will generate the Guid automatically when building the dll.
show how the COM client (C++) is built to use the COM server (C#)
http://msdn.microsoft.com/en-us/library/aa645738%28v=vs.71%29.aspx
note that in this article refers to another tool regasm.exe which registers the dll and can export the tlb at the same time but it is not necessary to register the dll on your system
as a developer you can just use the tlb file
So I have some .lib file (generated like this one) How to use it from my C# WPF application?
When you want to use native libraries from C# you won't need a .lib file. The way this is handled in .NET is by using Platform Invoke (P/Invoke). I suggest you follow the tutorial on MSDN, it will get you started:
Platform Invoke Tutorial
If you want to generate a wrapper you might want to have a look at the P/Invoke Interop Assistant on CodePlex. Please note that this tool works on the original C/C++ code. Using a .dll file to create a wrapper is not feasible because native DLLs don't store the signature of the exported functions (as described in this thread) and a lib file will store the signature in a compiler specific way.
I want to add a VC++ DLL reference into my C# Visual Studio project. But when I try to add it I see, "It is not a valid assembly or COM component".
Please suggest how I can use the VC++ DLL as a reference in a C# project.
There are two options for using a C++ DLL from C#: either COM interop, or P/Invoke. COM Interop involves creating a COM object in your C++ DLL, and then adding it as a reference. You can use the COM object like a C# object (for the most part) at this point.
P/Invoke allows you to call exported functions from C# (think calling standard Win32 API functions from C#). This is likely easier to set up, since all you need to do is export a function, however that could cause you to refactor your code in the C++ DLL, since it's not a very OOP way of doing things.
You can only use C++ components in C# when they have been prepared for use, for example by being written in C++/CLI or being written as a COM server.
If your component is a plain C++ dll you'll need to write some wrapper code, probably best is C++/Cli
I am not sure whether this solve..
run:
tlbimp /out:MyOldCom.dll MyNewAssembly.tlb
Then use it as you would any other assembly.
Please refer
http://msdn.microsoft.com/en-us/library/aa302324.aspx
http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
ie)
One way is to package your DLL as a COM class and Another way is using DllImport