Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An Issue with converting enumerations in C++\CLI

I have an assembly, written in C++\CLI, which uses some of enumerations, provided by .Net. It has such kind of properties:

property System::ServiceProcess::ServiceControllerStatus ^ Status  
{  
    ServiceControllerStatus ^ get()  
    {  
        return (ServiceControllerStatus)_status->dwCurrentState;   
    }  
}    

it works fine, but when i use this assembly from my C# code, type of this property is

System.Enum

and i have to make type-cast

 if ((ServiceControllerStatus)currentService.Status == ServiceControllerStatus.Running)
     //do smth

The question is simple: why is it so, and how to fix it ?

like image 225
Andrey Neverov Avatar asked Sep 22 '08 14:09

Andrey Neverov


2 Answers

In C++/CLI ^ is like the analagous * in standard C++. Because enumerations are value types the ^ should not be included otherwise you will see them as System.Enum.

Remove the ^ and you will see the correct enumeration on C# side.

property System::ServiceProcess::ServiceControllerStatus Status  
{  
    System::ServiceProcess::ServiceControllerStatus get()  
    {  
        return (System::ServiceProcess::ServiceControllerStatus)_status->dwCurrentState;   
    }  
}
like image 86
Jorge Ferreira Avatar answered Oct 10 '22 12:10

Jorge Ferreira


I think enums don't use the ^ -- try removing it from the property declaration and get().

like image 38
Lou Franco Avatar answered Oct 10 '22 14:10

Lou Franco