Skip to content

Conversation

Copy link

Copilot AI commented Nov 9, 2025

COM objects were always created in MTA state regardless of [STAThread] attribute or StandardOleMarshalObject inheritance. The main thread running the class factory callbacks was never explicitly initialized for STA.

Changes

ExecutableComServer.cs

  • Call CoInitializeEx(COINIT_APARTMENTTHREADED) in PreMessageLoop() before class factory registration
  • Call CoUninitialize() in PostMessageLoop() for cleanup

SimpleObject.cs

  • Inherit from StandardOleMarshalObject for proper cross-apartment marshaling

Program.cs

  • Add [STAThread] attribute to Main()

NativeMethods.cs

  • Add COM initialization constants: COINIT_APARTMENTTHREADED, S_OK, S_FALSE, RPC_E_CHANGED_MODE

Result

Objects created via SimpleObjectClassFactory.CreateInstance() now inherit STA apartment state from the main server thread:

// PreMessageLoop() now explicitly initializes STA before registration
int hResult = NativeMethods.CoInitializeEx(
    IntPtr.Zero, 
    NativeMethods.COINIT_APARTMENTTHREADED);

// Class factory callbacks execute on STA thread
// Created objects inherit STA apartment state

Clients can verify apartment state using the existing GetProcessThreadId() method combined with thread apartment state inspection tools.

Original prompt

This section details on the original issue you should resolve

<issue_title>Run the COMServer single Apartment state (STA)?</issue_title>
<issue_description>Is there any chance to run the COM server as in STA mode?
I tried these hints found on the internet:

  • add [STAThread] to the Main method
  • derive SimpleObject from StandardOleMarshalObject

But the SimpleObject is always created in MTA state . The documentation says that the apartment state of the created object is overtaken from the calling thread. SimpleObject is created in the callback method SimpleObjectClassFactory,CreateInstance which is called outside of the scope of CSExeCOMServer.Program.

Does anyone have a solution?</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits November 9, 2025 23:36
- Add [STAThread] attribute to Main method
- Add CoInitializeEx call with COINIT_APARTMENTTHREADED in PreMessageLoop
- Add CoUninitialize call in PostMessageLoop
- Add COM initialization constants to NativeMethods
- Make SimpleObject derive from StandardOleMarshalObject for proper STA marshaling

Co-authored-by: rkttu <1297346+rkttu@users.noreply.github.com>
- Fix duplicate hResult declaration in PreMessageLoop
- Add STA_VERIFICATION.md with detailed verification instructions
- Update README.md with apartment threading model section

Co-authored-by: rkttu <1297346+rkttu@users.noreply.github.com>
Co-authored-by: rkttu <1297346+rkttu@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix COMServer to run in single Apartment state Implement STA apartment threading for COM server Nov 9, 2025
Copilot AI requested a review from rkttu November 9, 2025 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run the COMServer single Apartment state (STA)?

2 participants