xInterop C++ .NET Bridge 4.0 is available now

xInterop C++ .NET Bridge 4.0 is available now

We are pleased to announce the release of xInterop C++ .NET Bridge 4.0, a Code Generator for Creating Two-Ways Bridges between C++ Native World and .NET World. Visual Studio 2017 is now supported.

xInterop C++ .NET Bridge 4.0 is available to the public for evaluation to anyone who is interested in using xInterop C++ .NET Bridge to

  • Generating C# .NET Wrapper/Bridge for native C++ DLLs so that you can call native C++ DLL from .NET.
  • Generating C++ Native DLL Bridge for .NET assemblies so that you can call .NET assemblies from native C++.

You may want to download it from the link below and start evaluating it and experiencing how powerful xInterop C++ .NET Bridge is. Once you start using xInterop C++ .NET Bridge, you will find that it is so easy to bridge between the C++ native world to C# .NET managed world by using xInterop C++ .NET Bridge. You will be able to evaluate the software for free for 30 days.

 

To anyone who is interested in purchasing xInterop C++ .NET Bridge, the licensing model and pricing document is available to you upon request. If you have any questions regarding xInterop C++ .NET Bridge software, licensing or pricing, please feel free to contact us using the Contact Us Page.

Ultimate Guide to Call .NET Assembly from Native C/C++ Using Unmanaged Exports

I have been mentioning Unmanaged Exports in a few of my articles, which is a very basic technique allowing us to export managed methods from .NET world to native C and C++. The resulting assembly is a mixed mode DLL containing native-like export entry. You can use Unmanaged Exports Nuget Package to create such DLLs, As long as you can write the native C/C++ declaration of the functions correctly, you should be able to create export function with simple and primitive types. But I went further to write an article named Unmanaged Exports, An Ultimate Guide to Call .NET Assembly from Native C/C++ Using Unmanaged Exports, once you learn those advanced tips, you should be able to even export classes, structs.

Calling .NET from Native C++: How to receive C# event

There are a few other technologies we can use to call .NET from native C++ as shown in Introduction to Native C++ to .NET Bridge, such as C++/CLI, COM, Reverse P/Invoke, Unmanaged DllExport, but none of them are easy to implement receiving events from .NET as xInterop Native C++ to .NET Bridge is.

Let’s write a sample assembly to demonstrate how we can receive C# .NET events from .NET using native C/C++ code. The sample assembly is quite simple, it has only one class named SimpleWebClient which implements a method named DownloadStringAsync and an event named DownloadStringCompletedEventHandler, as you see, SimpleWebClient basically wraps method and even from the class of WebClient.

With Native C++ to .NET Bridge, we can easily create the native C++ bridge DLL and a testing application to test the resulting native DLL.

The following is the corresponding C++ native bridge class, EventSample::SimpleWebClient which bridges to the C# class, SimpleWebClient.

DownloadStringCompletedEventHandler

The following is the code demonstrating how we can receive the C# .NET event from native C++

I am also posting the Uri bridge class if you are interested.

 

Using PdfSharp for Processing PDF from Native C++ : Watermark Sample

The whole package of PdfSharp comes with lots of sample applications, one of them is watermark sample which shows three variations how to add a watermark to an existing PDF file.

You can see the resulting PDF as shown below.

image

image

image

The following is the original C# console application of Watermark sample. The C# source code was released under MIT and it was written by Thomas Hövel.

In the previous article, we talked about how to use xInterop C++ .NET Bridge with Native C++ to .NET Bridge to generate the native C++ bridge DLL for PDFsharp .NET library assembly. In this Watermark example, we will need to use the native C++ bridge DLL PdbSharpgidBridge.DLL.

The following is the complete source code of the example.

The C++ code is very similar to the C# counterpart since we can simulate the C# property in the C++ code. The native C++ application creates exactly same PDF file as the one generated by the C# console sample application. Let’s go over the important steps before we can start calling the functions.

1. Include the header file for the native C++ Bridge DLL.

image

2. Reference the native C++ Bridge lib files.

image

3. Define all the required namespace inclusion.

image

See the difference between .NET namespace and Native C++ namespace

image

There are a few things worth mentioning in the C++ code.

1. Open an existing document.

image

2. Using properties.

image

In the preceding screen-shot, Version is a property. We are using them just like using the properties in the C# code.

3. System.Diagnostics.Process class

image

We also created the native C++ bridge class for the .NET class of Process, the screen-shot above shows how to use them.

Using PdfSharp for Processing PDF from Native C++ : Hello World Sample

The whole package of PdfSharp comes with lots of sample applications, one of them is the obligatory hello world sample which shows how to create a PDF document with one page and the text “Hello, World!” written in its center.

You can see the resulting PDF as shown below.

image

The following is the original C# console application of Hello World. The C# source code was released under MIT and it was written by Thomas Hövel.

In the previous article, we talked about how to use xInterop C++ .NET Bridge with Native C++ to .NET Bridge to generate the native C++ bridge DLL for PDFsharp .NET library assembly. In this Hello World example, we will need to use the native C++ bridge DLL PdbSharpgidBridge.DLL.

The following is the complete source code of the example.

The C++ code is very similar to the C# counterpart since we can simulate the C# property in the C++ code. Let’s go over the important steps before we can start calling the functions.

1. Include the header file for the native C++ Bridge DLL.

image

2. Reference the native C++ Bridge lib files.

image

3. Define all the required namespace inclusion.

image

There are a few things worth mentioning in the C++ code.

1. Define an instance of the C++ bridge class on the stack.

image

In the preceding screen-shot, the variable of document is created on the stack. It is simple, you certainly can create a pointer of an instance of PdfDocument by calling “new” and then release it later on.

2. Using properties.

image

In the preceding screen-shot, both Info and Title are properties. We are using them just like using the properties in the C# code.

3. System.Diagnostics.Process class

We also created the native C++ bridge class for the .NET class of Process, the screen-shot below shows how to use them.

image

Enhanced SEHException Handling

The problem with SEHException

There is indeed a problem with SEHException handling.

Let’s see the definition of SEHException from the official online document.

The SEHException class handles SEH errors that are thrown from unmanaged code, but that have not been mapped to another .NET Framework exception. The SEHException class also corresponds to the HRESULT E_FAIL (0x80004005).

The .NET Framework often encounters unmanaged SEH exceptions that are automatically mapped to managed equivalents. There are two common unmanaged SEH exceptions:

  • STATUS_NO_MEMORY exceptions are automatically mapped to the OutOfMemoryException class.
  • STATUS_ACCESS_VIOLATION exceptions are automatically mapped as follows:
    • If legacyNullReferencePolicy is applied, all access violations are mapped to the NullReferenceException class.
    • If the address at which the read/write was attempted is not in JIT-compiled code, the exception is mapped to theAccessViolationException class.
    • If the address at which the read/write was attempted is in JIT-compiled code, but it is not in the OS Null partition area, the exception is mapped to the AccessViolationException class.
    • If there is no legacyNullReferencePolicy, and the address at which the read/write was attempted is in JIT-compiled code and in the OS Null partition area, the exception is mapped to the NullReferenceException class.

Any SEH exception that is not automatically mapped to a specific exception is mapped to the SEHException class by default.

What is the problem? Well, the problem is .NET does not tell what it is if it is not mapped to .NET exception. When a SEHException occurs, the message of the exception always reads “External component has thrown an exception”. It is not really useful and can not be used to determine what really happened.

Enhanced SEHException Handling

xInterop C++ .NET Bridge introduces a feature of parsing the current execution exception and creating a new type of EnhancedSEHException which is derived from SEHException. When calling a underlying native C++ method, it is always wrapped inside a try catch block so that an enhanced SEHException will be thrown when a SEHException is caught. An instance of EnhancedSEHException contains the real message passed from the native C++ DLL and the type of the object thrown.

If you open xInterop C++ .NET Bridge, you can find a demo project called Enhanced SEH Exception Handling as shown below.

Step 1:

image

Step 2: Choose the options to create Exception Handling Sample project.

image

Step 3: The C# .NET Exception Handling Sample Project has been created.

image

image

Testing

I am showing the complete testing application C# file below. You may want to take a look into the details yourself or trying the sample project using xInterop C++ .NET Bridge.

Running the application.

image

Using PdfSharp, a .NET Library for Processing PDF from Native C++

PDFsharp is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language. The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.

PDFsharp has the following key features,

  • Creates PDF documents on the fly from any .NET language
  • Easy to understand object model to compose documents
  • One source code for drawing on a PDF page as well as in a window or on the printer
  • Modify, merge, and split existing PDF files
  • Images with transparency (color mask, monochrome mask, alpha mask)
  • Newly designed from scratch and written entirely in C#
  • The graphical classes go well with .NET

With xInterop Native C++ to .NET Bridge, we can create a native C++ bridge DLL for the PDFsharp library assembly easily. By creating a native C++ bridge to the PDFsharp library assembly, we got ourselves a very nice and powerful C++ PDF library. And PDFsharp is published under the MIT License. Once you have a paid version of xInterop Native C++ to .NET bridge, you can create Native C++ bridge DLL to as many as .NET library as you want, including PDFsharp library.

C++/CLI can also be used to create C++ bridge/wrapper to PDFsharp library assembly, but it will take too much time to create such a C++ bridge/wrapper to such a library as complicated as PDFsharp library. On top of that, developers will have to learn C++/CLI if they have not already, in my opinion, it is a pain to learn C++/CLI. If you already have C++ knowledge, it costs you nothing to learn xInterop Native C++ to .NET Bridge, it is just as simple as configuration using a few forms/windows.

Let’s start creating an xInterop Native C++ to .NET Bridge project.

Step 1: Create a new xInterop C++ .NET Bridge

image

Step 2: Choose Native C/C++ to .NET Bridge project

image

Step 3: Sets up the targeting .NET assembly and the options.

image

Step 4: Choose the .NET classes to create native C++ bridge classes.

image

Step 5: Start creating the Native C++ Bridge DLL and associated .NET Bridge assembly Visual Studio project.

image

The process of creating a Native C++ Bridge DLL and associated .NET Bridge assembly Visual Studio project is much slower than simply creating a .NET to Native C++ Bridge whose underlying native C++ usually contains less C++ classes. It usually takes between minutes to hours depending on the number of classes contained in the .NET assembly, or the number of .NET classes users pick. But it is the computer who does the job, once set up and configured, users barely need to do anything to the Native C++ Bridge DLL, it is ready for use immediately after it is done.

Step 5:

image

image

Step 6: Open the TestApp Visual Studio Solution

image

When you look at the TestApp.cpp file, you will find that a skeleton application has already been created as the code shows below.

We are also showing you the partial header file of the PDFsharp native C++ bridge DLL.