🎯 How to call Microsoft.Windows.AI from C++ [With Sample Code]
Since Windows 10, Windows has been equipped with a built-in **runtime capable of executing ONNX format AI models . This is ** Windows ML (Windows.AI.MachineLearning).
In this article, we will specifically explain how to call Microsoft.Windows.AI.MachineLearning from C++ (Win32 app based), along with ** sample code**.
✅ Preparation
◾ System Requirements
- Windows 10 (1809+) or Windows 11
- Visual Studio 2019 or later (Community edition is fine)
- C++/WinRT Support (
Microsoft.Windows.CppWinRT) - Windows SDK 10.0.17763.0 or higher
✅ Project Configuration
Create a project in Visual Studio with the following configuration:
Type: C++ Windows Desktop Application (Empty Project)
Subsystem: Windows (
WinMain)Add the following package via NuGet:
1Microsoft.Windows.CppWinRT
✅ Sample Code
Below is a minimal sample combining the Win32 API and Windows.AI.MachineLearning using WinMain.
- Note: Assume the ONNX model to be used is
model.onnx, and place it in the same folder as the executable file.
main.cpp
| |
✅ Supplement: How to Specify Input/Output Tensors
Depending on the model, it is necessary to create and bind Tensors before inference.
Example:
| |
Outputs can be obtained similarly using result.Outputs().Lookup(L"output_0").
✅ Debugging Tips
- A
FileNotFoundExceptionwill be thrown if the model file is not in the execution folder. - An
invalid_argumenterror will occur if the input/output names do not match. - The exact I/O specifications of the model can be confirmed with tools like Netron.
✅ Summary
| Item | Details |
|---|---|
| API Used | Windows.AI.MachineLearning (WinRT) |
| Language | C++ (Win32 based) |
| Recommended Method | Via C++/WinRT headers |
| Advantages | ONNX models run natively, GPU support available |
| Caution | Pay attention to model input names and Tensor shapes |
✅ Alternatives: For those who do not want to use WinRT
- By using Microsoft’s
ONNX Runtime, you can handle ONNX models from C++ entirely without WinRT. - It supports cross-platform, allowing common code for Windows/Linux.
📌 Conclusion
Windows ML (Microsoft.Windows.AI) is a powerful AI inference engine that can be robustly used even from C++. If you need native inference on Windows, please give it a try.
For those who want specific examples of creating ONNX models and Tensor binding, we plan to explain them in a follow-up article!
