Hi Nick,
Currently I understand that you would like to create a .NET class and
expose it to COM and call it from C++.
If I misunderstand, please let me know.
Here I write some code snippet for your reference.
1. .NET Server
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace dotNETServer
{
[InterfaceType(ComInterfaceType.InterfaceIsDual),ComVisible(true)]
public interface ITest
{
string HelloWorld();
}
[ClassInterface(ClassInterfaceType.None),ComVisible(true)]
public class TestClass :ITest
{
#region ITest Members
public string HelloWorld()
{
return "Hello World!";
}
#endregion
}
}
2. C++ client
// TestConsole.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#import "..\\dotNETServer\\bin\\Debug\\dotNETServer.tlb"
#include <comdef.h>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
dotNETServer::ITestPtr pdotNETServer;
HRESULT hr =
pdotNETServer.CreateInstance(__uuidof(dotNETServer::TestClass));
try
{
if FAILED(hr)
{
_com_issue_error(hr);
}
else
{
_bstr_t resultStr = pdotNETServer->HelloWorld();
printf("%S\n",(LPCWSTR)resultStr);
}
}
catch(_com_error &e)
{
printf("%S\n", e.ErrorMessage());
}
CoUninitialize();
return 0;
}
You may have a try and let me know the result.
BTW: here is a link for your reference.
..NET - COM Interoperability
http://www.codeproject.com/dotnet/COM_DOTNET_INTEROP.asp?print=true Best regards,
Perter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.