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 7704117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:36:46+00:00 2026-05-31T23:36:46+00:00

My IDL: [ object, uuid(52D64BCC-03F1-442B-BED1-70992111E2B1), helpstring(ISimpleObject Interface), pointer_default(unique) ] interface ISimpleObject : IUnknown{ [helpstring(method

  • 0

My IDL:

[
    object,
    uuid(52D64BCC-03F1-442B-BED1-70992111E2B1),
    helpstring("ISimpleObject Interface"),
    pointer_default(unique)
]
interface ISimpleObject : IUnknown{
    [helpstring("method Hoop"), local] HRESULT Hoop(void);
};
[
    uuid(3D9ABD55-3C84-43C8-9C34-3915B6B34989),
    version(1.0),
    helpstring("ComServer 1.0 Type Library")
]
library ComServerLib
{
    importlib("stdole2.tlb");
    [
        uuid(42E2236D-1DA0-455F-9EF4-31A4A1E04F47),
        helpstring("SimpleObject Class")
    ]
    coclass SimpleObject
    {
        [default] interface ISimpleObject;
    };
};

My COM class:

class ATL_NO_VTABLE CSimpleObject :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CSimpleObject, &CLSID_SimpleObject>,
    public ISimpleObject
{
public:
    CSimpleObject()
    {
    }

DECLARE_REGISTRY_RESOURCEID(IDR_SIMPLEOBJECT)

BEGIN_COM_MAP(CSimpleObject)
    COM_INTERFACE_ENTRY(ISimpleObject)
END_COM_MAP()

    DECLARE_PROTECT_FINAL_CONSTRUCT()

    HRESULT FinalConstruct()
    {
        return S_OK;
    }

    void FinalRelease()
    {
    }

public:
    // ISimpleObject
    STDMETHOD(Hoop)(void)
    {
        return S_OK;
    }
};

OBJECT_ENTRY_AUTO(__uuidof(SimpleObject), CSimpleObject)

My .NET client:

[ComImport]
[Guid("52D64BCC-03F1-442B-BED1-70992111E2B1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISimpleObject
{
    [PreserveSig]
    int Hoop();
}

[ComImport]
[Guid("42E2236D-1DA0-455F-9EF4-31A4A1E04F47")]
public class SimpleObject
{
}

class Program
{
    static void Main(string[] args)
    {
        SimpleObject simpleObject = new SimpleObject();

        ISimpleObject simpleObjectInterface = (ISimpleObject)simpleObject;

        int returnValue = simpleObjectInterface.Hoop(); // Error!
    }
}

The client gets exception “System.AccessViolationException: Attempted to read or write protected memory…”. Why?

I’m using Visual Studio 2008, Windows Vista x64. C++ and C# projects have x86 configuration.

  • 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-31T23:36:48+00:00Added an answer on May 31, 2026 at 11:36 pm
    int Hoop();
    

    is an incorrect translation of the IDL declaration into C#.
    The correct one would be

    void Hoop();
    

    .NET wraps COM result codes (HRESULT) into correspondent C# exceptions. For example if you return E_NOTIMPL in C++ code, .NET runtime will throw an instance of the NotImplementedException class

    *UPDATE*

    According, to MSDN, the main application thread is initialized to ‘MTA’ by default. Your object CSimpleObject is configured to reside in the ‘STA’. In order to make cross-apartament calls, the COM runtime will use your implementation of the proxy/stub to marshall the corresponding data.

    The runtime needs a valid proxy/stub implementation which is generated by MIDL from your IDL file.

    You have marked the ‘Hoop’ function as “Local”. This means that MIDL will not generate any marshalling code for this method, that is why you get an exception.

    One should remove the ‘local’ attribute since it is designed to be used only inside the server where an object is implemented.
    But as a possible solution I may suggest you to use the object from a thread configured to run in STA. Anyway, this is a bad approach, since it doesn’t guarantees, that no marshalling will be involved.

    You may mark your main thread to run in STA as follows:

    [STAThread()]
    static void Main(string[] args)
    {
        ...
    }
    

    I hope this will help you to solve the problem.

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

Sidebar

Related Questions

I've got a snippet of IDL that looks like this: [ object, uuid(...), pointer_default(unique)
I have a method defined in IDL as follows : interface IMyFunc : IDispatch
I have an IDL file with some interfaces in it. [ object, uuid(newguid), dual,
I need to access a third-party COM server with following interface definition (idl): interface
When you interface a COM object from .NET code, VS creates an interop DLL,
I have an ATL COM object that I am using from C#. The interface
I want to invoke a method on a COM object that is defined in
I'm trying to create the IDL for the IConverterSession interface and I'm confused by
We have a legacy COM DLL with the following simplified IDL for some method:
I'm working on the verification of an interface formalised in the OMG's IDL, and

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.