Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4530808
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:50:28+00:00 2026-05-21T13:50:28+00:00

I am using the following function within a WndProc override: Public Function GetPortName() As

  • 0

I am using the following function within a WndProc override:

Public Function GetPortName() As String
    Dim portNameData As String
    Dim comPortNumber As String

    Try
        Dim portSearcher As New ManagementObjectSearcher("\root\CIMV2", "Select Name, PNPDeviceID from Win32_PnPEntity")
        For Each port As System.Management.ManagementObject In portSearcher.Get()

            If port("Name").ToString.ToUpper.Contains("(COM") Then
                portNameData = port("Name").ToString
                comPortNumber = port("Name").ToString.Substring(port("Name").ToString.IndexOf("(COM") + 4)
                comPortNumber = comPortNumber.TrimEnd(")"c)
                If port("PNPDeviceID").ToString.ToUpper.StartsWith("USB\VID_1234&PID_1234") Then
                    Return "COM" & comPortNumber
                End If
            End If
        Next
    Catch ex As Exception
    End Try

    Return ""
End Function

The function works fine, but I am using it in a new application now from a form. On each iteration of the For loop (on the For Each port As System... line), I get this message:

DisconnectedContext was detected

Context 0x607fd8 is disconnected. No proxy will be used to service the request on the COM component. This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.

I think this is a threading issue of some sort. How can I call this function from a form in such a way that this error doesn’t occur?


I found a post on MSDN outlining a very similar problem problem. The guy says:

Sorry, don’t waste your time. The code works fine when it’s run in a simple console app. The problem arose when it was called in a Windows form responding to a DriveDetectorEvent.

I wish I could figure out what is different in a Forms application.


Edit and Possible Solution: Based on the answer at DisconnectedContext MDA when calling WMI functions in single-threaded application I think the solution here is to call GetPortName() outside of WndProc. I will test this tonight and post my result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T13:50:29+00:00Added an answer on May 21, 2026 at 1:50 pm

    After reading the answer at DisconnectedContext MDA when calling WMI functions in single-threaded application it seems that the reason my code doesn’t work is that it is being called from WndProc, blocking WndProc. This causes problems, as marshaling requires pumping messages… but I have blocked.

    For my purposes, simply scanning on a timer works. You may have to find another solution, such as calling it asynchronously on another thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to override a JavaScript function named dismissRelatedLookupPopup(). In Firefox,
I am using the following JavaScript function within IE6: window.onbeforeunload = function() { if
I am using the following jquery code: $(#top ul li.corner).mouseover(function(){ $(span.left-corner).addClass(left-corner-hover); $(span.right-corner).addClass(right-corner-hover); $(span.content).addClass(content-hover); }).mouseout(function(){
I have using the following code for developing tabs. $(document).ready(function() { $('#container-1').tabs(); } ....
I'm using the following JavaScript code: <script language=JavaScript1.2 type=text/javascript> function CreateBookmarkLink(title, url) { if
I am (successfully) calling the Windows FilterSendMessage function in c# using the following pinvoke
How would I convert the following to a VB.NET predicate using Array.Find? Private Function
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I'm using the following function to map columns from a DataTable (passed from the
I'm using the following ajax function: function callAjax(request,callback) { if (window.XMLHttpRequest) {// code for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.