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.
Related
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 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.
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
I have an interface defined in an IDL file that I would like to use in C#. Is there a way to convert the IDL to something usable in C#?
One way is to run MIDL on the IDL to create a type library (.tlb). This requires a library block in the IDL. Once you have the .tlb, you can run tlbimp.exe on it to get a C# definition/Interop DLL.
What datatypes/structures are used in the IDL? You should first define the datatypes in C# first if there is no inbuild type already.
You can use the following tool to convert the structures, but you need to verify the ouput manually.
Download: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe
This utility is described at Accessing Windows API Constants and Structs for P/Invoke.
The original January 2008 article is now only available as a .CHM help file download, linked at the bottom of https://msdn.microsoft.com/magazine/msdn-magazine-issues. For the time being, the source code can be found at http://clrinterop.codeplex.com/.
For example, I've recently used the XPS Print API and needed the xpsobjectmodel.h interfaces. The Windows SDK comes with xpsobjectmodel.idl fortunately.
I generated the TLB file with MIDL first and used TLBIMP to generate a proper DLL assembly ready to be added in the 'References...' section in my C# project.
Be sure to use the tools with the correct version for your project framework. e.g, if your project framework is 3.5-based, using tlbimp from the 4.0 toolset won't work.
I'm writing a C# wrapper for a third-party native library, which we have as a DLL. I would like to be able to distribute a single DLL for the new assembly. Is it possible for me to embed the win32 DLL in my .NET DLL, and still make calls into it using P/Invoke? If so, how?
I've never done it but I know of an opensource project that does this. They embed the native SQLite3 code into the managed SQLite assembly using their own tool called mergebin.
Go and take a look at the SQLite project for .NET by PHX and grab the source and you can see how it's done.
Should work, if the native dll does not have any dependencies.
You can compile the dll in as embedded resource, than access the stream from inside your code, serialize it to the temporary folder and use it from there.
Too much to post example code here, but the way is not to complicated.
I don't think you can do it directly, but it's possible to extract it at runtime to some temporary location and make call to that copy.