none
Create instance for a COM object RRS feed

  • Question

  • Hi,

    Im trying a sample application in VC++.Net(2010) that consumes a COM component. That COM component is created in C#.net 2010. The component was created & registered successfully in registry & GAC.  WHen trying to consume it in VC++ .net im getting compilation error. I have attached the code of  both the sample applications(The C#.net & the client application that is in vc++.net)

    C#.Net code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace NewApptrial4com
    {
      [ComVisible(true)]
      [Guid("89c591eb-09a5-460a-b6f8-735e754d7baa")]
      public interface ApptryclassInt
      {
        void Displaymessage();
        void DispMsg();
      }
    
      [ComVisible(true)]
      [Guid("70e543dc-ee15-4859-809d-e9585fe71275")]
      public class Apptryclass : ApptryclassInt
      {
        public Apptryclass()
        { 
          //
        }
    
        public void DispMsg()    
        { 
    
        }
    
        public void Displaymessage()
        {
          //MessageBox.Show("Apptryclass");
        }
      }
    }
    
    

     

    C#.Net assemblyinfo.cs

    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    // General Information about an assembly is controlled through the following 
    // set of attributes. Change these attribute values to modify the information
    // associated with an assembly.
    [assembly: AssemblyTitle("NewApptrial4com")]
    [assembly: AssemblyDescription("")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("Microsoft")]
    [assembly: AssemblyProduct("NewApptrial4com")]
    [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    [assembly: AssemblyKeyFile("strkey.snk")]
    
    // Setting ComVisible to false makes the types in this assembly not visible 
    // to COM components. If you need to access a type in this assembly from 
    // COM, set the ComVisible attribute to true on that type.
    [assembly: ComVisible(true)]
    
    // The following GUID is for the ID of the typelib if this project is exposed to COM
    [assembly: Guid("bf92ae2b-e2a0-47c1-8665-9ffafe53c0a3")]
    
    // Version information for an assembly consists of the following four values:
    //
    //   Major Version
    //   Minor Version 
    //   Build Number
    //   Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers 
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
    
    

     

    VC++.Net (Client Application)

    // ConsumeTrial1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #import "C:\\Users\\mmanickam\\Documents\\Visual Studio 2010\\Projects\\NewApptrial4com\\NewApptrial4com\\bin\\Release\\newapptrial4com.tlb"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	CoInitialize(NULL); 
    	NewApptrial4com::ApptryclassIntPtr pDotNetCOMPtr;
    <span style="text-decoration:underline"><strong>	HRESULT hres= pDotNetCOMPtr.CreateInstance(NewApptrial4com::_ApptryclassPtr);
    </strong></span>	return 0;
    }
    
    
    

    The line showing error is in bold and underlined. I'm getting error type name is not allowed.

     

    Please let me know what can be done to solve this issue

    Tuesday, July 12, 2011 11:22 AM

Answers

  • Try this:

     HRESULT hres= pDotNetCOMPtr.CreateInstance(__uuidof(NewApptrial4com::_Apptryclass));

    or something similar.  CreateInstance takes the GUID of the class you are creating.


    • Marked as answer by Rob Pan Thursday, August 4, 2011 9:15 AM
    Tuesday, July 12, 2011 1:23 PM

All replies

  •  

    i also tried the below code but it too doesnot seem to work.

     

    int _tmain(int argc, _TCHAR* argv[])
    {
    	CoInitialize(NULL); 
    	NewApptrial4com::ApptryclassIntPtr pDotNetCOMPtr;
    	HRESULT hres= pDotNetCOMPtr.CreateInstance(NewApptrial4com::_ApptryclassPtr);
    	return 0;
    }
    
    
    


    in both the cases im getting error in the line

     HRESULT hres= pDotNetCOMPtr.CreateInstance(NewApptrial4com::_ApptryclassPtr);

     

    Tuesday, July 12, 2011 12:03 PM
  • Try this:

     HRESULT hres= pDotNetCOMPtr.CreateInstance(__uuidof(NewApptrial4com::_Apptryclass));

    or something similar.  CreateInstance takes the GUID of the class you are creating.


    • Marked as answer by Rob Pan Thursday, August 4, 2011 9:15 AM
    Tuesday, July 12, 2011 1:23 PM
  • Spencer is correct. You need to pass the class GUID. You can examine the contents of the generated *.tlh file to check to see if you got the spelling of the GUID name correct.

    Moreover, you can examine the HR value to see what the error code is, if the value is negative. Post it here if you continue to have problems.

    Tuesday, July 12, 2011 4:04 PM
  • Hi Spencer,

    CreateInstance solved the compilation error. Can pls let me know how to read the values returned by the COM object in the VC++ application. for ex: if the C# method returns a string the how to read the string in vc++ and display the value.

    Wednesday, July 13, 2011 10:58 AM