VARIANT type problem

Z

ZhangZQ

If I have a method in an OCX, the definition is

[id(5), helpstring("Method Test1")] void Test1(VARIANT* a);

After I add reference to this OCX in Visual studio .net, it generates 2
dlls, and I look at the object browser, the definition of the above method
in the wrapper class is

Test1(ref object);

Now I use the C# to call this method,

object myObj = new object();
MyOCX.Test1(ref myObj); // Then it will throw an exception with error
message "type mismatch"

and no matter which value I set to myObj it always throws the exception with
the same error message.


I want to know how to call that
[id(5), helpstring("Method Test1")] void Test1(VARIANT* a); method?


Thank you very much!
 
Z

ZhangZQ

This is the IL of that RCW

.method public hidebysig newslot virtual
instance void Test1(object& a) cil managed
{
.maxstack 3
IL_0000: ldarg.0
IL_0001: ldfld class
[Interop.ocx030820Lib]ocx030820Lib._Docx030820
Axocx030820Lib.Axocx030820::blush:cx
IL_0006: brtrue.s IL_0014

IL_0008: ldstr "Test1"
IL_000d: ldc.i4.0
IL_000e: newobj instance void
[System.Windows.Forms]System.Windows.Forms.AxHost/InvalidActiveXStateExcepti
on::.ctor(string,

valuetype
[System.Windows.Forms]System.Windows.Forms.AxHost/ActiveXInvokeKind)
IL_0013: throw

IL_0014: ldarg.0
IL_0015: ldfld class
[Interop.ocx030820Lib]ocx030820Lib._Docx030820
Axocx030820Lib.Axocx030820::blush:cx
IL_001a: ldarg.1
IL_001b: callvirt instance void
[Interop.ocx030820Lib]ocx030820Lib._Docx030820::Test1(object&)
IL_0020: ret
} // end of method Axocx030820::Test1

The exception will be thrown at IL_011b.
 
P

Peter Koen

If I have a method in an OCX, the definition is

[id(5), helpstring("Method Test1")] void Test1(VARIANT* a);

After I add reference to this OCX in Visual studio .net, it generates
2 dlls, and I look at the object browser, the definition of the above
method in the wrapper class is

Test1(ref object);

Now I use the C# to call this method,

object myObj = new object();
MyOCX.Test1(ref myObj); // Then it will throw an exception with
error message "type mismatch"

and no matter which value I set to myObj it always throws the
exception with the same error message.


I want to know how to call that
[id(5), helpstring("Method Test1")] void Test1(VARIANT* a); method?


Thank you very much!

Hi,

Either you use Convert.TOxxxx to get the value you want from that object
or you make your own wrapper. For that you will have to use the
MarshalAsAttribute to tell the Runtime what type you really want to pass.

Details about the mapping of the different datatypes can be found here:

http://msdn.microsoft.com/library/en-
us/cpguide/html/cpconcomdatatypes.asp

Further Information about Marshaling can also be found here:
http://msdn.microsoft.com/library/en-
us/cpguide/html/cpconmarshalingdatawithcominterop.asp

--
------ooo---OOO---ooo------

Peter Koen - www.kema.at
MCAD CAI/RS CASE/RS IAT

------ooo---OOO---ooo------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top