Calling a .NET object from C++

1. Follow the rules in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcominteropsamplecomclientnetserver.asp

2. For the .NET interface the following attributes are necessary
[ComVisible(true)] [Guid(“e15af71b-9860-36e5-af62-9f405c231daa”)] public interface ILoan
{
}

3. The interface assembly should be strong named and registered in the GAC
[assembly:AssemblyKeyFile(@”..\..\..\sample.snk”)]
Use:
gacutil -i NETInt.dll

4. For the .NET class the following attributes are necessary [ClassInterface(ClassInterfaceType.AutoDual)] [ComVisible(true)] [Guid(“18fe2e0a-1130-388b-9f82-171909823cc2″)] public class Loan : ILoan
{
}

5. The .NET class assembly should be strong named and registered in the GAC[assembly:AssemblyKeyFile(@”..\..\..\sample.snk”)]Use:gacutil -i NETclass.dll

6. The .cpp client should
#import “LoanLib\LoanLib.tlb” raw_interfaces_only, no_namespace

7. Two methods of smart ptr creating the .NET object
// Method #1: Declaring and instantiating a Loan object _LoanPtr pILoan( __uuidof(Loan ) );
OR
// Method #2: Declaring and instantiating a Loan object _LoanPtr pILoan = NULL; HRESULT hr = S_OK;
hr = pILoan.CreateInstance( __uuidof(Loan ) );

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: