'System.ArgumentException: Value does not fall within the expected range' when using Windows Forms user control within VB6 form via COM interop
- Dominant language
- C#
- Stars
- 4.9k
- Forks
- 1.1k
- Avg merge
- 20h 23m
- Merged PRs (30d)
- 103
Description
### .NET version
.NET 8.0 preview 5
### Did it work in .NET Framework?
Yes
### Did it work in any of the earlier releases of .NET Core or .NET 5+?
.NET 7
### Issue description
Using a Windows Forms user control embedded into a VB6 form via COM interop fails, with an exception:
System.ArgumentException: Value does not fall within the expected range.
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
at Windows.Win32.System.Com.IGlobalInterfaceTable.RegisterInterfaceInGlobal(IUnknown* pUnk, Guid* riid, UInt32* pdwCookie)
at Windows.Win32.Foundation.GlobalInterfaceTable.RegisterInterface[TInterface](TInterface* interface)
at Windows.Win32.Foundation.AgileComPointer`1..ctor(TInterface* interface, Boolean takeOwnership)
at System.Windows.Forms.Control.ActiveXImpl.InPlaceActivate(OLEIVERB verb)
at System.Windows.Forms.Control.ActiveXImpl.OnFocus(Boolean focus)
at System.Windows.Forms.Control.ActiveXOnFocus(Boolean focus)
at System.Windows.Forms.Control.ChildGotFocus(Control child)
at System.Windows.Forms.Control.OnGotFocus(EventArgs e)
at System.Windows.Forms.TextBox.OnGotFocus(EventArgs e)
at System.Windows.Forms.Control.WmSetFocus(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.TextBox.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, WM msg, WPARAM wparam, LPARAM lparam)
### Steps to reproduce
I built a minimal repro application based on the code found in: https://github.com/dotnet/samples/tree/main/core/extensions/COMServerDemo
[ComInteropTest.zip](https://github.com/dotnet/winforms/files/12026351/ComInteropTest.zip)
To reproduce the issue, unzip the sample application to some folder and build the COM interop DLL using "dotnet build /p:UseNet8=True". After the build is done, register the created COM interop DLL according to the printed instructions. For example (this must be done in elevated command prompt):
regsvr32.exe "C:\Repro\ComInteropTest\COMServer\bin\x86\Debug\COMServer.comhost.dll"
After registering the built COM interop DLL, you can reproduce the issue by running the included VB6 sample app in the folder "VB6TestClient" (source code is included). The folder includes a pre-built binary "Project1.exe", which when run opens a form, with a single command button. Clicking on the button opens another form with the embedded user control on it, which fails with the "System.ArgumentException: Value does not fall within the expected range" exception.
If you unregister the built DLL and re-build the project using "dotnet build", it will be built against .NET 7. If you then re-register the DLL and run the VB6 sample app, the form will open with no exceptions.
The code to embed the User Control within VB6 form is based on code from "Interop Toolkit 2.1" published by Microsoft a long time ago and it works on full .NET framework and still used to work on .NET 7, so I would imagine this to be a regression in some part of the stack. At least I didn't find any breaking changes notes that seem to be related to this issue.
Contributor guide
Assessment
This issue has not been assessed yet.