none
How to call Standard C++ member functions in WinRT Class from C#? RRS feed

  • Question

  • I want to create a C++ class with static and non-static member functions and call them from C#. I want to use standard C++ (and not /CX -- maybe I'll ask about /CX in another post). My C# code is not part of a Universal Windows Platform (UWP) App. It is not a GUI application.

    (1) I see in Intro to WinRT that C++ WinRT is supposed to be substitute for C++/CX and WRL. This leads me to believe that my C# should be able to be independent of UWP.

    (2) Can someone point me to some step by step instructions for creating a C++ project in Visual Studio and creating a second C# project that contains code that calls the C++ member functions in the first project? I've been looking for such a tutorial for years now.

    (3) I've been reading this article in Microsoft Docs on Authoring APIs with C++/WinRT and I'm confused because it does not mention the using the Visual Studio extension (Download the Visual Studio WinRT Extension) and it does not explain how to create a new WinRT Component project.

    (4) When I use Visual Studio 2019 V16.4.4 (after having installed the above extension) to create a new WinRT Component, the generated code has lots of syntax errors. (I also notice that in the description of the code generator, it says "A project for C++/WinRT Windows Runtime component that can be used by a Universal Windows Platform App". Perhaps this means I must call it from a UWP app and I'm out of luck. Is this true?). Rebuilding the solution does not help. The syntax errors include a missing "#include file" ("Class.g.h"). I suspect this is a file generated from the IDL and a "rebuild all" is not generating this header file. How do I fix this?

    Thank you!

    Siegfried


    siegfried heintze

    Sunday, February 2, 2020 9:47 PM

Answers

  • I downloaded the project as .zip file,
    extratced, compiled.
    Strangeley, the Class.idl file was not included, so I downloaded this separately.
    Then I compiled and run the solution as Debug x64,
    I can click on the "Call WinRT Component" button, and get a dialog with:
    "Hello from an Non-Packaged WinRT Component! :D"

    there was no need to copy any postbuild step.
    • Edited by EckiS Tuesday, February 11, 2020 7:25 PM
    • Marked as answer by siegfried_ Wednesday, February 26, 2020 3:58 AM
    Tuesday, February 11, 2020 7:24 PM

All replies

  • There might be a sample in Enhancing Non-packaged Desktop Apps using Windows Runtime Components - Windows Developer Blog.

    Actually, that probably does not help. All the documentation and articles are frustratingly detailed yet totally vague about useful information. Author APIs with C++/WinRT - UWP applications seems to say that we develop a WinRT component project from among the UWP projects eventhough the project is not really a UWP project. If that is what we need to do then it is typical of Microsoft to make things confusing.



    Sam Hobbs
    SimpleSamples.Info


    Sunday, February 2, 2020 11:11 PM
  • Thanks!

    I downloaded the sample code at https://aka.ms/regfreewinrtcs and it is not working.

    (1) I'm getting the following error (see the TypeLoadException error below).

    (2) I tried manually copying the manifest file to the bin\x64\debug directory (while running in debug mode) but this did not help.

    (2) I don't understand this sentence. What am I supposed to do?

    The VCRedist can be found here: https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

    Why would I need this in a Visual studio environment? I clicked on the downloaded exe but it did not help.

    (3) Now is there a code generator in VS 2019 to generate such clients and components as described in this article? Which one? I tried creating a C++ WinRT component using the Visual Studio Code generator but could not get the virgin code go compile.

    Thanks

    Siegfried

    System.TypeLoadException
      HResult=0x80131522
      Message=Requested Windows Runtime type 'WinRTComponent.Class' is not registered.
      Source=mscorlib
      StackTrace:
       at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
       at WinRTComponent.Class..ctor()
       at WinFormsApp.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\v-rihei\Documents\WinOOP\new\Examples\WinRTDesktop\RegFree_WinRT\CS\WinFormsApp\Form1.cs:line 22
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at WinFormsApp.Program.Main() in C:\Users\v-rihei\Documents\WinOOP\new\Examples\WinRTDesktop\RegFree_WinRT\CS\WinFormsApp\Program.cs:line 19
    Inner Exception 1:
    COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))


    siegfried heintze

    Tuesday, February 4, 2020 11:49 PM
  • Hi siegfried,

    Thank you for posting here.

    During my search, I found that many people have encountered this exception. Check the following link to see if there is a solution for you.

    MSDN Forums

    Problems creating WinRT Component DLL

    And I noticed that you tried to copy some files to the bin \ x64 \ debug folder. Are you trying to switch the compilation platform?

    If so, you could right-click on the project and select property-> Target platform to switch it.

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 7, 2020 9:46 AM
  • MSDN Forums

    Problems creating WinRT Component DLL

    I am not Siegfried but I do not see where any of that answers the question about Requested Windows Runtime type 'WinRTComponent.Class' is not registered. Of the threads about components not registered, most of them are not about WinRT. The one about WinRT is asking about other things.

    I hope Siegfried is able to figure it out and that you can ignore this.



    Sam Hobbs
    SimpleSamples.Info

    Friday, February 7, 2020 2:03 PM
  • after I got it to compile: the RegFree_WinRT Windows Forms app works on my system.
    From the readme: "The sample requires Windows Build 18309+ to work"
    So at least Windows 10 build 1903
    Saturday, February 8, 2020 2:49 AM
  • Timon: regarding the bin\64\debug folder:  Is it not necessary to copy the winmd and other files produced by the component build into the bin directory of the client? How else does the client find the compiled code for the component?

    EckiS: 

    I am also running Windows 10 V 1903.

    Where you able to cut and paste the "copy /Y..." statement verbatim into the post build step?

    I'm not sure I understand the environment variables in this statement. Are they x86/x64 & debug/release neutral?

    How were you building? Debug? Release? x86? x64?

    Here is the error I get when paste that copy command into the post build event for the component:

    Severity Code Description Project File Line Suppression State
    Error MSB3073 The command "copy /Y "C:\Users\xyz\Documents\WinOOP\new\Examples\WinRTDesktop\RegFree_WinRT\Cpp\WinRTComponent\bin\Win32\Release\WinRTComponent.dll"  "C:\Users\xyz\Documents\WinOOP\new\Examples\WinRTDesktop\RegFree_WinRT\Cpp\WinRTComponent\C:\Users\xyz\Documents\WinOOP\new\Examples\WinRTDesktop\RegFree_WinRT\Cpp\WinRTComponent\bin\x86\Release\WinRTComponent.dll" 
    :VCEnd" exited with code 1. WinRTComponent C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 149

    Thanks!

    Siegfried


    siegfried heintze


    • Edited by siegfried_ Tuesday, February 11, 2020 4:51 AM typos
    Tuesday, February 11, 2020 3:52 AM
  • I downloaded the project as .zip file,
    extratced, compiled.
    Strangeley, the Class.idl file was not included, so I downloaded this separately.
    Then I compiled and run the solution as Debug x64,
    I can click on the "Call WinRT Component" button, and get a dialog with:
    "Hello from an Non-Packaged WinRT Component! :D"

    there was no need to copy any postbuild step.
    • Edited by EckiS Tuesday, February 11, 2020 7:25 PM
    • Marked as answer by siegfried_ Wednesday, February 26, 2020 3:58 AM
    Tuesday, February 11, 2020 7:24 PM
  • Thank you EckiS. 

    It works!

    So if I want to start from scratch, what type of project type do I select when creating a new project with visual studio?


    siegfried heintze

    Wednesday, February 26, 2020 4:13 AM
  • I found the problem I was having.

    When you use 

    git clone https://github.com/microsoft/RegFree_WinRT.git

    it does not work. I get the exception:

    System.IO.FileNotFoundException: The specified Module could not be found. ... 0x8007007e on line 22 of WinFormsApp.cs

    Here is the code:

    WinRTComponent.Class winRTClass = new WinRTComponent.Class();

    I confirmed that the WinRTComponent.* files had been copied from the bin directory of the WinRTCompont.


    siegfried heintze

    Saturday, February 29, 2020 10:59 PM