.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

what is com marshalling and com interoprability in .net

Posted By:      Posted Date: September 03, 2010    Points: 0   Category :ASP.Net
please any one tell me what is com marshalling and com interoprability in .net with some basic exaple

View Complete Post

More Related Resource Links

Marshalling Your Data: Efficient Data Transfer Techniques Using COM and Windows 2000


The way you choose to transfer data is vitally important in a distributed application. Windows 2000 provides several new features that allow you to transfer data more efficiently. Lightweight handlers allow you to write smart proxies that can cache results and perform buffered reads and writes, minimizing the number of network calls. Windows 2000 also allows you to use pipe interfaces to transfer large amounts of data efficiently through a read-ahead facility. This article illustrates several ways to improve data transfer in Windows 2000 using these new features. It also reports the results of transfer time tests and provides recommendations for transferred buffer sizes.

Richard Grimes

MSDN Magazine September 2000

P/Invoke Marshalling AnsiBstr to string x86 vs x64

I am developing a C# application that uses P/Invoke to call methods on a DLL.  I have a compiler directive that references the x64 version of the DLL and prototypes using a conditional statement when I set my project target architecture to x64.  Otherwise, the 32-bit version of the DLL is loaded when target is x86 or Any.The prototype for the method is: [DllImport("vcmowr64.dll")] internal static extern Int32 wcmo_init( [MarshalAs(UnmanagedType.AnsiBStr), In()] ref string WCMOarg_Handle, [MarshalAs(UnmanagedType.AnsiBStr), In()] ref string WCMOarg_User, [MarshalAs(UnmanagedType.AnsiBStr), Out()] out string WCMOarg_DataOut, [MarshalAs(UnmanagedType.AnsiBStr), Out()] out string WCMOarg_ErrOut); public static string Init(string handle, string user) { string dataOut; string errorOut; if (NativeMethods.wcmo_init(ref handle, ref user, out dataOut, out errorOut) != 0) { throw new VcmoProxyException(string.Format(VcmoErrorMsg, "wcmo_init",errorOut)); } return dataOut; } The only difference in the code when target is x86 is the DLL name.I can run the x86 version locally and everything works as expected.  However, after compiling the x64 version and deploying to the server,

Interop Marshalling of C++ with pointers in the structure

I have a vendor supplied dll that is written in C++. I need to use it in a C# application and am trying to use Interop. I have .h files and example .cpp code from the SDK, but I'm clearly in over my head. There are dozens of functions defined in this API, but if I can just get started on how to do this, I'd appreciate some guidance. My biggest headeache is pointers. Here's examples of the C++ and how far I got with C# (which i'm sure I totally botched) MY.h: part of the included headers typedef struct my_handle *MY_Handle; MY_API_PUBLIC int _cdecl MY_HandleCreate(MY_Handle *my_handle, const char *serial_number, const char *client_id, const char *product_type, const char *product_version); MY_API_PUBLIC int _cdecl MY_HandleDestroy(MY_Handle *my_handle); MY_API_PUBLIC int _cdecl MY_Init(void); MY_API_PUBLIC int _cdecl MY_Shutdown(void); MY.cpp: some example functions. int MY_Init(void); int MY_HandleCreate(MY_Handle *my_handle, const char *serial_number, const char *client_id, const char *product_type, const char *product_version); int MY_HandleDestroy(MY_Handle *my_handle); int MY_Shutdown(void); MY.cs: The class created to house the interop stuff. class MYClass { //MY_Init() [DllImport("ts.dll", SetLastError = true)] public static extern int MY_Init(); //MY_Shutdown() [DllImport("ts.dll", SetLastError

Marshalling error in ASP.Net, COM dll


I've a ASP.Net web application that uses a Win32 COM dll through Com Callable Wrappers. The dll in turn makes CORBA calls. I'm seeing intermittent marshalling error in the CORBA calls made from the DLL. The error occurs only when the DLL is used in the context of a web application. When I use the same DLL in a windows form, the method invocation succeeds every time.

CORBA server log shows similar kind of trace messages for method invocations from the web and desktop app. 

We use Windows Server 2008 64 bit, IIS7, ASP.Net 2.0 & OmniOrb 4.1.4. Our application is 32 and is hosted by Classic Application pool in IIS with support enabled for 32 bit applications.

Appreciate any clues to figure out the problem.

Thanks in advance,


threaded event marshalling pattern


Hi all,

I'm curious about common / recommended patterns for using events in multithreaded Windows Forms applications.

As an example, I have an application with several worker threads which communicate with other applications over a network. These worker threads generate a fairly large number of events which both the GUI thread and other worker threads subscribe to. This model has two challenges:

  1. C# "events" are not inherently thread-safe. So instead of this:

    public event EventHandler SomethingHappened;
    protected void RaiseSomethingHappened()
      if( SomethingHappened != null )
        SomethingHappened( this, EventArgs.Empty );

    In order to protect against the case where two threads are simultaneously accessing the event's backing field, I actually have to do this:
    public event EventHandler SomethingHappened
     add { lock( this ) mSomethingHappened += value; }
     remove { lock( this ) mSomethingHappened -= value; }
    protected void RaiseSomethingHappened()
     EventHandler handler;
     lock( this ) handler = mSomethingHappened;
     if( handler != null ) handler( this, EventArgs.Empty );
    private EventHandler mSomethingHappened;

  2. Since GUI operations always have to happen on the GUI thread, I have to use BeginInvoke() everywhere. If a Form subscribes to my SomethingHappened e

Marshalling 2 dimensional arrays from COM



I have a COM component which pushes a 2 dimensional double array to a client.

The IDL is:


interface IMyClient : IUnknown
  [helpstring("method CollectData")] HRESULT CollectData([in] long channel_count,
                                [in] long data_count,
                                [in, size_is(channel_count)] long* pChannels,
                                [in, size_is(channel_count, data_count)] double** pData);

 In C# the method is implemented as

public void CollectData(int channel_count, int data_count, ref int pChannels, IntPtr pData)
Does anyone know how I can read the pData values passed up from the COM component as doubles?

Thank you

Problem marshalling the following structure



I'm experiencing a problem in c# tryin to marshall a pointer to a structure inside another structure

I'm trying to call the following function hosted in a C++ library:

HRESULT GetNextRow(MYUSERDATA *userdata);

typedef struct _LDAPATTRIBUTE
  WCHAR attributeName[520];
  WCHAR attributeValue[520];

typedef struct _MYUSERDATA
  WCHAR objectGUID[80];
  WCHAR distinguishedName[520];
  PLDAPATTRIBUTE attributesList;

In C#, I have created the following definition:


    const int MAX_PATH = 260 * 2;
    const int MAX_OBJECTGUID = 40 * 2;

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct LDAPATTRIBUTE
      /// WCHAR[520]
      [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]

Create Interop dll from type library (Tlbimp) without marshalling attributes



I have the idl of com component, I generate type library using midl and use type library to generate interop dll. I want to create interop dll without any marshalling attribute, for example BSTR should be returned as IntPtr, not as string after marshalling. I could not find any attirbute in TlbImp to control the marshalling, how can I achieve this requirement?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend