E
Eric
hi,
I want to convert a C# class into COM, so that I can use the class in C++.
The codes compile and link well. But when I run the program, I got a
exception.
Any comment is welcome. Thanks in advance.
Eric
----------------------------------------------------------------------
in C#, compile into a COM :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;
namespace SonicBase
{
public delegate int dProc(byte[] buf, int offset, int size);
public class MyClass : IMyClass
{
private dProc myProc = null;
bool StartServer(dProc dlgt)
{
myProc = dlgt;
byte[] buf = new byte[100];
int ret = myProc(buf, 0, 100);
return (ret > 0);
}
bool SonicBase.IMyClass.StartServer(dProc dlgt)
{
return false;
}
}
public interface IMyClass
{
bool StartServer(dProc dlgt);
}
}
--------------------------------------------------------------------------
in C++, use the COM :
// To use managed-code servers like the C# server,
// we have to import the common language runtime:
#import "mscorlib.tlb" raw_interfaces_only
#import "EricBase.tlb" no_namespace named_guids
extern "C"
int ProcData(char* buf, int offset, int size)
{
return 0;
}
void CMainFrame::OnTest()
{
CoInitialize(NULL);
IMyClass* icp = NULL;
HRESULT hr = CoCreateInstance(CLSID_MyClass,
NULL, CLSCTX_INPROC_SERVER,
IID_IMyClass, reinterpret_cast<void**>(&icp));
DWORD count=0;
AllocConsole();
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsole(hOut, "a\n", 2, &count, NULL);
if(FAILED(hr))
{
AfxMessageBox("fail create");
return;
}
try
{
_dProc* dlgt = (_dProc*)ProcData;
icp->StartServer(dlgt);
}
catch(_com_error e)
{
CString strInfo = e.ErrorMessage();
}
WriteConsole(hOut, "b\n", 2, &count, NULL);
CoUninitialize();
}
I want to convert a C# class into COM, so that I can use the class in C++.
The codes compile and link well. But when I run the program, I got a
exception.
Any comment is welcome. Thanks in advance.
Eric
----------------------------------------------------------------------
in C#, compile into a COM :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections;
namespace SonicBase
{
public delegate int dProc(byte[] buf, int offset, int size);
public class MyClass : IMyClass
{
private dProc myProc = null;
bool StartServer(dProc dlgt)
{
myProc = dlgt;
byte[] buf = new byte[100];
int ret = myProc(buf, 0, 100);
return (ret > 0);
}
bool SonicBase.IMyClass.StartServer(dProc dlgt)
{
return false;
}
}
public interface IMyClass
{
bool StartServer(dProc dlgt);
}
}
--------------------------------------------------------------------------
in C++, use the COM :
// To use managed-code servers like the C# server,
// we have to import the common language runtime:
#import "mscorlib.tlb" raw_interfaces_only
#import "EricBase.tlb" no_namespace named_guids
extern "C"
int ProcData(char* buf, int offset, int size)
{
return 0;
}
void CMainFrame::OnTest()
{
CoInitialize(NULL);
IMyClass* icp = NULL;
HRESULT hr = CoCreateInstance(CLSID_MyClass,
NULL, CLSCTX_INPROC_SERVER,
IID_IMyClass, reinterpret_cast<void**>(&icp));
DWORD count=0;
AllocConsole();
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsole(hOut, "a\n", 2, &count, NULL);
if(FAILED(hr))
{
AfxMessageBox("fail create");
return;
}
try
{
_dProc* dlgt = (_dProc*)ProcData;
icp->StartServer(dlgt);
}
catch(_com_error e)
{
CString strInfo = e.ErrorMessage();
}
WriteConsole(hOut, "b\n", 2, &count, NULL);
CoUninitialize();
}