Teaser Visual Studio 2022

How to read Data Layer Nodes through Visual Studio

kuldeepM
Long-established Member


Introduction

Visual Studio 2022 is a popular integrated development environment (IDE) used by many software developers to create applications for various platforms. One of the key features of Visual Studio 2022 is its ability to work with dynamic-link libraries (DLLs) to provide additional functionality and modularity to an application. In this article, we will discuss how to use comm_datalayer.dll in Visual Studio 2022 and explore its benefits in terms of code reuse, maintainability, and performance optimization. We will also provide step-by-step instructions on how to reference and use DLLs in your Visual Studio 2022 project to read ctrlX CORE datalayer nodes.

Diagram for usecaseDiagram for usecase

Prerequisites
Configuration

To create a such project in Visual Studio please follow step by step description given below.

Step 1: Create a Console app project in visual studio

Open a Visual Studio and click on create a new project as shown in picture below.

Create a new projectCreate a new project

Now you can see the templates library for creating a new project. In the search bar type in "console app".

create a console appcreate a console app

From the drop-down select a project with C++. After double-clicking on that project new dialogue box will open. Give your project a relevant name and click on Create. Make sure to check the location where you create this project.

create projectcreate project

Now your environment should look like the picture below.

visual studio environmentvisual studio environment

Step 2: Write code in the cpp file to read datalayer nodes

In the cpp file copy the code below. Depending on your application you can change the address of the datalayer and your ctrlX CORE login credential. By executing the code below you will be able to read the CPU utilisation of ctrlX CORE.



#include <stdio.h>
#include <iostream>

#include "comm/datalayer/datalayer.h"
#include "comm/datalayer/datalayer_system.h"


static std::string getConnectionString(
const std::string& ip = "192.168.1.1",
const std::string& user = "boschrexroth",
const std::string& password = "boschrexroth",
int sslPort = 443)
{

std::string connectionString = DL_TCP + user + std::string(":") + password + std::string("@") + ip;

if (443 == sslPort)
{
return connectionString;
}

return connectionString + std::string("?sslport=") + std::to_string(sslPort);
}


int main()
{

int counter = 1;

comm::datalayer::Variant data;
comm::datalayer::DlResult result;
comm::datalayer::DatalayerSystem datalayerSystem;
datalayerSystem.start(false);
std::cout << getConnectionString() << std::endl;
comm::datalayer::IClient* dataLayerClient = datalayerSystem.factory()->createClient(getConnectionString());
std::cout << dataLayerClient << std::endl;
if (dataLayerClient->isConnected())
{
std::cout << "Bingo! it is connected" << std::endl;
}
else {
std::cout << "Client is not connected" << std::endl;
}

//endless loop
for (int p = 0; p < 1000; p++)
{
//std::string co = "ping 192.168.1.1";
//std::system(co.c_str());
//std::cout << "Loop #" << counter++ << std::endl;
//Synchronous read to the Data Layer path "framework/met..."
result = dataLayerClient->readSync("framework/metrics/system/cpu-utilisation-percent", &data);

if (result == DL_OK) {
std::cout << "Value CPU utilisation: " << double(data) << " %" << std::endl;
}
else {
std::cout << "Reading the CPU utilisation failed with: " << result.toString() << std::endl;
}

}

delete dataLayerClient; // Callbacks are no more called
}


Save this code. You will notice that it shows many errors now. This is because headers are not configured yet.

Step 3: Configure headers

To configure headers go to solution explorer on the left or right side and right-click on the project. In the drop-down menu select properties. The alternative in the Menubar project-->properties.

project propertiesproject properties

 

In the properties dialogue box go to C++-->General-->Additional Include Directories-->edit.

edit properties for headersedit properties for headers

When you click on edit new window will pop up in this window define the address where your 'ctrlx-automation-sdk/include/comm.datalayer'. You either manually type in or locate via clicking on the button with three dots.

locate sdk repositorylocate sdk repository

After locating the right folder click on ok. Now your headers are configured because in this included directory all necessary headers for the datalayer are located.

But this is not enough. We also need to feed this project additional dependencies by locating respective .lib and .dll files.

Step 4: Configure .lib files

again go to the properties dialogue box. this time Linker-->Input-->Additional Dependencies-->edit

locating .lib files in propertieslocating .lib files in properties

In windows of additional dependencies type the exact same .lib as highlighted in the picture above.

Here we have only provided the names of the .lib file which need to be added but it does not know where to find such .libs. Therefore go to Linker-->General-->additional library directories-->edit and define the location of each .lib file in SDK as shown below in the picture.

define location of dependenciesdefine location of dependencies

Click on ok and .libs are configured.

Step 5: Build and release the project

To build a project go to Menubar -->Build-->Build project_name or just press ctrl+B.

output of buildoutput of build

The build process has to be finished by '1 succeeded'. If not check the location of configured headers.

Now change the mode from debug to release and click on local Windows Debugger as shown in the picture below.

select release in toolbarselect release in toolbar

Now release will end with an error dialogue box. Don't worry about that, just click on Close and Open file explorer.

Step 6: Copy DLLs at the project  location

The above error occurred because it could not file dll. and there is the reason why we have not resolved this error before. Because if we have built a release in the first place then there would be no release folder inside the project directory.

Now to resolve this error go to your 'ctrlx-automation-sdk-->bin'. Inside bin there are several folders and in some folders you will find 'win-msvc-x64-->release' go to this folder and copy every file in this folder to 'your_project-->x64-->Release'.

location dll inside sdklocation dll inside sdk

After copying the necessary files your Release folder should look like the picture below in my case there are files named vscode, which is the name of my project.

Look of Release folderLook of Release folder

 

Step 7: Rebuild and Run the code

After locating DLL dependencies inside the release folder now go to the visual studio project.

To rebuild first go to 'Menubar-->Build-->Clean Project_name' after the cleaning process is finished again go to 'build-->Build Project_name'. Now the build is finished and click on the 'Local Windows Debugger' button on the toolbar.

Now you can realise your output in the console window of the visual studio as can be seen in the picture below.

Output of programOutput of program

At runtime If you encounter any error related to OpenSSL DLLS then go to: https://slproweb.com/products/Win32OpenSSL.html install OpenSSL 1.1.1 relevant files and add those to environment variables.

kuldeepM_0-1695974695536.png

 

If adding an environment variable is not an option for you then, manually copy dlls of "C:\Program Files\OpenSSL-Win64\bin" to Release folder.

At the bottom, you can find a zip file of my project which is already configured with dependencies.

Thank you for taking the time to read this article. I hope you found it informative and enjoyable. If you have any questions, comments or encounter any unusual problems with the project, feel free to leave them in the comments section below. I would love to hear from you and continue the conversation. Your feedback is always appreciated!

2 Comments
Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist