Code generation between .NET Framework and other frameworks. #823
-
Hello, We have tested some native calls using the NuGet on various frameworks (.NET Framework, .NET Core 3.1, .NET 5, .NET 6); we have noticed that there are interesting differences between the code generated in .NET Framework with the rest. Attached sample generated code: .NET Framework 4.8 public static unsafe Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName,
winmdroot.Storage.FileSystem.FILE_ACCESS_FLAGS dwDesiredAccess,
winmdroot.Storage.FileSystem.FILE_SHARE_MODE dwShareMode,
winmdroot.Security.SECURITY_ATTRIBUTES* lpSecurityAttributes,
winmdroot.Storage.FileSystem.FILE_CREATION_DISPOSITION dwCreationDisposition,
winmdroot.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes,
SafeHandle hTemplateFile)
{
...
} .NET 5 public static unsafe Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName,
winmdroot.Storage.FileSystem.FILE_ACCESS_FLAGS dwDesiredAccess,
winmdroot.Storage.FileSystem.FILE_SHARE_MODE dwShareMode,
winmdroot.Security.SECURITY_ATTRIBUTES? lpSecurityAttributes,
winmdroot.Storage.FileSystem.FILE_CREATION_DISPOSITION dwCreationDisposition,
winmdroot.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes,
SafeHandle hTemplateFile)
{
...
} For this scenario, in the .NET Framework, the fourth parameter (lpSecurityAttributes) must be passed as a pointer, whereas in .NET 5 it is a nullable value type. And this happens with other native calls that we are testing. Is there a reason for this difference between using one framework or another? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
There are differences in code generation across the frameworks, but they aren't the ones you're looking at. On .NET 7, we emit both of these: internal static unsafe Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName, winmdroot.Storage.FileSystem.FILE_ACCESS_FLAGS dwDesiredAccess, winmdroot.Storage.FileSystem.FILE_SHARE_MODE dwShareMode, winmdroot.Security.SECURITY_ATTRIBUTES? lpSecurityAttributes, winmdroot.Storage.FileSystem.FILE_CREATION_DISPOSITION dwCreationDisposition, winmdroot.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes, SafeHandle hTemplateFile)
internal static extern unsafe winmdroot.Foundation.HANDLE CreateFile(winmdroot.Foundation.PCWSTR lpFileName, winmdroot.Storage.FileSystem.FILE_ACCESS_FLAGS dwDesiredAccess, winmdroot.Storage.FileSystem.FILE_SHARE_MODE dwShareMode, [Optional] winmdroot.Security.SECURITY_ATTRIBUTES* lpSecurityAttributes, winmdroot.Storage.FileSystem.FILE_CREATION_DISPOSITION dwCreationDisposition, winmdroot.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes, winmdroot.Foundation.HANDLE hTemplateFile); While on .NET Framework we emit this: internal static unsafe Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(string lpFileName, winmdroot.Storage.FileSystem.FILE_ACCESS_FLAGS dwDesiredAccess, winmdroot.Storage.FileSystem.FILE_SHARE_MODE dwShareMode, winmdroot.Security.SECURITY_ATTRIBUTES? lpSecurityAttributes, winmdroot.Storage.FileSystem.FILE_CREATION_DISPOSITION dwCreationDisposition, winmdroot.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes, SafeHandle hTemplateFile)
internal static extern unsafe winmdroot.Foundation.HANDLE CreateFile(winmdroot.Foundation.PCWSTR lpFileName, winmdroot.Storage.FileSystem.FILE_ACCESS_FLAGS dwDesiredAccess, winmdroot.Storage.FileSystem.FILE_SHARE_MODE dwShareMode, [Optional] winmdroot.Security.SECURITY_ATTRIBUTES* lpSecurityAttributes, winmdroot.Storage.FileSystem.FILE_CREATION_DISPOSITION dwCreationDisposition, winmdroot.Storage.FileSystem.FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes, winmdroot.Foundation.HANDLE hTemplateFile); In general, we emit the same source code. But we make special cases for target framework and language version where necessary so that the code will compile. For instance, we avoid using When you are multi-targeting, you may find setting |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for the quick response, we have tested the tag that you mentioned to us; we create new projects in Visual Studio 2022 using the .NET Framework and .NET 5; the code is generated the same for both cases. However, in a project that works in Visual Studio 2019, we would like to incorporate the NuGet, when generating the code it continues to leave the fourth parameter as a pointer, even if we add the corresponding tag on the .json file. All these tests that I mentioned have been carried out using version 0.2.63-beta. Could it be something related to the source generator feature? Thanks in advance |
Beta Was this translation helpful? Give feedback.
There are differences in code generation across the frameworks, but they aren't the ones you're looking at.
The two CreateFile overloads you refer two are both generated for all frameworks.
On .NET 7, we emit both of these: