Hi!
I have IDL-file:
const byte DataStrLen = 255;
#pragma pack(1)
typedef struct
{
wchar_t fDataStr[DataStrLen];
} tSomeData;
#pragma pack()
[
object,
uuid(...),
pointer_default (unique),
local
]
interface ISomeData : IUnknown
{
tSomeData* getDataPtr();
};
I trying to reproduce it in managed c++:
const Byte DataStrLen = 255;
[ComVisible(true )]
[StructLayout(La youtKind::Seque ntial, Pack = 1, CharSet =
CharSet::Unicod e)]
public value struct tSomeData
{
[MarshalAs(Unman agedType::ByVal TStr, SizeConst = DataStrLen)]
String ^fDataStr;
};
[ComVisible(true )]
[Guid("...")]
[InterfaceType(C omInterfaceType ::InterfaceIsIU nknown)]
public interface class ISomeData
{
[PreserveSig]
tSomeData* getDataPtr();
};
[ComVisible(true )]
[Guid("...")]
[ClassInterface( ClassInterfaceT ype::None)]
public ref class SomeData : public ISomeData
{
public:
SomeData ()
{
m_tSomeData = gcnew tSomeData;
m_tSomeData->fDataStr= "ABC";
}
virtual tSomeData* getDataPtr()
{
pin_ptr pinPtr = &*m_tSomeDat a;
return pinPtr;
}
private:
tSomeData ^m_tSomeData;
};
After processing in tlbexp.exe I use tlb-file in test application:
#import "SomeData.t lb" no_namespace raw_interfaces_ only
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = ::CoInitializeE x(0, COINIT_MULTITHR EADED);
if (FAILED(hr))
return -1;
try
{
CComPtr<ISomeDa taspSomeData;
hr = ::CoCreateInsta nce(__uuidof(So meData), NULL,
CLSCTX_INPROC_S ERVER,
__uuidof(ISomeD ata),
(void**)&spSome Data);
tSomeData *pSomeData = spSomeData->getDataPtr() ; // !!!
MarshalDirectiv eException !!!
}
catch (...)
{
}
::CoUninitializ e();
return 0;
}
When I run test following error occurred while determining how to
marshal the parameters of member 'getDataPtr' of type
'SomeData.ISome Data':
"System.Runtime .InteropService s.MarshalDirect iveException: Cannot
marshal 'parameter #4294967295': Pointers cannot reference marshaled
structures. Use ByRef instead. This is most likely due to an
incompatible MarshalAs attribute on one of the parameters."
Where is error? What shall i do to implement such component?
I have IDL-file:
const byte DataStrLen = 255;
#pragma pack(1)
typedef struct
{
wchar_t fDataStr[DataStrLen];
} tSomeData;
#pragma pack()
[
object,
uuid(...),
pointer_default (unique),
local
]
interface ISomeData : IUnknown
{
tSomeData* getDataPtr();
};
I trying to reproduce it in managed c++:
const Byte DataStrLen = 255;
[ComVisible(true )]
[StructLayout(La youtKind::Seque ntial, Pack = 1, CharSet =
CharSet::Unicod e)]
public value struct tSomeData
{
[MarshalAs(Unman agedType::ByVal TStr, SizeConst = DataStrLen)]
String ^fDataStr;
};
[ComVisible(true )]
[Guid("...")]
[InterfaceType(C omInterfaceType ::InterfaceIsIU nknown)]
public interface class ISomeData
{
[PreserveSig]
tSomeData* getDataPtr();
};
[ComVisible(true )]
[Guid("...")]
[ClassInterface( ClassInterfaceT ype::None)]
public ref class SomeData : public ISomeData
{
public:
SomeData ()
{
m_tSomeData = gcnew tSomeData;
m_tSomeData->fDataStr= "ABC";
}
virtual tSomeData* getDataPtr()
{
pin_ptr pinPtr = &*m_tSomeDat a;
return pinPtr;
}
private:
tSomeData ^m_tSomeData;
};
After processing in tlbexp.exe I use tlb-file in test application:
#import "SomeData.t lb" no_namespace raw_interfaces_ only
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = ::CoInitializeE x(0, COINIT_MULTITHR EADED);
if (FAILED(hr))
return -1;
try
{
CComPtr<ISomeDa taspSomeData;
hr = ::CoCreateInsta nce(__uuidof(So meData), NULL,
CLSCTX_INPROC_S ERVER,
__uuidof(ISomeD ata),
(void**)&spSome Data);
tSomeData *pSomeData = spSomeData->getDataPtr() ; // !!!
MarshalDirectiv eException !!!
}
catch (...)
{
}
::CoUninitializ e();
return 0;
}
When I run test following error occurred while determining how to
marshal the parameters of member 'getDataPtr' of type
'SomeData.ISome Data':
"System.Runtime .InteropService s.MarshalDirect iveException: Cannot
marshal 'parameter #4294967295': Pointers cannot reference marshaled
structures. Use ByRef instead. This is most likely due to an
incompatible MarshalAs attribute on one of the parameters."
Where is error? What shall i do to implement such component?