[RESOLVED] COM interop problem between native and managed on x64
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] COM interop problem between native and managed on x64

Hybrid View

  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    [RESOLVED] COM interop problem between native and managed on x64

    I am porting an application from 32-bit to 64-bit (x64 platform) and I have a problem with interop between a native in-proc COM server and a C# application. Everything works nicely when the COM server is used from a C++ app (regardless of 32- or 64-bit), but only works in 32-bit when called from a C# app. The 64-bit version of the COM server DLL and the C# app do not work well together.

    I traced the problem to the marshaling of a custom defined structure. It looks like this (actually I have several, and all behave the same):

    Code:
     
    [uuid(6F13C84D-0E01-48cd-BFD4-F7071A32B49F)] struct S
    {
          long a;
          BSTR b;
          long c;
          BSTR d;
          long e;
          BSTR f;
          BSTR g;
          BSTR h;
          BSTR i;
          long j;
          BSTR k;
          long l;
          BSTR m;
          long n;
    };
    There is a COM method that returns an array of such structures. Its signature is

    Code:
    [id(54)] HRESULT GetListOfStructs(SAFEARRAY(struct S)* arrRes);
    When the call from the COM method returns to the managed app that consumes the COM object, an access violation occurs (in 64-bit). If the BSTR members are left uninitialized, there is no access violation.

    I read that custom structures are problematic, but 64-bit version of midl.exe should be able to handle them. I compiled the .IDL file for x64 with both 32-bit and 64-bit version of midl.exe from Windows SDK v7.0A, and there is no difference. Here is how the proxy/stub file header is when compiled for:

    32-bit (win32) with 32-bit midl.exe
    Code:
     /* File created by MIDL compiler version 7.00.0500 */
    /* at Thu Sep 22 17:52:25 2011
     */
    /* Compiler settings for .\RAC.idl:
        Oicf, W1, Zp8, env=Win32 (32b run)
        protocol : dce , ms_ext, c_ext, robust
        error checks: allocation ref bounds_check enum stub_data 
        VC __declspec() decoration level: 
             __declspec(uuid()), __declspec(selectany), __declspec(novtable)
             DECLSPEC_UUID(), MIDL_INTERFACE()
    */
    //@@MIDL_FILE_HEADING(  )
    
    #if !defined(_M_IA64) && !defined(_M_AMD64)
    64-bit (x64) with both 32-bit and 64-bit midl.exe
    Code:
    /* File created by MIDL compiler version 7.00.0500 */
    /* at Thu Sep 22 17:58:46 2011
     */
    /* Compiler settings for .\RAC.idl:
        Oicf, W1, Zp8, env=Win64 (32b run)
        protocol : dce , ms_ext, c_ext, robust
        error checks: allocation ref bounds_check enum stub_data 
        VC __declspec() decoration level: 
             __declspec(uuid()), __declspec(selectany), __declspec(novtable)
             DECLSPEC_UUID(), MIDL_INTERFACE()
    */
    //@@MIDL_FILE_HEADING(  )    
    
    #if defined(_M_AMD64)
    Any help is greatly appreciated.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: COM interop problem between native and managed on x64

    I got this working. The problem was actually the interop file generated by Visual Studio when adding a reference to the COM object. Instead, I generated the interop file with 64-bit version tlbimp.exe and added a direct reference to the interop assembly. Then it worked. The methods signature was a bit different. Instead of

    Code:
    [DispId(54)]
    void GetListOfStructs(ref Array arrRes);
    the new interop had

    Code:
    [DispId(54)]
    void GetListOfStructs(ref S[] arrRes);
    so a few changes to the code were necessary, but now it
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center