System.IO.SerialPort throws System.PlatformNotSupportedException when calling from c++, but not from c#.

Michael Bower [emkaTech] 20 Reputation points
2023-04-21T16:22:45.1233333+00:00

I have a perplexing issue with the System.io.Ports package in a Class Library I built using c#/.NET6. I suspect it is a misconfiguration, but I'm not seeing much helpful documentation thus far on StackOverflow. As some background, I must support an old ANSI C Windows application that collects data from a usb device. To do so, we decided to create a c# component to do the job.

To test the component, I created three different test projects.

My first project simply created the component within a Console App. Everything worked as expected. Great! My second example was to build a separate c#/Winforms Application that referenced the component. Again, worked as expected. More good news!

The problem occurred when we built a similar c++ Console Application to access the Component. We referenced the DLL, an had no issues instantiating the component; however, when the component tries to instantiate the SerialPort class, the PlatformNotSupported Exception is thrown: "System.PlatformNotSupportedException System.IO.Ports is currently only supported on Windows "

I read in somewhere in a discussion group that this issue is that the default .netstandard dll is a cross platform DLL, and that the project should include the following addition.

<RuntimeIdentifier>win</RuntimeIdentifier>

No Luck with that mod for me, unfortunately.

Any ideas? The fundamental goal is to expose a few static methods to the ANSI C program. It seems that the DLLExport NuGet isn't getting much love, which was my first choice.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,754 questions
{count} votes

Accepted answer
  1. Minxin Yu 11,996 Reputation points Microsoft Vendor
    2023-04-24T07:56:58.24+00:00

    Hi,

    Currently C++/CLI console app does not support .Net6, please consider using .Net Framework.(Common Language Runtime Support (/clr) and create your c# dll in .Net Framework. I created CLR Console APP(.Net Framework) template project and tested the snippet from document: SerialPort Class There is C++/CLI sample:

    User's image

    enter image description here

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. D Sanchen 1 Reputation point
    2023-06-02T07:48:27.8466667+00:00

    I had a similar problem with the constructor of ServiceController, and managed to resolve the issue with specifying <RuntimeIdentifier>win-x86</RuntimeIdentifier> in the <PropertyGroup Label="Globals">

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.