Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# "Unmanaged Exports" [closed]

Tags:

c#

unmanaged

I've been trying to use the extension "Unmanaged Exports" by Robert Giesecke in a Visual Studio 2010 pro/C# project. Yet, I can't make it work - when I check the compiled DLL for exports, the viewer (http://www.nirsoft.net/utils/dll_export_viewer.html) always comes up empty, no exports seem to be defined at all.

I have all but copied the example and set build/config manager/active platform to x86. Can I somehow check if the MSBuild task that does all the magic is actually run or not? What should the project file contain (it seems to be suspiciously empty to me?)

like image 996
user2489608 Avatar asked Jun 15 '13 20:06

user2489608


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

I would recommend you do this the documented way instead of relying on a undocumented hack from an author who doesn't provide support. Let's do it with an example:

namespace Publics {
    public class Class1 {
        public static void Run() { 
            // Stuff...
        }
    }
}

Add a new C++/CLI class library to your project. Right-click the solution, Add, New Project. Open the "Other Languages" node, Visual C++, CLR, and pick the "Class Library" project template. Right-click the new project, Properties, Common Properties, Framework and References, click the Add New Reference button. From the Projects tab, pick the C# project whose method(s) you want to export.

Delete the pre-generated empty class with the //TODO comment and write this kind of code:

extern "C" __declspec(dllexport)
void __stdcall Example() 
{
    Publics::Class1::Run();
}

Build your solution. Check that the Example function got exported by running dumpbin.exe /exports on the DLL. You should see something similar to this:

      1    0 00001020 _Example@0 = _Example@0

Beyond the name and the calling convention, you now also have lots of choices to tweak the exported function. If you want to export an instance method instead of a static method you could write the function like this for example:

extern "C" __declspec(dllexport)
void __stdcall Example() 
{
    Publics::Class1^ obj = gcnew Publics::Class1;
    obj->Run();
}

Etcetera, some familiarity with the C++/CLI language is required if you are going to make this elaborate. Last but not least, you are also likely to find out what went wrong in your original attempt to make Giesecke's IL rewriter work. It otherwise uses the exact same technique that the C++/CLI compiler uses to export the managed method.

like image 102
Hans Passant Avatar answered Sep 20 '22 16:09

Hans Passant


I've been using version 1.1.3 and see there is now a newer version with NuGet support. I just did a test with that.

Can I somehow check if the MSBuild task that does all the magic is actually run or not?

You can get more detail from MSBuild using the command line or adjust the verbosity that Visual Studio requests: Tools > Options > Project and Solutions > Build and Run > MSBuild project build output verbosity [VS 2010]. You'll probably want to reset it as soon as you are done troubleshooting.

I saw the target and task were being called but didn't see any results until I switched the project platform to x86. Then I see various, relevant log entries including Adding .vtentry:0 .export....

What should the project file contain (it seems to be suspiciously empty to me?)

There isn't much needed in the project file. NuGet does it all: A reference to the DllExport assembly and an include for the target file.

A couple things that I can think of that might be tripping you up:

  1. Make sure you are actually building the project. The Solution Build Manager can have some projects not set to build for the selected solution configuration.
  2. Make sure you are checking the right DLL. The build task writes the path into the build log. The line starts with Assembling.
like image 36
Tom Blodget Avatar answered Sep 17 '22 16:09

Tom Blodget