COM Interop problem

When I try to call any method that returns a point3d, for example

load_assembly 'bentley.interop.microstationdgn'
include Bentley::Interop::MicroStationDGN
u = ApplicationClass.new
pnt = u.point3dfromxyz(100.0, 200.0, 300.0)

I get this error:

ArgumentError: The specified record cannot be mapped to a managed value class.
        from mscorlib:0:in `GetObjectForNativeVariant'

I am trying this from IRB in ironruby but I have the same problem in other languages accesing via COM

However this VB works fine even when run from Excel.

Sub Macro1()
    Dim oMDL As ModelReference
    Set oMDL = ActiveModelReference
    Debug.Print (oMDL.GlobalOrigin.X)
End Sub

What am I doing wrong? (I am a bit of a newb with microsoft technologies, hence the tinkering via ironruby :)

Also I don't understand why all the VBA examples for accesing properties do all that stuff with CreatePropertyHandler is that something to do with it? I have tried this technique via the COM interface and it doesn't seem to make any difference, however it doesn't seem to be neccessary at all in VBA?

Thanks

Will

  • I am trying this from IRB in ironruby but I have the same problem in other languages accessing via COM. However this VBA works fine even when run from Excel.

    Let's be clear: when you write VBA code in Excel and call methods in the MicroStationDGN.Application, you are using COM. Therefore your statement "I have the same problem in other languages accessing via COM" may be true, but it's not a COM issue — rather, it's a language issue.

    What you have posted does not provide a complete context: What, for example, is the declaration of pnt? The error you quote looks like a .NET issue: it can't figure out what a Point3d is, and can't assign it because it isn't a managed class.

    A Point3d is a plain old data (POD) type, not a managed class. So the message seems consistent.

    However, you have chosen to develop code using IronRuby. As yours is the first & only post that mentions that language, I regret that you are in a minority. I suggest that you use a Microsoft supported language, such as C#. When your C# code is working, you can port it to IronRuby.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • IronRuby is a microsoft supported managed language. It's a variant of Ruby which is developed by microsoft to run on the CLR and can be compiled to MSIL just like C#. It's very convienient because it has an interactive prompt which allows you to introspect classes on the fly. I've never used C# before but I've read quite a lot recently trying to figure out what's going on. I will try and recreate the example in C# and see if I get the same problem. But, before I do this, and this is probably a stupid question, is it possible to access the XYZ values of a Point3d from C#?

  • Submit questions in a language that we can understand

    IronRuby is a Microsoft supported managed language. It's a variant of Ruby which is developed by Microsoft.

    Those comments are moot …

    Furthermore, just because a language is supported by Microsoft you cannot draw the conclusion that it is therefore a good language to use to write a MicroStation application.

    If you want to use IronRuby because you like the language, that's fine. However, it's not reasonable to expect people on this Forum to be able to answer questions phrased in IronRuby code.

    The native languages of MicroStation are VBA, MDL, and the C++ MicroStationAPI. C# is close enough and can use either MDL as managed code or VBA via a COM InterOp. If you phrase your question in VBA or MDL, then the maximum number of people can understand it and respond. If you phrase your question in C# (unquestionably a Microsoft-supported language) then a smaller number of people will be able to understand your question and provide a response. If you phrase your question in some other language (Perl, Cobol, Python, F#, IronRuby), even if that language uses the .NET CLR, then few people will understand your question and fewer will respond to it.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • Will,

    SharpDevelop has a converter for Ruby if that helps. I haven't tried it though.

    Also, as someone who has been picking up C# in MS over the last couple of months, my suggestion is to start with the TextMod example (in the SDK) as a template and modify it. While you are bound to get errors, you could use SharpDevelop to convert the example to IronRuby to help work out what's going on.

    Are all the .NET languages that hard to cipher once you know one?

    steve

  • Are all the .NET languages that hard to cipher once you know one?

    The fact that different languages share the common language runtime (CLR) is not the issue. All compiled languages, for example, end up as Intel-compatible binary code, but I doubt that you would argue that "Since all compiled languages run on an Intel processor, they can't be hard to understand".

    If all .NET languages were so similar, there wouldn't be a lot of point in people developing different languages. And, they're not similar: compare Perl's gloriously untyped excellence at string handling with F#'s demonstration of Microsoft's commitment to functional programming.

    The point at issue is clarity and ease of diagnosis. Who knows what language-dependent problems may occur because of different usage of the CLR? Look at the endless questions on these Forums about .NET InterOp usage, and use of managed and unmanaged (Microsoft terminology) code.

    To put it another way, why should anyone who spends time & effort — for no material reward — have to spend unnecessary time interpreting a question about WierdReciprocatingPi.NET language? If the questioner expects a prompt answer, it's best to ask the question in a language that the listener understands and has the least likelihood of misinterpretation.

    Regards, Jon Summers
    LA Solutions

     
    Regards, Jon Summers
    LA Solutions

  • Object from native variant...

    This seems to be an initializing problem. In MS supported languages it's better (when using COM) if you declare your type before initializing it. I'm not Ruby programmer, but if there is any way to define variable's type before initializing it from COM Interop, why not to do it? New C# also supports var type, but it is on compiler then to recognize what type it should be. If you declare it directly, you save compiler from determining your means...

    What you can try next is to use casting using CType or DirectCast methods...

     

  • DanPaul,
    I think you are right; this C# code works fine

    using System;
    using BCOM = Bentley.Interop.MicroStationDGN;

    public class Hello
    {
    public static void Main()
    {
     Console.WriteLine("Hello C# World");
     BCOM.Application app = new BCOM.ApplicationClass();
     app.OpenDesignFile(@"H:\_w110 RSHP general\test\data\Test_Global_Origin.dgn", false, 0);
     BCOM.ModelReference model = app.ActiveModelReference;
     BCOM.Point3d pnt = model.GlobalOrigin;

     Console.WriteLine(pnt.X);
     Console.WriteLine(pnt.Y);
     Console.WriteLine(pnt.Z);
    }
    }

    Compiled like this
    csc /nologo /lib:"C:\Program Files (x86)\Bentley\MicroStation V8i (SELECTseries 1)\MicroStation\assemblies" /r:"bentley.interop.microstationdgn.dll" /out:hello.exe hello.cs

    (In case anyone's interested, if you don't have gacutil.exe then you need to copy the .dlls into the same directory as the exe.)

    It's not possible to declare types in Ruby. The rubyists say it's 'Duck Typed'; if it walks like a duck and talks like a duck, it's a duck. Having said this, the IronRuby folks have probably solved this problem somehow. So the solution to the problem is in another part of the internet. I know it's a silly way to learn to program microstation COM but I like being able to explore the Microstation objects through a REPL and anyway I am curious now and have to get to the bottom of it...  :-)