From 6e2ea89469b2d47ebf44e79ba0232890a97f54cc Mon Sep 17 00:00:00 2001 From: Copilot <223556219+Copilot@users.noreply.github.com> Date: Tue, 14 Apr 2026 09:29:11 +0800 Subject: [PATCH 1/2] Implement GetTestMessage in CppConsoleDll using Ort::Env initialization The README documented a GetTestMessage function that was never implemented. Add GetTestMessage which creates an Ort::Env to verify the ONNX Runtime initializes correctly - the canonical ORT startup step. On success, returns the original intended message: WindowsML DLL is working correctly! Wire up the client call and keep README expected output as-is (it was correct for the intended design, just never implemented). Bug: #61791036 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../ConsoleClient/ConsoleClient.cpp | 24 ++++++++-- .../WindowsMLWrapper/WindowsMLWrapper.cpp | 47 +++++++++++++++++++ .../WindowsMLWrapper/WindowsMLWrapper.h | 8 +++- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp b/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp index c90b10087..aaf65b4f0 100644 --- a/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp +++ b/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp @@ -4,7 +4,7 @@ #include #include -// Function pointer type for the DLL exports +// Function pointer types for the DLL exports typedef char*(__stdcall* GetOrtVersionStringFunc)(); typedef char*(__stdcall* GetTestMessageFunc)(); @@ -18,17 +18,32 @@ int main() } // Get function pointers + GetTestMessageFunc getTestMessage = (GetTestMessageFunc)GetProcAddress(hDll, "GetTestMessage"); GetOrtVersionStringFunc getOrtVersionString = (GetOrtVersionStringFunc)GetProcAddress(hDll, "GetOrtVersionString"); - if (getOrtVersionString == nullptr) + + if (getTestMessage == nullptr || getOrtVersionString == nullptr) { - std::wcout << L"Failed to get GetOrtVersionString function from DLL" << std::endl; + std::wcout << L"Failed to get function pointers from DLL" << std::endl; FreeLibrary(hDll); return 1; } try { - // Call ONNX Runtime version function + // Call GetTestMessage - verifies the ORT runtime initializes correctly + std::wcout << L"Calling GetTestMessage from DLL..." << std::endl; + char* testMsg = getTestMessage(); + if (testMsg != nullptr) + { + std::wcout << L"Test message: " << testMsg << std::endl; + CoTaskMemFree(testMsg); // Clean up string allocated by DLL + } + else + { + std::wcout << L"GetTestMessage returned null" << std::endl; + } + + // Call GetOrtVersionString - demonstrates ORT C API via DLL std::wcout << L"Calling GetOrtVersionString from DLL..." << std::endl; char* versionString = getOrtVersionString(); if (versionString != nullptr) @@ -48,6 +63,7 @@ int main() return 1; } + std::wcout << L"DLL functions called successfully!" << std::endl; FreeLibrary(hDll); return 0; } diff --git a/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp b/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp index 23decb5a1..deca4780a 100644 --- a/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp +++ b/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp @@ -5,10 +5,57 @@ #include #include #include +#include // Include ONNX Runtime headers from WindowsAppSDK.ML package #include +// Initializes an ORT environment to verify the WindowsML runtime is operational. +// Returns a success message if the runtime loads correctly. +extern "C" __declspec(dllexport) char* GetTestMessage() +{ + try + { + // Creating an Ort::Env is the canonical ORT initialization step. + // If this succeeds, the ONNX Runtime and WindowsAppSDK ML components are functional. + Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "DllTest"); + + const char* message = "WindowsML DLL is working correctly!"; + size_t len = strlen(message) + 1; + char* result = static_cast(CoTaskMemAlloc(len)); + if (result) + { + strcpy_s(result, len, message); + } + return result; + } + catch (const Ort::Exception& e) + { + std::string error = "WindowsML DLL test failed: "; + error += e.what(); + size_t len = error.size() + 1; + char* result = static_cast(CoTaskMemAlloc(len)); + if (result) + { + strcpy_s(result, len, error.c_str()); + } + return result; + } + catch (...) + { + const char* fallback = "WindowsML DLL test failed: unknown error"; + size_t len = strlen(fallback) + 1; + char* result = static_cast(CoTaskMemAlloc(len)); + if (result) + { + strcpy_s(result, len, fallback); + } + return result; + } +} + +// Demonstrates the ORT C API (OrtGetApiBase) and CoTaskMemAlloc string management. +// Returns the ONNX Runtime version string. extern "C" __declspec(dllexport) char* GetOrtVersionString() { try diff --git a/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.h b/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.h index cc6992a38..a14022462 100644 --- a/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.h +++ b/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.h @@ -13,7 +13,11 @@ extern "C" { - // Returns the ONNX Runtime version string - // Caller is responsible for freeing the returned string using CoTaskMemFree + // Initializes an ORT environment to verify the WindowsML runtime is operational. + // Caller is responsible for freeing the returned string using CoTaskMemFree. + WINDOWSMLWRAPPER_API char* GetTestMessage(); + + // Returns the ONNX Runtime version string (ORT C API). + // Caller is responsible for freeing the returned string using CoTaskMemFree. WINDOWSMLWRAPPER_API char* GetOrtVersionString(); } From a7c0cd268de4574c94605d6eef36e606c9a13da4 Mon Sep 17 00:00:00 2001 From: Copilot <223556219+Copilot@users.noreply.github.com> Date: Tue, 14 Apr 2026 12:08:21 +0800 Subject: [PATCH 2/2] Address Copilot review: fix calling convention, use API macro, suppress unreferenced warning Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp | 4 ++-- .../CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp b/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp index aaf65b4f0..8ffcfd0bd 100644 --- a/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp +++ b/Samples/WindowsML/cpp/CppConsoleDll/ConsoleClient/ConsoleClient.cpp @@ -5,8 +5,8 @@ #include // Function pointer types for the DLL exports -typedef char*(__stdcall* GetOrtVersionStringFunc)(); -typedef char*(__stdcall* GetTestMessageFunc)(); +typedef char*(*GetOrtVersionStringFunc)(); +typedef char*(*GetTestMessageFunc)(); int main() { diff --git a/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp b/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp index deca4780a..90db64ac6 100644 --- a/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp +++ b/Samples/WindowsML/cpp/CppConsoleDll/WindowsMLWrapper/WindowsMLWrapper.cpp @@ -12,13 +12,14 @@ // Initializes an ORT environment to verify the WindowsML runtime is operational. // Returns a success message if the runtime loads correctly. -extern "C" __declspec(dllexport) char* GetTestMessage() +extern "C" WINDOWSMLWRAPPER_API char* GetTestMessage() { try { // Creating an Ort::Env is the canonical ORT initialization step. // If this succeeds, the ONNX Runtime and WindowsAppSDK ML components are functional. Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "DllTest"); + static_cast(env); // Construction is the test; suppress C4189 const char* message = "WindowsML DLL is working correctly!"; size_t len = strlen(message) + 1; @@ -56,7 +57,7 @@ extern "C" __declspec(dllexport) char* GetTestMessage() // Demonstrates the ORT C API (OrtGetApiBase) and CoTaskMemAlloc string management. // Returns the ONNX Runtime version string. -extern "C" __declspec(dllexport) char* GetOrtVersionString() +extern "C" WINDOWSMLWRAPPER_API char* GetOrtVersionString() { try { @@ -85,7 +86,7 @@ extern "C" __declspec(dllexport) char* GetOrtVersionString() } } -extern "C" __declspec(dllexport) void FreeString(char* str) +extern "C" WINDOWSMLWRAPPER_API void FreeString(char* str) { if (str) {