Skip to content

Commit 1fb03cc

Browse files
Copilotsnnn
andauthored
Fix DllImport library names for case-sensitive filesystems (#26415)
On case-sensitive filesystems (Windows with WSL, developer mode, or per-directory case sensitivity), DllImport fails to load native libraries due to relying on .NET's automatic platform-specific extension/prefix addition, which can produce incorrect casing. ## Changes - **NativeMethods.shared.cs**: Changed desktop platform library names from `"onnxruntime"` to `"onnxruntime.dll"` and `"ortextensions"` to `"ortextensions.dll"` - Explicitly specifying extensions ensures consistent behavior across case-sensitive and case-insensitive filesystems - Android/iOS platform-specific names unchanged ## Impact **Windows**: No changes required - libraries already named `onnxruntime.dll` **Linux/macOS**: Native packaging may need updates to provide `onnxruntime.dll` in runtime folders (either as actual filename or symlink to `libonnxruntime.so`/`libonnxruntime.dylib`) ```csharp // Before (relied on automatic extension addition) internal const string DllName = "onnxruntime"; // After (explicit extension for consistency) internal const string DllName = "onnxruntime.dll"; ``` Fixes #23509 <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Does not work on case-sensitive filesystems</issue_title> > <issue_description>### Describe the issue > > Library does not work on case-sensitive filesystems. We get: > > ``` > Unhandled exception. System.TypeInitializationException: The type initializer for 'Microsoft.ML.OnnxRuntime.NativeMethods' threw an exception. > ---> System.EntryPointNotFoundException: Unable to find an entry point named 'OrtGetApiBase' in DLL 'onnxruntime'. > at Microsoft.ML.OnnxRuntime.NativeMethods.OrtGetApiBase() > at Microsoft.ML.OnnxRuntime.NativeMethods..cctor() > --- End of inner exception stack trace --- > at Microsoft.ML.OnnxRuntime.SessionOptions..ctor() > at Microsoft.ML.OnnxRuntime.InferenceSession..ctor(String modelPath) > at Program.<Main>$(String[] args) in Z:\temp\onnxtest\Program.cs:line 1 > ``` > > Probably due to a mistyped filename somewhere. > > ### To reproduce > > Create new C# project, use this Program.cs: > > ``` > new Microsoft.ML.OnnxRuntime.InferenceSession(""); > ``` > > > ### Urgency > > _No response_ > > ### Platform > > Windows > > ### OS Version > > Microsoft Windows [Version 10.0.19045.6332] > > ### ONNX Runtime Installation > > Released Package > > ### ONNX Runtime Version or Commit ID > > 1.22.1 > > ### ONNX Runtime API > > Other / Unknown > > ### Architecture > > X64 > > ### Execution Provider > > Other / Unknown > > ### Execution Provider Library Version > > _No response_</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > <comment_new><author>@snnn</author><body> > I checked the code, still do not have much clue. We didn't specify the extension name. Maybe it was added by the .net runtime. > > https://github.com/microsoft/onnxruntime/blob/main/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs#L831</body></comment_new> > <comment_new><author>@snnn</author><body> > > Maybe we should just specify explicit extensions then in DllImport > > That might be the easiest fix. Would like to submit a PR?</body></comment_new> > <comment_new><author>@snnn</author><body> > Don't close it.</body></comment_new> > </comments> > </details> - Fixes #26129 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: snnn <[email protected]> Co-authored-by: Changming Sun <[email protected]>
1 parent 4de6893 commit 1fb03cc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,13 +866,14 @@ static NativeMethods()
866866
internal class NativeLib
867867
{
868868
#if __ANDROID__
869-
// define the library name required for android
869+
// Define the library name required for Android
870870
internal const string DllName = "libonnxruntime.so";
871871
#elif __IOS__
872-
// define the library name required for iOS
872+
// Define the library name required for iOS
873873
internal const string DllName = "__Internal";
874874
#else
875-
internal const string DllName = "onnxruntime";
875+
// Note: the file name in ONNX Runtime nuget package must be onnxruntime.dll instead of onnxruntime.DLL(Windows filesystem can be case sensitive)
876+
internal const string DllName = "onnxruntime.dll";
876877
#endif
877878
}
878879

@@ -2951,7 +2952,9 @@ internal static class OrtExtensionsNativeMethods
29512952
#elif __IOS__
29522953
internal const string ExtensionsDllName = "__Internal";
29532954
#else
2954-
internal const string ExtensionsDllName = "ortextensions";
2955+
// For desktop platforms, explicitly specify the DLL name with extension to avoid
2956+
// issues on case-sensitive filesystems. See NativeLib.DllName for detailed explanation.
2957+
internal const string ExtensionsDllName = "ortextensions.dll";
29552958
#endif
29562959

29572960
[DllImport(ExtensionsDllName, CharSet = CharSet.Ansi,

0 commit comments

Comments
 (0)