在ATL / COM之间传递结构的SAFEARRAY - C# [英] Passing SAFEARRAY of Structure between ATL/COM - C#
问题描述
我试图从C# - ATL / COM传递结构的SAFEARRAY。我没有成功。可以告诉我,我在代码中缺少什么?或者它不可能?
I tried to pass SAFEARRAY of structures from C# - ATL/COM. I didn't have a success. Can anybody tell me, what am I missing in the code? or it is not possible?
// testATLCOMSvr.idl : IDL source for testATLCOMSvr//// This file will be processed by the MIDL tool to// produce the type library (testATLCOMSvr.tlb) and marshalling code.import "oaidl.idl";import "ocidl.idl";[ object, uuid(), oleautomation, nonextensible, pointer_default(unique)]interface IATLTest : IUnknown{ typedef [ uuid(), version(1.0), helpstring("safe arrary") ] struct MG_ACQU_CHANNEL_T { BSTR strChannelName; //ecm channels class props }MG_ACQU_CHANNEL_T; [id(1), helpstring("get atl string"), helpcontext(1)] HRESULT GetATLString2([in] BSTR myString, [out,retval] BSTR* retVal); [id(2), helpstring("safearray"), helpcontext(2)] HRESULT GetSafeArrayOfParam([in] SAFEARRAY(MG_ACQU_CHANNEL_T) ChannelList, [out,retval] BSTR* retVal);};[ uuid(), version(1.0),]library testATLCOMSvrLib{ importlib("stdole2.tlb"); [ uuid() ] coclass ATLTest { [default] interface IATLTest; };};ATLTest.h// ATLTest.h : Declaration of the CATLTest#pragma once#include "resource.h" // main symbols#include "testATLCOMSvr_i.h"#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."#endifusing namespace ATL;// CATLTestclass ATL_NO_VTABLE CATLTest : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CATLTest, &CLSID_ATLTest>, public IATLTest{public: CATLTest() { }DECLARE_REGISTRY_RESOURCEID(IDR_ATLTEST)DECLARE_NOT_AGGREGATABLE(CATLTest)BEGIN_COM_MAP(CATLTest) COM_INTERFACE_ENTRY(IATLTest)END_COM_MAP() DECLARE_PROTECT_FINAL_CONSTRUCT() HRESULT FinalConstruct() { return S_OK; } void FinalRelease() { }public: STDMETHOD(GetATLString2)(BSTR myString, BSTR* retVal); STDMETHOD(GetSafeArrayOfParam)(SAFEARRAY * ChannelList, BSTR* retVal);};OBJECT_ENTRY_AUTO(__uuidof(ATLTest), CATLTest)ATLTest.cpp// ATLTest.cpp : Implementation of CATLTest#include "stdafx.h"#include "ATLTest.h"// CATLTestSTDMETHODIMP CATLTest::GetATLString2(BSTR myString, BSTR* retVal){ // TODO: Add your implementation code here return S_OK;}STDMETHODIMP CATLTest::GetSafeArrayOfParam(SAFEARRAY * ChannelList, BSTR* retVal){ // TODO: Add your implementation code here return S_OK;}C# - ATLClientArray arr = Array.CreateInstance(typeof(MG_ACQU_CHANNEL_T), 2); for (int i = 0; i < 2; i++) { MG_ACQU_CHANNEL_T channel = new MG_ACQU_CHANNEL_T(); channel.strChannelName = "welcome " + i; arr.SetValue(channel, i); } IATLTest atlTest = new ATLTest(); try { string str = "test string"; atlTest.GetATLString2(str); atlTest.GetSafeArrayOfParam(arr); } catch (ArgumentException ae) { }When I right click on function "GetSafeArrayOfParam " and go to function definition in c#. I see below function definition.using System;using System.Runtime.InteropServices;namespace testATLCOMSvrLib{ [TypeLibType(384)] [Guid()] [InterfaceType(1)] public interface IATLTest { string GetATLString2(string myString); string GetSafeArrayOfParam(Array ChannelList); //structure showed as array in C# }}
当我运行代码时,我得到一个异常:
When I run the code, I get an exception:
异常:
发现了System.ArgumentException
消息=参数不正确。 (HRESULT异常:0x80070057(E_INVALIDARG))
来源= mscorlib
StackTrace:
在System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToNative(IntPtr pMarshalState,Object& pManagedHome,IntPtr pNativeHome)
at testATLCOMSvrLib.IATLTest.GetSafeArrayOfParam(Array ChannelList)
在ATLClient的文件中,ATLClient的一个ATLClient.Form1.Form1_Load(Object sender,EventArgs e):第51行。
InnerException:
Exception:
System.ArgumentException was caught
Message=The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
Source=mscorlib
StackTrace:
at System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToNative(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome)
at testATLCOMSvrLib.IATLTest.GetSafeArrayOfParam(Array ChannelList)
at ATLClient.Form1.Form1_Load(Object sender, EventArgs e) in ATLClient\Form1.cs:line 51
InnerException:
提前致谢。
推荐答案
GSReddy ,
我正在尝试让一些高级工程师参与这个问题,这需要一些时间。非常感谢您的耐心等待。感谢您的理解。
I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated. Thanks for your understnading.
这篇关于在ATL / COM之间传递结构的SAFEARRAY - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!