Hi all,
we think we found a very fundamental issue with marshaling bool values from
..NET to COM. Our .NET server code looks like this (MC++, using VS.NET 2003
and .NET 1.1 SP1):
-----------------------------
namespace booltest
{
[Guid("466C7E09-E505-41ab-A331-39906FBBA20A"), ComVisible(true)]
public __gc __interface IBool {
bool Foo(void);
};
[ClassInterface(ClassInterfaceType::None), ComVisible(true)]
[Guid("B9E85D68-9284-4e62-A876-1B03EE8948B1")]
public __gc class Booltest : public IBool
{
public:
Booltest() {}
bool Foo(void) { return false; }
};
}
-----------------------------
So all we're trying to do is return a "bool" type to the caller. If we now
write a COM client which calls this code, we find that the automatically
generated marshaling code trashes the stack:
-----------------------------
#import "booltest.tlb"
int main(int argc, char * argv[])
{
::CoInitialize(0);
// Instantiate server code (COM wrapper on top of managed code)
Booltest::IBoolPtr pBool(__uuidof(Booltest::Booltest), 0, CLSCTX_ALL);
// call simple bool function; this trashes the stack
// (triggers alert when running in the debugger)
bool ret = pBool->Foo();
::CoUninitialize();
return 0;
}
-----------------------------
The generated ATL wrapper code assumes that the bool value returned from the
server is only one byte in size, but the CCW writes four bytes!
We are aware of the KB article 823072 which talks about incorrect bool
marshaling, but that problems occurs when calling unmanaged code from managed
code; in our case, we're marshaling in the other direction. Also, the KB
article only talks about cases where incorrect bool values are returned; in
our case, the stack is corrupted, and the code crashes.
Did anybody run into this before? If anybody's interested, I can provide a
trivial test project which shows the problem.
Thanks,
Claus