Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 2.59 KB

File metadata and controls

79 lines (62 loc) · 2.59 KB

Interaction between Native and Managed Code

The [Native] attribute indicates that the class contains methods designed for native implementations, showcasing the ability to leverage lower-level functions, libraries, or performance optimizations that may not be feasible in managed code.

C# Class

SimpleFB1.cs

The class interacts with the native implementation via the SimpleFB1NativeMethods class, which is marked with the [Native] attribute, indicating that it contains methods that interface directly with native code.

    [Native]
    public static class SimpleFB1NativeMethods
    {
        public static int Calculate(int valueA, int valueB)
        {
            // Implementation in native code
            return 0;
        }
    }

Native Implementation

SimpleFB1NativeMethods-cli.cpp

The methods within SimpleFB1NativeMethods are defined for use by the C# function block and are prefixed with __PInvoke__, allowing them to be called from the managed environment.

Int32 __PInvoke__ DocuSharedNativeLibrary::SimpleFB1NativeMethods::Calculate(Int32 p0, Int32 p1)
{
    // Native implementation
}

C# Function Block

SimpleFB2.cs

The SimpleFB2 function block class is marked by the [Native] attribute. Only the potential content of the __Init() method needs to be run managed and their for marked with the [Managed] attribute.

    [FunctionBlock]
    [Native]
    public class SimpleFB2
    {
        [Input]
        public int IN1;
        [Input]
        public int IN2;
        [Output]
        public int OUT;

        [Initialization]
        [Managed]
        public void __Init()
        {
        }

        [Execution]
        public void __Process()
        {
            // Implementation in native code
        }
    }

Native Implementation

SimpleFB2-cli.cpp

The methods in SimpleFB2 are defined in the accompanying native implementation file, where the __Process method performs the core logic—adding the input values together and assigning the result to the output. This method is prefixed with __PInvoke__ to allow seamless invocation from the managed C# environment.

void __PInvoke__ DocuSharedNativeLibrary::SimpleFB2::__Process()
{
    OUT = IN1 + IN2;
}

Conclusion

By using the [Native] attribute, the integration allows for robust functionality to be implemented in native code, which can be seamlessly accessed from the managed C# environment. This provides enhanced performance and the ability to utilize platform-specific features.