Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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,7 +4,7 @@
#include <windows.h>
#include <iostream>

// Function pointer type for the DLL exports
// Function pointer types for the DLL exports
typedef char*(__stdcall* GetOrtVersionStringFunc)();
typedef char*(__stdcall* GetTestMessageFunc)();
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.

The DLL exports are declared/defined with the default calling convention (i.e., __cdecl), but the client casts them to __stdcall function pointers. That mismatch is harmless on x64/ARM64 but will cause stack corruption if an x86 configuration is ever added. Align the calling convention by either removing __stdcall from the typedefs or explicitly exporting the functions with a matching convention (and reflecting that in the header).

Suggested change
typedef char*(__stdcall* GetOrtVersionStringFunc)();
typedef char*(__stdcall* GetTestMessageFunc)();
typedef char*(* GetOrtVersionStringFunc)();
typedef char*(* GetTestMessageFunc)();

Copilot uses AI. Check for mistakes.

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,10 +5,57 @@
#include <windows.h>
#include <combaseapi.h>
#include <cstring>
#include <string>

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

// 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()
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.

WindowsMLWrapper.h already declares GetTestMessage with WINDOWSMLWRAPPER_API, but the definition hard-codes __declspec(dllexport). To keep import/export attributes centralized and avoid accidental mismatches across build configurations, prefer defining the function without repeating __declspec(...) (or use WINDOWSMLWRAPPER_API consistently in the definition).

Suggested change
extern "C" __declspec(dllexport) char* GetTestMessage()
extern "C" char* GetTestMessage()

Copilot uses AI. Check for mistakes.
{
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.

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" __declspec(dllexport) char* GetOrtVersionString()
{
try
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