Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2004

Tip: Looking for answers? Try searching our database.

Exposing .Net struct in COM

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mads - 06 Oct 2004 09:53 GMT
I'm trying to make my own ValueType in C#, i need to call this from COM so i
need to expose it.

When i'm trying to assign it from COM it says "Incompatible types: FinVal
and Integer"

public struct FinVal
{
    private bool _assigned;
    private bool Assigned
    {
        get
        {
            return _assigned;
        }
        set
        {
            _assigned = value;
        }
    }
    private double _vaerdi;
    public double Vaerdi
    {
        get
        {
            if(Assigned)
                return _vaerdi;
            else
                throw new Exception("Vaerdien er ikke sat!: " +
this.GetType().TypeInitializer.Name);
        }
        set
        {
            Assigned = true;
            _vaerdi = value;
        }
    }
    public static FinVal operator + (FinVal val1, FinVal val2)
    {
        return new FinVal( val1.Get(val1.ValutaKode) + val2.Get(val1.ValutaKode),
val1.ValutaKode);
    }
    .....
    public static explicit operator int(FinVal val)
    {
        return (int)val.Vaerdi;
    }
    public static implicit operator FinVal(int val)
    {
        return new FinVal(val, ValutaHandler.eValutaKode.DKK);
    }
    .....
}

The result of exposing is 2 variables _assigned and _vaerdi.
Luis Abreu - 06 Oct 2004 10:12 GMT
If I'm not mistake, structs are exposed to COM as simple idl structs, which
means that none of the methods are exported. Assuming this is true, none of
your methods (and i think this also means properties) are exported. by the
way, you should use the StructLayoutAttribute with the value
LayoutKind.Sequential to fix the members of the struct.

regarding the error you're speaking of, without COM code there's nothing
more I can do about it.

Signature

--
Regards,
Luis Abreu
http://weblogs.pontonetpt.com/luisabreu
http://www.pontonetpt.com

> I'm trying to make my own ValueType in C#, i need to call this from COM so i
> need to expose it.
[quoted text clipped - 51 lines]
>
> The result of exposing is 2 variables _assigned and _vaerdi.
Mads - 06 Oct 2004 11:47 GMT
Thank you for helping :o).

Here is a complete example of the code:

I try to ask another way: Is there any way to expose the type FinVal so it
is posible to use like i do in C#.

Ex i need to call it from Delphi:

var
 test1 : Test;
begin
 test1 := CoTest.Create;
 test1.Val := 100;                   // this makes error "Incompatible types"
 test1.Val._vaerdi := 100;       // this makes errer "Left side cannot be
assigned to"
end;

using System;
using System.Runtime.InteropServices;

namespace ClassLibrary1
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Test
    {
        public Test()
        {
        }

        private FinVal _val;
        public FinVal Val
        {
            get
            {
                return _val;
            }
            set
            {
                _val = value;
            }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct FinVal
    {
        private bool _assigned;
        private bool Assigned
        {
            get
            {
                return _assigned;
            }
            set
            {
                _assigned = value;
            }
        }
        private double _vaerdi;
        public double Vaerdi
        {
            get
            {
                if(Assigned)
                    return _vaerdi;
                else
                    throw new Exception("Vaerdien er ikke sat!: " +
this.GetType().TypeInitializer.Name);
            }
            set
            {
                Assigned = true;
                _vaerdi = value;
            }
        }

        public FinVal(double vaerdi)
        {
            _assigned = true;
            _vaerdi = vaerdi;
        }

        public override string ToString()
        {
            return Convert.ToString(Vaerdi);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode ();
        }                              
       

        public static explicit operator int(FinVal val)
        {
            return (int)val.Vaerdi;
        }

        public static implicit operator FinVal(int val)
        {
            return new FinVal(val);
        }

    }
}

> If I'm not mistake, structs are exposed to COM as simple idl structs, which
> means that none of the methods are exported. Assuming this is true, none of
[quoted text clipped - 61 lines]
> >
> > The result of exposing is 2 variables _assigned and _vaerdi.
Luis Abreu - 06 Oct 2004 14:54 GMT
Hello again.

> I try to ask another way: Is there any way to expose the type FinVal so it
> is posible to use like i do in C#.
short answer: I think not...what you must understand is that COM is not
about OO programming...it's about contracts made at binary level (at least
that's my opinion).

Another thing that you must understand is that when your struct is exported
to COM it looses all its members. So, if you use oleview to see what you've
got, you'll notice that there's only 2 members _assigned and _vaerdi. btw,
if you really want to use the struct, then maybe it's better to expose them
without the underscore ( _ ) (because VB doesn't show them due to the fact
that names started with underscore should be private).

Signature

--
Regards,
Luis Abreu
http://weblogs.pontonetpt.com/luisabreu
http://www.pontonetpt.com


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.