.Net 5.0 - Method not found "System.Object.System.Runtime.InteropService.Marshal.GetActiveObject(Stem.String)

Nadeem Bader 21 Reputation points
2022-11-03T00:59:47.267+00:00

Hi
While working with .Net 4.8, the following code works as expected:

var tcObject = Marshal.GetActiveObject(toolFullName);  
```But after moving my project to .Net 5.0, I'm getting the following error meager:    
"Method not found "System.Object.System.Runtime.InteropService.Marshal.GetActiveObject(Stem.String)"    
    
Am I missing any reference to my project? Or I should use different code in order to make it works in .Net 5.0?     
    
    
Can you please assist?    
    
Best Regards,  
Nadeem Bader   
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,292 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 33,451 Reputation points Microsoft Vendor
    2022-11-03T01:54:12.227+00:00

    Hi @Nadeem Bader ,
    According to the document Marshal.GetActiveObject(String) Method, it only applies to .Net Framework.
    To use it in .Net 5, you can check the following link.
    https://github.com/dotnet/runtime/issues/37617
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. RLWA32 47,196 Reputation points
    2022-11-03T01:55:02.377+00:00

    In .Net 5 and 6 the GetActiveObject method was removed from the Marshal class in the System.Runtime.InteropServices namespace.

    You can use p/invoke with the Windows API GetActiveObject function to retrieve an interface pointer for an object from the running object table.

    0 comments No comments

  3. Nadeem Bader 21 Reputation points
    2022-11-03T09:09:15.647+00:00

    HI
    Thank you for your replies.
    I'm using c# code. Can you please show me how to do that in code?
    Best Regards,
    Nadeem Bader


  4. RLWA32 47,196 Reputation points
    2022-11-03T10:34:26.24+00:00

    Using .Net 5 -

       [DllImport("Oleaut32.dll", CallingConvention = CallingConvention.StdCall, PreserveSig = true)] static extern int GetActiveObject(ref Guid clsid, IntPtr reserved, out IntPtr punk);  
    

    ITestObject is a COM interface obtained from the running object table -

       Guid clsid = new Guid("FBD3B83F-DAC1-431E-9C22-42C3F593620D");  
          IntPtr unk;  
         
         var hr = GetActiveObject(ref clsid, IntPtr.Zero, out unk);  
         
          if (hr == 0)  
          {  
                ITestObject iFace = (ITestObject)Marshal.GetObjectForIUnknown(unk);  
                //Rest of Code  
          }  
    

  5. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.