How to get GUID (Assembly attribute) of a C# assembly in c++ - c#

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

Related

It there have any way to create the DLL with a header file by using visual C#?

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

How to export c# function so it can be called from unmanaged environment?

I would like to know how can I export a function so I can use it in other programming language?
I want to use the dll functions in unmanaged programming language.
What can I do?
You will need to run regasm.exe with the /tlb command against your .NET assembly and then reference the output type library (tlb) from C++.
I've done this many times before and it can be a pain, especially when you go to deploy it. I recommend using the /codebase switch, which tells the registry where to find your .NET assembly when its classes are instantiated in a COM context. That makes it easy to find in the registry and tweek it as necessary.
Two articles that may be of use... it sounds rather painful:
An Overview of Managed/Unmanaged Code Interoperability
Exposing .NET Framework Components to COM

Exporting functions from a C# class library

How would I export functions defined in a C# class library, while enabling them to be imported to and called from an unmanaged C++ application/DLL ?
Strictly speaking, you can't just export functions as you would in a classic .dll, as .NET .dll's aren't really .dll's at all. Your only three options are:
Use managed C++
Expose your C# classes as COM objects and consume them from your C++ code
Host the .NET runtime in your C++ project and interact with your C# classes through that.
Your C++ Apllication would have to start by hosting the CLR. There is nothing special required from the .NET DLL.
You would not. Not supported. You can pretty much only export COM objects from a C# class librarly.
You could also make a C++ wrapper for your C# library - a simple Managed C++ DLL that would import .NET methods and export them natively. This adds an additional layer, but it might be useful if C# library is a must have.
Another option is to tweak the compiled assembly to export the functions. A C# compiler cannot do this, but it takes a slight change of MSIL code to get the things done.
Have a look at this article - there're some links on how the stuff works, and a tool to automate it (though I haven't tried it myself).

Using a VB6 Class in C#

Is it possible to use a VB6 class in C#?
I think you should just be able to add the library that contains your VB6 type as a reference in your C# project. Visual Studio will create an Interop Assembly on the fly, and you'll get access to all of the types in the VB6 library via Runtime Callable Wrappers.
The tool that creates the Interop Assembly is TLBIMP.EXE, and you can run this yourself if you want more control over the process, eg. if you want to create a Primary Interop Assembly that might be shared by multiple managed components.
You can use a compiled VB6 dll in a C# program by using COM Interop.
https://stackoverflow.com/questions/tagged/interop
As #Wayne states in his post (+1) it is absolutely possible.
I would go for a rewrite of your VB6 class:
If you have the VB6 source and the funding, I would recommend you to rewrite the class in C#.
Although VB6 may live forever :
Current support Statement for Visual Basic 6.0
Sure, you just need to make it a COM object.

Convert Interface IDL file to C#

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.

Categories