byte [] buf = new byte [100]; byte [] buf = new byte [100]; 我想将C#类转换为COM,以便我可以在 byte [] buf = new byte [100]; int ret = myProc(buf,0,100); return(ret> 0); } bool SonicBase.IMyClass.StartServer(dProc dlgt) {返回false; } } >公共接口IMyClass { bool StartServer(dProc dlgt); } } -------- -------------------------------------------------- ---------------- ,使用COM: //要使用C#服务器之类的托管代码服务器,我们必须导入公共语言运行库: #import" mscorlib.tlb" raw_interfaces_only #import" EricBase.tlb" no_namespace named_guids extern" C" int ProcData(char * buf,int offset,int size) {返回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(失败创建); 返回; } 尝试 { _dProc * dlgt =(_ dProc *)ProcData; icp-> StartServer(dlgt); } catch(_com_error e) {CS> strtring = e.ErrorMessage(); } WriteConsole(hOut,b\ n,2,& count,NULL); CoUninitialize( ); } 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 aexception.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();} 解决方案 Hmm... this is tricky stuff.You can''t pass a C function ptr and use it directly as a delegate pointer,you have to pass it as a long (C/C++) and marshal this value as aFuctionPtr.Here''s what you should do. 1. change you C# code.. public interface IMyClass{bool StartServer(dProc dlgt);}into: public interface IMyClass{bool StartServer([MarshalAs(UnmanagedType.FunctionPtr)]dProc dlgt);} 2. Your C++ code: icp->StartServer(dlgt);into:// reinterpret_cast - You know what you are doing, don''t you?icp->StartServer(reinterpret_cast<long>(dlgt)); Why do you implement an explicit interface for the method, there is no needto do this just declare your interface methods as public. Or am I missingsomething? Willy."Eric" <wa**********@msn.com> wrote in messagenews:ub**************@TK2MSFTNGP15.phx.gbl... 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(); }Check out the information at: http://msdn.microsoft.com/library/de...nentstocom.asp Also checkout the interop news group:microsoft.public.dotnet.framework.interop"Eric" <wa**********@msn.com> wrote in messagenews:ub**************@TK2MSFTNGP15.phx.gbl... 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(); }Hi, Willy, After modifying the code, I got another exception saying that "invalidagument", I guessed there would be some mismatch between delegate dProc andfunctionPtr ProcData, but I don''t know what''s wrong. I looked into the MSDN, it gaves only the samples using explicitinterface. Would you please give me a sample without an explicit interface?Using interface to export a C# class into COM, is troublesome. It would be agreat pleasure not using the explicit interface here. Thanks,Eric"Willy Denoyette [MVP]" <wi*************@pandora.be> D′è?óê?tnews:ux**************@TK2MSFTNGP14.phx.gbl... Hmm... this is tricky stuff. You can''t pass a C function ptr and use it directly as a delegate pointer, you have to pass it as a long (C/C++) and marshal this value as a FuctionPtr. Here''s what you should do. 1. change you C# code.. public interface IMyClass { bool StartServer(dProc dlgt); } into: public interface IMyClass { bool StartServer([MarshalAs(UnmanagedType.FunctionPtr)]dProc dlgt); } 2. Your C++ code: icp->StartServer(dlgt); into: // reinterpret_cast - You know what you are doing, don''t you? icp->StartServer(reinterpret_cast<long>(dlgt)); Why do you implement an explicit interface for the method, there is noneed to do this just declare your interface methods as public. Or am I missing something? Willy. "Eric" <wa**********@msn.com> wrote in message news:ub**************@TK2MSFTNGP15.phx.gbl... hi, I want to convert a C# class into COM, so that I can use the class inC++. 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(); } 这篇关于无法将C#类转换为COM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 20:18