Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <windows.h>
#include <iostream>

// Function pointer type for the DLL exports
typedef char*(__stdcall* GetOrtVersionStringFunc)();
typedef char*(__stdcall* GetTestMessageFunc)();
// Function pointer types for the DLL exports
typedef char*(*GetOrtVersionStringFunc)();
typedef char*(*GetTestMessageFunc)();

int main()
{
Expand All @@ -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)
Expand All @@ -48,6 +63,7 @@ int main()
return 1;
}

std::wcout << L"DLL functions called successfully!" << std::endl;
FreeLibrary(hDll);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,59 @@
#include <windows.h>
#include <combaseapi.h>
#include <cstring>
#include <string>

// Include ONNX Runtime headers from WindowsAppSDK.ML package
#include <winml/onnxruntime_cxx_api.h>

extern "C" __declspec(dllexport) char* GetOrtVersionString()
// Initializes an ORT environment to verify the WindowsML runtime is operational.
// Returns a success message if the runtime loads correctly.
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");
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ort::Env env(...) is never referenced after construction. With /W4 and TreatWarningAsError enabled for this project, this is likely to trigger an "unreferenced local variable" warning and fail the build. Either explicitly mark it as used (e.g., cast to void) or restructure the code so the env object is actually referenced.

Suggested change
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "DllTest");
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "DllTest");
(void)env;

Copilot uses AI. Check for mistakes.
static_cast<void>(env); // Construction is the test; suppress C4189

const char* message = "WindowsML DLL is working correctly!";
size_t len = strlen(message) + 1;
char* result = static_cast<char*>(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<char*>(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<char*>(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" WINDOWSMLWRAPPER_API char* GetOrtVersionString()
{
try
{
Expand Down Expand Up @@ -38,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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Loading