I’ve created a COM object with Visual Studio that I want to use with Delphi.
[Guid("9D0CCD2D-75E9-46AC-BC40-C4D84669FC45")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyComClassDispatch
{
string PropertyString { get; set; }
string funcGetString();
void funcSetString(string aString);
}
[ClassInterface(ClassInterfaceType.None)]
public class MyComClassDispatch : IMyComClassDispatch
{
public string PropertyString { get; set; }
public string varString;
public string funcGetString()
{
return varString;
}
public void funcSetString(string aString)
{
varString = aString;
}
}
Then I’ve generated the tlb file, and finally a pascal file corresponding to my COM object with tlibimp.exe bundled with Delphi 5:
C:\>tlibimp.exe -P+ mylib.tlb
and here is the generated code:
// *********************************************************************//
// DispIntf: IMyComClassDispatch
// Flags: (4096) Dispatchable
// GUID: {9D0CCD2D-75E9-46AC-BC40-C4D84669FC45}
// *********************************************************************//
IMyComClassDispatch = dispinterface
['{9D0CCD2D-75E9-46AC-BC40-C4D84669FC45}']
property PropertyString: WideString readonly dispid 1610743808;
function funcGetString: WideString; dispid 1610743810;
procedure funcSetString(const aString: WideString); dispid 1610743811;
end;
The problem is that my property is marked as read only and I don’t really see why that is happening, because this tool can also generates C++ code and the property can be modified:
// *********************************************************************//
// Interface : IMyComClassDispatch
// Indicateurs : (4096) Dispatchable
// GUID : {9D0CCD2D-75E9-46AC-BC40-C4D84669FC45}
// *********************************************************************//
interface IMyComClassDispatch : public TDispWrapper<IDispatch>
{
BSTR __fastcall get_PropertyString()
{
_TDispID _dispid(/* PropertyString */ DISPID(DISPID_UNKNOWN/*[1610743808]*/));
TAutoArgs<0> _args;
OlePropertyGet(_dispid, _args);
return _args.GetRetVariant();
}
void __fastcall set_PropertyString(BSTR Param1/*[in]*/)
{
_TDispID _dispid(/* PropertyString */ DISPID(1610743808));
TAutoArgs<1> _args;
_args[1] = Param1 /*[VT_BSTR:0]*/;
OlePropertyPut(_dispid, _args);
}
BSTR __fastcall funcGetString()
{
_TDispID _dispid(/* funcGetString */ DISPID(DISPID_UNKNOWN/*[1610743810]*/));
TAutoArgs<0> _args;
OleFunction(_dispid, _args);
return _args.GetRetVariant();
}
void __fastcall funcSetString(BSTR aString/*[in]*/)
{
_TDispID _dispid(/* funcSetString */ DISPID(DISPID_UNKNOWN/*[1610743811]*/));
TAutoArgs<1> _args;
_args[1] = aString /*[VT_BSTR:0]*/;
OleProcedure(_dispid, _args);
}
__property BSTR PropertyString = {read = get_PropertyString, write = set_PropertyString};
};
Does this means that I should avoid properties with COM objects or is there any options that I should change ? Is this a problem with tlibimp or a limitation of the compiler that cannot generate instructions to write on COM objects properties ?
Any help would be appreciated on this!
This is known defect in Delphi 5 & 6 tlibimp.exe. You shall upgrade to Delphi 7 or newer version to avoid it.