Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COM Interop in C#

Status
Not open for further replies.

lindholm81

Programmer
Apr 4, 2008
1
Hi!

I need your help guys. I wrote .NET application that uses COM-objects. I have imported tlb file of a library with such COM-objects into Visual Studio C#. Then Visual Stidio gave me classes of that COM-objects. There is my issue:
The one object has function EnableRules(string templName, ref object rules, bool enable).
In a COM-objects' API help there is a description of this function in VB code:
Method EnableRules (

templateName As String,

ruleIDs As Variant,

enable1 As Boolean

)
and an example

Dim rules() As Long

ReDim rules (1 To 2)

rules(1) = 18

rules(2) = 30

fr.BatchDocument.EnableRules "Test", rules, True

In my code I try such thing:

object rules = new long[2]{0,1};
fr.BatchDocument().EnableRules("Batch4", ref rules, true);

But it throws COMException like: impossible parameter value
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at FineForm.IFineFormBatchDoc.EnableRules(String templateName, Object& ruleIDs, Boolean enable1)
...

What is wrong in my code? Can you give me any advise?

Thanks
 
The problem lies with the variant data type. The simple way to deal with this is either change the source of the VB app or write an ActiveX wrapper for the VB app that translates the Variant data type to another type that .Net can understand. You would then use the COM Interop to access the wrapper instead of the ActiveX Control directly. There are other ways of dealing with this, but I have found these methods tend to be the most straightforward.

 
Another option is to use the MarshalAs attribute when declaring the method through DllImport.

im in ur stakz, overflowin ur heapz!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top