question

DavidHoffman-6474 avatar image
0 Votes"
DavidHoffman-6474 asked DavidHoffman-6474 commented

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

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.


c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered DavidHoffman-6474 commented

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.

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I will try it. Thanks. I would not have thought that the enum class was not supported, so I will try that first.

0 Votes 0 ·

Sorry I should have put the comments under your answer. Still new to this forum. I tried your suggestion all the possible ways I could and it only made the problem worse. I will keep trying until I get it.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 DavidHoffman-6474 ·

Maybe you did not try the simplest test. You must compile 'first.idl' from command line, then compile 'second.idl'.


0 Votes 0 ·

I tried what you suggested and it never worked. Thanks for the help.

0 Votes 0 ·
Show more comments