C++/WinRT how to include an enum class in .h file for IDL file

David Hoffman 121 Reputation points
2021-09-10T21:25:02.16+00:00

Here is my .h file.

#pragma once

#include "pch.h"
#include "TCP_Page.g.h"

using namespace winrt;
using namespace std;
using namespace Windows::UI;
using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Xaml::Media::Animation;
using namespace Windows::Foundation;
using namespace Windows::Networking::Connectivity;
using namespace Windows::Storage::Streams;

enum class NotifyType
{
    NoNotifyTypeMessage,
    StatusMessage,
    ErrorMessage
};

namespace winrt::GUI_Test::implementation
{
    struct TCP_Page : TCP_PageT<TCP_Page>
    {
        TCP_Page();
        void UpdateStatus(hstring strMessage, NotifyType type);
        void NotifyUser(hstring strMessage, NotifyType type);
        void NotifyUserFromAsyncThread(hstring strMessage, NotifyType type);
        ...
    };
}

namespace winrt::GUI_Test::factory_implementation
{
    struct TCP_Page : TCP_PageT<TCP_Page, implementation::TCP_Page>
    {
    };
}

Here is my IDL file.

namespace GUI_Test
{
    [default_interface]
    runtimeclass TCP_Page : Windows.UI.Xaml.Controls.Page
    {
        TCP_Page();
        void UpdateStatus(String strMessage, NotifyType type);
        void NotifyUser(String strMessage, NotifyType type);
        void NotifyUserFromAsyncThread(String strMessage, NotifyType type);
    }
}

Here is the error I get.

Error MIDL2011 [msg]unresolved type declaration [context]: GUI_Test.NotifyType [ Parameter 'type' of Procedure 'UpdateStatus'( RuntimeClass 'GUI_Test.TCP_Page' ) ]

I have never dealt with an IDL file before. Because NotifyType is an enum and is declared in the .h file, the IDL file can't resolve its type. I just don't know exactly what to do to solve the issue. Importing the .h file did not seem to fix it unless I put it on the wrong line which was the line before TCP_Page(). Since I made the enum a class, I figure that might be the problem. I suppose I could just add it to the IDL file, but then I will have 2 places that I need to maintain the code. Not a good solution.

This is probably easy to fix, I just have no clue on how to do it. Thanks.

Developer technologies | C++
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-09-11T05:36:52.393+00:00

    Maybe try dividing into two files:

    // first.idl
    
    namespace GUI_Test
    {
       enum NotifyType
       {
          NoNotifyTypeMessage,
          StatusMessage,
          ErrorMessage
       };
    }
    
    
    // second.idl
    
    import "first.idl";
    
    namespace GUI_Test
    {
       [default_interface]
       runtimeclass TCP_Page : Windows.UI.Xaml.Controls.Page
       {
          TCP_Page();
          void UpdateStatus(String strMessage, NotifyType type);
          void NotifyUser(String strMessage, NotifyType type);
          void NotifyUserFromAsyncThread(String strMessage, NotifyType type);
       }
    }
    

    Remove the enum from .h file and insert #include "first.idl". You can also rename first.idl to first.h.

    It seems that 'enum class' is not supported.


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.