How to use System namespace and library through MS Visual Studio in UE4 C++ project?

Dear community and staff,

A personal project of mine, namely controlling a wireless toy cannon from a user interface built with the Unreal Engine has been blocked for while now. I can’t seem to integrate the Microsoft Windows platform serial communication directly into a C++ class in the Unreal Engine 4.

Edit 2015-02-21: To summarize, I am trying to use a System::IO::Ports::SerialPort in my project.

Below is a minimal code snippet to reproduce the problem, with marked known compilation failures. Thank you for any feedback, suggestion, comments.

In SerialPort.h file:

#pragma once

#include "GameFramework/Actor.h"
#include "SerialPort.generated.h"

#using <System.dll> // 1. Compilation failed: Unrecognized compiler directive using
using namespace System; // 2a. Compilation failed: error C2871: 'System' : a namespace with this name does not exist
    
UCLASS()
class TEST_API ASerialPort : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASerialPort();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

    // Serial port methods aggregation removed from the example for simplicity: connect(), write(), etc...

private:
	System::IO::Ports::SerialPort _port; // 2b. Also: Compilation failed: error C2871: 'System' : a namespace with this name does not exist
};

Additional notes:

This is a very specific project, for MS Windows deployment only, more of a toy really.
I have both pieces working separately: front end and back end serial port in other projects of mine.
I have added CLR support in the project, general configuration properties.
I believe I added dependencies and includes to the project.

Please let me know of any suggestion or question.
Thank you,
Fen

What exactly are you trying to do with

#using <System.dll>

This isn’t valid C++ syntax at all.

Unreal’s Build Tools and friends do all the actual work. VS is basically only just for editing source files in nice GUI. You need to add in some flag via the Project.Build.cs or Project*.Target.cs files. Quick search shows you’ll need to set CLRMode = CPPCLRMode.CLREnabled somewhere.

./Source/Programs/UnrealBuildTool/Configuration/UEBuildModule.cs

Well yes but, it is compiler specific and works with MSVC++. The standalone version works just fine. It is likely a tool chain specific constraint in my case.

Hm… I see, another route might be to build a standalone DLL and link it to the project for that class, following the tutorial for linking libraries.

If you search the forums for Kinect v2, you can see how they did it. Or ask on their thread. Must have run into what you did.

I was eventually able to control my toy cannon from the unreal engine menu I created, controlled through a .Net framework DLL using the System SerialPort. I will post a brief video about the project next week, the article which was the starting point for solving the situation was: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums I will add further to this article and enhance it for everyone to reuse the work. All in all this question is solved. Thank you everyone.

As promised, the wiki article for linking DLL has been updated with all of what I learned. And I recorded a video of the little project I used this knowledge for. It was pretty awesome to work on this with the Unreal Engine. Enjoy.