from @saschanaz (copied from https://github.com/dotnet/core-setup/issues/6286)
Steps to reproduce
(From https://stackoverflow.com/questions/55980553/is-it-possible-to-execute-arbitrary-c-sharp-code-when-registering-com-dll)
```c#
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace MyNamespace
{
[ComVisible(true)]
[Guid("c60bcc75-8876-4e31-9e4c-a8e82712d8ca")]
public class Code
{
[ComRegisterFunction]
static void Temp()
{
Debug.Assert(false);
}
}
}
```
Expected behavior
Debug message box should appear
Actual behavior
Nothing happens
Environment data
Using VS2019 stable version. Would installing preview one help?
@AaronRobinsonMSFT @jkoritzinsky
@saschanaz This will not work at present. The registration of COM servers no longer executes or loads any managed code. The scenario is designed to perfectly mimic native COM registration. Is this blocking a specific scenario that requires additional registration logic? What additional COM registration steps have you found useful in the past?
The reason this logic isn't presently supported is because on .NET Framework RegAsm.exe is used to register a managed COM server. In the order to simplify scenarios, we opted to closely follow the traditional COM model and solely rely on regsvr32.exe.
I'm trying to mimic Windows IME sample where the code registers itself as an IME (which is required after COM registration, to be deployed as an IME) in DllRegisterServer() and later unregisters in DllUnregisterServer(). Currently it seems I have no way to mimic the behavior.
Specifically this line: https://github.com/Microsoft/Windows-classic-samples/blob/d57fb8ce9fff44fe409a8278c438f710625c3ac2/Samples/IME/cpp/SampleIME/Server.cpp#L278
@saschanaz You are correct in that there is no way to do this with the current method. The only way to do this would be to create another tool that performs the registration independently of regsvr32.exe. Thank you for bringing this up, we may have to consider this for 3.1 or perhaps 3.0, but that will require some more external feedback.
Technically the work wouldn't be too hard since there is already an internal class to communicate with and we know the name of the class that will act as COM server. All that would be required would be to find the method with the attribute and execute it at the appropriate time.
@saschanaz Again, thank you for bringing this up. We are going to handle this for 3.0. It will require coordination across the coreclr and core-setup repos. Once both fixes are in, support should be available in the subsequent nightly build.
Most helpful comment
@saschanaz You are correct in that there is no way to do this with the current method. The only way to do this would be to create another tool that performs the registration independently of
regsvr32.exe. Thank you for bringing this up, we may have to consider this for 3.1 or perhaps 3.0, but that will require some more external feedback.Technically the work wouldn't be too hard since there is already an internal class to communicate with and we know the name of the class that will act as COM server. All that would be required would be to find the method with the attribute and execute it at the appropriate time.