Skip to main content

C# quick integration

1. C# quick integration

Since our library is developed in C++, it cannot be called directly through functions in interface.h. Instead, we need to use the DllImport attribute to declare the prototype of the C function and define the implementation of the function using the extern keyword.

Here is a simple example:

using System;
using System.Runtime.InteropServices;

class Program
{
// Declare the prototype of the connect_robot function
[DllImport("libnrc_host.dll")]
public static extern int connect_robot(string ip, string port, string robotName);

// Declare the prototype of the get_current_position function
[DllImport("RobotLibrary.dll")]
public static extern int get_current_position(double[] pos, int coord, string robotName);

static void Main()
{
// Call the connect_robot function
int result = connect_robot("192.168.1.13", "6001", "RobotA");

// Check the connection result
if (result == 0)
{
Console.WriteLine("Connection successful!");
}
else
{
Console.WriteLine("Connection failed!");
}
// Create an array to store the position information
double[] currentPosition = new double[7];

// Call the get_current_position function
int result = get_current_position(currentPosition, 1, "RobotA");

// Check the result of retrieving the position
if (result == 0)
{
Console.WriteLine("Successfully retrieved current position:");
Console.WriteLine($"X: {currentPosition[0]}, Y: {currentPosition[1]}, Z: {currentPosition[2]}");
}
else
{
Console.WriteLine("Failed to retrieve current position!");
}
}
}