ThingWorx Edge .NET SDK Reference > Loading C SDK-based Edge Extensions within the .NET SDK
Loading C SDK-based Edge Extensions within the .NET SDK
Two paradigms exist for ThingWorx Edge SDKS:
Flat or Procedural paradigm — Represented by the C SDK
Object Oriented paradigm — Represented by the Java, Android, and .NET SDKs.
The C SDK Edge Extension Framework was designed to allow any application that uses the C SDK to load and use Edge Extensions that are built as shared libraries for the purpose of adding new features. Since the .NET SDK is based on the C SDK, the .NET SDK can load the C SDK Edge Extensions. It is important to note, however, that the you are loading a C-based Edge Extension from the Flat paradigm and using it to augment a Thing created from the Object Oriented paradigm.
Where Does the C Extension API Fit into the .NET API?
In the Object Oriented Paradigm, before the introduction of Edge Extensions for Object Oriented SDKs, all Remote Things are implemented by extending the class VirtualThing and then annotating this class to generate the required metadata for properties, events, and services that are required to bind a Thing to the platform.
As of v.5.8.0, the .NET SDK provides a way to load a C-based extension and an existing Thing Shape onto an existing .NET Thing. The changes to the .NET SDK are generic enough that they can be used for any potential C-based extension.
To provide this functionality, two key methods have been added to the .NET SDK. The ConnectedThingClient.CLoadExtensionLibrary method calls the C SDK directly, using a .NET feature called importing. The .NET SDK has a class called twApiWrapper that collects all of the library functions that are imported. The ConnectedThingClient can now load extension libraries. When an extension library is loaded, all of the shapes and templates that it provides become available to be added on to any .NET class.
All .NET classes that implement Remote Things must extend VirtualThing. If you are going to build a class and then add on a C Thing Shape, you must have an instance of VirtualThing. As a result, VirtualThing now has a method called CAddEdgeThingShape, in which you provide one of the Thing Shapes from the library that you previously loaded. As soon as you do, you instantly gain all the service implementations and properties that were called out in that Thing Shape.
The combination of the CLoadExtensionLibrary and CAddEdgeThingShape calls can be used to take any C-based edge extension and add the shapes within it to any .NET -based Remote Thing that you require.
* 
If you want to learn more about .NET import statements, refer to the Microsoft documentation at https://docs.microsoft.com/en-us/cpp/dotnet/calling-native-functions-from-managed-code?view=vs-2017..
The following table shows the mapping of C SDK API calls to .NET SDK calls.
C Extension API Call
.NET SDK Call
void * twExt_LoadExtensionLibrary(char *shapeLibraryName);
ConnectedThingClient.CLoadExtensionLibrary(string libraryName);
int twExt_AddEdgeThingShape(const char *entityName, const char *shapeName, const char *thing_namespace);
UInt32 VirtualThing(Instance).CAddEdgeThingShape(string shapeName, string thing_namespace);
To use a Data Shape, a Thing Shape, or a Thing Template, you need to import these functions into .NET
As shown in the table above, the naming convention are:
twExt_AddEdgeThingShape becomes VirtualThing(Instance).AddCEdgeThingShape(string shapeName, string thing_namespace).
Note that twExt_LoadExtensionLibrary does not fall into this category and perhaps is best implemented as a static function on VirtualThing as LoadCExtensionLibrary. The ConnectedThingClient can also load extension libraries, via CLoadExtensionLibrary(string libraryName).
How to Connect C SDK Functions to .NET?
The run time loading of a function referenced from C-based libraries is controlled by the dllimport command. Many examples of this command are available already within the .NET SDK. The example below shows how you would load a function reference inside of a .NET class at runtime. In this example,
int twExt_AddEdgeThingShape(const char *entityName, const char *shapeName, const char *thing_namespace);
can be declared as:
[System.Runtime.InteropServices.DllImport(@"twApi.dll",
CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl,
CharSet = CharSet.Ansi)]

public static extern int twExt_AddEdgeThingShape([System.Runtime.InteropServices.MarshalAs(UnmanagedType.LPStr)]
string entityName, [MarshalAs(UnmanagedType.LPStr)] string shapeName, [MarshalAs(UnmanagedType.LPStr)] string
thing_namespace);
This allows C SDK functions to be attached as static function calls to any .NET class to be loaded when the class itself is loaded.
Was this helpful?