I am unable to translate the interface definition below from Delphi to C#:
IDCDSPFilterInterface = interface(IUnknown)
['{BD78EF46-1809-11D6-A458-EDAE78F1DF12}']
// removed functions thjat are already working
function get_FilterName(Index : integer; out Name : PChar): HRESULT; stdcall;
end;
I have tried with StringBuilder in the following way:
[ComVisible(true), ComImport, SuppressUnmanagedCodeSecurity,
Guid("BD78EF46-1809-11D6-A458-EDAE78F1DF12"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDCDSPFilterInterface : IBaseFilter
{
[PreserveSig]
int get_FilterName(int Index, [MarshalAs(UnmanagedType.LPStr)] StringBuilder Name);
}
I tried with LPStr, LPWStr, which both gives garbage characters in the string builder, and LPTStr which fails with an error message saying that this kind of marshalling combination is not allowed.
The definition of the method in Delhi is:
function TDCDSPFilter.get_FilterName(Index : integer; out Name : PChar): HRESULT; stdcall;
begin
{$IFDEF EXCEPT_DEBUG}try{$ENDIF}
FcsFilter.Lock;
try
{$IFDEF WITH_INTERNAL_DSP}
Result := S_FALSE;
if (Index < 0) or (Index > fFilters.Count -1) then Exit;
Name := PChar(fFilters.Name[Index]);
Result := S_OK;
{$ELSE}
Result := E_NOTIMPL;
{$ENDIF}
finally
FcsFilter.UnLock;
end;
{$IFDEF EXCEPT_DEBUG} except er('TDCDSPFilter.get_FilterName'); end; {$ENDIF}
end;
The fFilters.Name is declared as:
property Name[Index: integer]: String read GetName;
All my other interface methods work well with other basic types (in and ref) except this one with PChar output.
I get S_OK but the string in the StringBuilder is garbage...
I know the method is properly called because if I pass the wrong indexes I get S_FALSE (as the method body is defined to do).
Can anybody help to give the proper Marshalling for the Delphi out PChar?