[MicroStation v8i (SELECTSeries)] C# Addin - How to call mdlCell_Create through PInvoke

Hi,

I'm looking to call mdlCell_create from my C# addin, I've implimented the VBA decleration but stuck on how to call it. Any thoughts welcome. Alternativly can anyone give me some guidence on how to call this in a VBA environment?

[DllImport("stdmdlbltin.dll")]

public extern static void mdlCell_Create(int cellElm, int cellName, Point3d origin, int pointCell);

based on

Declare Function mdlCell_create Lib "stdmdlbltin.dll" (ByVal cellElm As Long, ByVal cellName As Long, ByRef origin As Point3d, ByVal pointCell As Long) As Long

Thanks,

Mike

Parents
  • I'm looking to call mdlCell_create from my C# addin

    You're using MicroStation V8i.  You can call the VBA methods through an InterOp quite easily.  Why do you feel the need to use MDL?

    Declare Function mdlCell_create Lib "stdmdlbltin.dll" (ByVal cellElm As Long, ByVal cellName As Long, ByRef origin As Point3d, ByVal pointCell As Long) As Long

    It's not easy to use MDL functions that require pointers from other languages that don't have pointer support.  In this case the first argument cellElm is a pointer-to-pointer.  How does C# deal with that?  What does that MDL function provide that VBA doesn't?

     
    Regards, Jon Summers
    LA Solutions

  • I'm trying to avoid the issue of com memory not releasing until the process is closed.

    I've considered a MDL addin but we have a large number of .NET libraries built up. I guess the solution would be to build a C++ wrapper to then call in C#. It's possible to use pointers in C# through use of the unsafe decleration, and similarly in VBA with VarPtr so thought it'd be possible to do a direct call to this function.

    As always, thanks for the fast response Jon.

  • HI Michael,

    I'm trying to avoid the issue of com memory not releasing until the process is closed.

    Do you have any reference about this issue? I have not too much experience with COM programming, but I have never heared about such condition when COM/Interop is used from NET.

    It's possible to use pointers in C# through use of the unsafe decleration

    Yes, it's possible, but it's treated as bad practice as far as I know. There are some consequences how code can be optimized and in my opinion it makes the code less maintenable.

    I guess the solution would be to build a C++ wrapper to then call in C#.

    If you do not want to use Interop API available in MicroStation addin or you need to use anything available in C/C++ API only, to use C++/CLI to create own wrappers is recommended. To use just a few calls to C (using e.g. P/Invoke) is fine, but when a number of such calls grows, C++/CLI is the way to go.

    Regards,

      Jan

  • I'm trying to avoid the issue of com memory not releasing until the process is closed
    • How do you know that's a problem? 
    • What analytical tools have you used to establish that COM memory is clogging up your computer? 
    • Why do you perceive COM memory deallocation to be a problem when you're happy to use non-deterministric garbage collection with C#?

    Use explicit delete in VBA:

    Set oSomething = Nothing

    Using MDL, you've introduced the problem of not releasing C-allocated memory (there's a hidden malloc() in mdlCell_Create().  You must explicitly delete that allocated memory at the appropriate time.

     
    Regards, Jon Summers
    LA Solutions

Reply
  • I'm trying to avoid the issue of com memory not releasing until the process is closed
    • How do you know that's a problem? 
    • What analytical tools have you used to establish that COM memory is clogging up your computer? 
    • Why do you perceive COM memory deallocation to be a problem when you're happy to use non-deterministric garbage collection with C#?

    Use explicit delete in VBA:

    Set oSomething = Nothing

    Using MDL, you've introduced the problem of not releasing C-allocated memory (there's a hidden malloc() in mdlCell_Create().  You must explicitly delete that allocated memory at the appropriate time.

     
    Regards, Jon Summers
    LA Solutions

Children
No Data