Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have written a DLL in C# which has a static class to be used in MFC. Can you tell me is this possible? If so, how can I reference the DLL in visual studio .net.
Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 6-Jan-11 15:22pm    
Please see my update to my Answer: added reference to original work.

Hi,

have a look at this msdn forum thread. There are some links that might point you to a solution:

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/f5a2e721-728f-498e-a398-43237e16bd75[^]
 
Share this answer
 
Comments
Shah Rukh Qasim 6-Jan-11 1:33am    
OK, tell me that can we use GetProcAddress method with C# DLLs?
Sergey Alexandrovich Kryukov 6-Jan-11 3:03am    
No. Not just "no", it's "of course not, ever, for a regular .NET Assembly!"

Please see my answer. I mean, if the assembly is modified as I described, yes, it will be accessible via LoadDLL and GetProcAddress.
Sergey Alexandrovich Kryukov 6-Jan-11 3:12am    
@JF2015: In the first edition of my answer I forgot to credit your Answer (I'll vote 5).
Now I've fixed that. Also, added/improved motivation; will you take a look? -- pretty interesting...
Thank you.
Sergey Alexandrovich Kryukov 6-Jan-11 4:07am    
Shah, my reply about GetProcAddress was inaccurate -- now clarified. Please see.
Sorry for the inconvenience.
The most usual technique to make a functionality of .NET Assembly accessible to native (unmanaged executables) is using COM. Relevant references can be found from the Answer by JF2015.

There are certain problems with COM. The technology is gradually becoming obsolete. Main problem is overhead. In particular, normally it requires registration of a component in the system registry -- one of the major sources of registry contamination. .NET and Microsoft are trying to go away from such things in favor of local approach to component dependency and versioning. Also, COM approach will hardly be available for Unix (with Mono or other product).

In view of all that, a direct export from managed assembly to the unmanaged executable would be quite attractive. But how to do this?

Officially, it is said that an assembly cannot export an umnanaged method, so .NET Assembly cannot be used by a native (unmanaged) application (MFC or something).

This is a subtle lie. If you look at the standard IL, there is a way to export a method as unmanaged (native). Indeed, it cannot be done with C# along. So, what to do? Right!

The trick is to do several additional build steps to automatically do the trick. These steps can be nicely embedded into the project (see Microsoft documentation on MSBuild -- very comprehensive). Here are the steps:

1) After .NET Assembly is compiled, disassemble into IL code;
2) Open IL file and transform the text to mark the unmanaged entry points;
3) Re-compile modified code back to executable assembly; now it exports few unmanaged method.

This needs good knowledge of IL (but no programming skills). Note, that when the tool is already available, IL programming is not required at all.

We need another trick: how to mark what methods to export as unmanaged. Naturally, those should be some static functions using common data types for parameters. But usually we need to export not all of them, only a part.

For this purpose, we can create a special attribute and mark an assembly with this attribute:
C#
[assembly:ExportToManaged(typeof(MyClass1))] 
[assembly:ExportToManaged(typeof(MyClass2))]


This will mean that all static functions of the classes MyClass1 and MyClass2 should be exported as unmanaged. Alternatively (or additionally), separate methods can be marked by the attribute (in this case, only those methods will be exported as unmanaged -- even better).

The text-processing utility working with the disassembled IL code should perform text search to find the attribute and perform the modification to the specified methods.

These are the references to the original articles:

"Unmanaged code can wrap managed methods" by Emilio Reale, 29 Aug 2004.

"How to Automate Exporting .NET Function to Unmanaged Programs" by Selvin, 22 Nov 2006.

As it often happens to a decent serious publication, these CodeProject articles collected not very good votes (I suggest everyone who is interested supports the authors).

New work found in a reader's Message: "C# Project Template for Unmanaged Exports" by Robert Giesecke, Jan 11, 2010.


100% credit to these authors.


I tried to use only slightly different approach (so it would be purely derivative work), but my code (circa 2006) is not operational yet. Later on, I lost interest to this topic due to deeper focus on managed platforms I developed. If I ever find time to put my minor improvements, and if still there is interest, I could post it as a derivative work -- not sure now though.
 
Share this answer
 
v13
Comments
Sergey Alexandrovich Kryukov 6-Jan-11 3:10am    
Updated. Added/improved motivation and added a credit to the Answer by JF2015.
JF2015 6-Jan-11 3:14am    
Very Good Answer. It'd be great If you could find your old source and show it to us. Have my 5!
Espen Harlinn 6-Jan-11 3:16am    
5+ Nice work ... interesting idea
Sergey Alexandrovich Kryukov 6-Jan-11 3:55am    
This is pretty tricky technique. I think this is essentially the only one thing to be done with C#.

Another technique might be using a mixed-mode C++/CLI project, but... are you only asking about C# only?

Finally, I found original article -- please see the updated Answer. [EDIT] Found by now -- see updated Answer. [END EDIT]
Sergey Alexandrovich Kryukov 6-Jan-11 3:58am    
Very funny there is a vote of "1". I almost proud of it: I noticed the best answers often irritate some people and got "1" or "2" (of course, worse answers also got "1" but not so often). Also, lamers hate when somebody offer them to think... sad -- not for me, for them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900