Hi there. I have a COM DLL that I have converted into an interop assembly
via the tlbimp.exe tool. One function that I am trying to use now in C# has
a default parameter defined in my IDL file:
HRESULT Convert( [in, defaultvalue( "" )] BSTR file, [out, retval] long
*count );
When I try to make use of the function in C# I get a compiler error saying
that this function does not take 0 parameters.
using the ildasm.exe to take a look at the assembly code I get:
.method public hidebysig newslot virtual
instance int32 Convert([in][opt] string marshal( bstr) file)
runtime managed internalcall
{
.custom instance void
[mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = (
01 00 09 00 00 00 00 00 )
.param [1] = ""
.override MYINTEROP:Convert
}
It looks to me like the parameter is optional and that it is indeed setting
a default value of "". Do I have to do something else for C# to recognize
this? It is not that big of a deal to pass a value to the function( I would
prefer not to ), but it seems like there should be all that is needed for C#
to fill in the value if it isn't there. Any help is appreciated. Thank you
for your time.
-Mac
Mattias Sjögren - 20 Sep 2005 06:41 GMT
>It looks to me like the parameter is optional and that it is indeed setting
>a default value of "". Do I have to do something else for C# to recognize
>this?
There's no way, C# simply doesn't support optional parameters.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Rag - 30 Sep 2005 09:26 GMT
try using System.Reflection.Missing.Value;
Mattias Sjögren - 07 Oct 2005 23:12 GMT
>try using System.Reflection.Missing.Value;
That works for object/VARIANT, not for string/BSTR.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.