Board index » delphi » .net COM problem, converting C# code

.net COM problem, converting C# code


2006-03-20 02:23:06 PM
delphi241
I have already posted this to the Com.Interop.DotNet group, but it's
very quiet in there and thought someone may be able to help me here....
Actually my problem is more to do with passing an object by reference.
Here is the line in C#...
wiaRoot = (ItemClass) wiaManager.Create( ref selectUsingUI );
The confusion started with getting mixed up with the method "Create" and
a Delphi constructor.
The line in context is:
WiaClass wiaManager = null;// WIA manager COM object
// WIA devices collection COM object
CollectionClass wiaDevs = null;
ItemClass wiaRoot = null;// WIA root device COM object
CollectionClass wiaPics = null;// WIA collection COM object
ItemClass wiaItem = null;// WIA image COM object
try {
wiaManager = new WiaClass();// create COM instance of WIA manager
// call Wia.Devices to get all devices
wiaDevs = wiaManager.Devices as CollectionClass;
if( (wiaDevs == null) || (wiaDevs.Count == 0) )
{
MessageBox.Show( this, "No WIA devices found!", "WIA",
MessageBoxButtons.OK, MessageBoxIcon.Stop );
return;
}
object selectUsingUI = System.Reflection.Missing.Value; // = Nothing
// let user select device
wiaRoot = (ItemClass) wiaManager.Create( ref selectUsingUI );
I was looking at InvokeMethod, but it is so complex and confusing...
Here is what I have so far...
wiaManager := WiaClass.Create;
wiaDevs := CollectionClass(wiaManager.Devices);
if ((wiaDevs = nil) or (wiaDevs.Count = 0)) then
begin
MessageBox.Show(self, 'No WIA devices found!', 'WIA',
MessageBoxButtons.OK, MessageBoxIcon.Hand);
Exit;
end
else
begin
{ Need to use InvokeMember to pass the selectUsingUI object by
reference. }
{ START Com call code code. }
{ Object to hold return object. }
selectUsingUI := Missing.Value;
ParamModifiers[0] := ParameterModifier.Create(1);
ParamModifiers[0][0] := true; // pass by reference.
{ Parameter Objects are expected in an array. }
Args[0] := selectUsingUI;
{ InvokeMember needs to be called on the type. }
aType := wiaManager.GetType();
wiaRoot := aType.InvokeMember('create', BindingFlags.InvokeMethod,
nil, wiaManager, Args, ParamModifiers, nil, nil)
as ItemClass;
But I get an "Invalid Number of Parameters" exception on the last line
at runtime. Help! Please???
Thanks,{*word*106}
 
 

Re:.net COM problem, converting C# code

Hi,
Quote
wiaRoot = (ItemClass) wiaManager.Create( ref selectUsingUI );
I think you need the keyword escape character, e.g.
wiaRoot := (ItemClass) wiaManager.&Create( selectUsingUI );
[I think the C# usage of "ref" in the invocation is optional
and it will be called by reference anyway].
--Craig
 

Re:.net COM problem, converting C# code

Craig Farrell wrote in news:441f358f$XXXX@XXXXX.COM:
Quote
Hi,

>wiaRoot = (ItemClass) wiaManager.Create( ref selectUsingUI );

I think you need the keyword escape character, e.g.

wiaRoot := (ItemClass) wiaManager.&Create( selectUsingUI );

[I think the C# usage of "ref" in the invocation is optional
and it will be called by reference anyway].

--Craig
Hi Craig,
I can not believe I screwed around with ParamModifiers and InvokeMethod, and
all I needed was that ampersand!!!! It worked beautifully! Thank you!
I also tried putting that form in a C# assembly and just create an instance
from my Delphi app. That worked too :-)
Thanks again,{*word*106}