Exposing enums, properties and variables

A

alexia.bee

Hi,

I created dll class library which I exposed methods on it.
I want to expose enums, properties and variables. The problem is that
I don't have an idea.

This is what I do:

namespace EIServices
{

[Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _Network
{
[DispId(1)]
EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);
}

public class Network
{
public enum EI_STAT_SERVICES
{
/* Infra */
ieFAIL = 0,
ieSUCCESS
}
public EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable)
{
}
}
}

What should I do if I want to expose the enum to the dll user?

Thanks for the help.
 
J

Jon Skeet [C# MVP]

What should I do if I want to expose the enum to the dll user?

That has already exposed the enum - but nested inside the Network
class. Any reason for not making it a top-level type instead of nesting
it?
 
A

alexia.bee

That has already exposed the enum - but nested inside the Network
class. Any reason for not making it a top-level type instead of nesting
it?

Jon hi and thanks for the reply.

How do I make it a top-level type which can be used in vbs also?
Is that the way to expose variables and properties to vbs also?
 
J

Jon Skeet [C# MVP]

How do I make it a top-level type which can be used in vbs also?

Just declare it outside the class.
Is that the way to expose variables and properties to vbs also?

No - variables and properties always need to be inside a type, whereas
an enum is a type itself.
 
A

alexia.bee

Just declare it outside the class.


No - variables and properties always need to be inside a type, whereas
an enum is a type itself.

I did as you suggested but when I try to get a vlaue suing VBScript I
get exception.

This is what I do in VBscript
set eiObj = createobject("EIServices.EI_STAT_SERVICES")' <---
exception here
msgbox eiObj.ieAdapterNotFound

This is what I do in C#
namespace EIServices
{
public enum EI_STAT_SERVICES
{
ieSUCCESS = 32,
ieAdapterNotFound
}
[Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _Network
{
[DispId(1)]
EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);

}
public class Network
{

}
}

When I create a C# app which uses the dll I get a string instead of
the decimal value of the enum.
what am I doing wrong?

thanks.
 
W

Willy Denoyette [MVP]

Just declare it outside the class.


No - variables and properties always need to be inside a type, whereas
an enum is a type itself.

I did as you suggested but when I try to get a vlaue suing VBScript I
get exception.

This is what I do in VBscript
set eiObj = createobject("EIServices.EI_STAT_SERVICES")' <---
exception here
msgbox eiObj.ieAdapterNotFound

This is what I do in C#
namespace EIServices
{
public enum EI_STAT_SERVICES
{
ieSUCCESS = 32,
ieAdapterNotFound
}
[Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _Network
{
[DispId(1)]
EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);

}
public class Network
{

}
}

When I create a C# app which uses the dll I get a string instead of
the decimal value of the enum.
what am I doing wrong?

thanks.



Hmmm...... Why are you trying to create an instance of an enum type????
The enum is the type returned by the SetDeviceState method.

Here is what you should do...

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> 32 Then
...

Willy.
 
A

alexia.bee

Just declare it outside the class.
No - variables and properties always need to be inside a type, whereas
an enum is a type itself.

I did as you suggested but when I try to get a vlaue suing VBScript I
get exception.

This is what I do in VBscript
set eiObj = createobject("EIServices.EI_STAT_SERVICES")' <---
exception here
msgbox eiObj.ieAdapterNotFound

This is what I do in C#
namespace EIServices
{
    public enum EI_STAT_SERVICES
    {
        ieSUCCESS = 32,
        ieAdapterNotFound
    }
    [Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface _Network
    {
        [DispId(1)]
        EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);

    }
    public class Network
    {

    }

}

When I create a C# app  which uses the dll I get a string instead of
the decimal value of the enum.
what am I doing wrong?

thanks.

Hmmm...... Why are you trying to create an instance of an enum type????
The enum is the type returned by the SetDeviceState method.

Here is what you should do...

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> 32 Then
    ...

Willy.

Hi Willy,

I don't want to write hard coded the val 32, I want to use it in the
following way:

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> stat.ieSUCCESS Then...
 
W

Willy Denoyette [MVP]

Just declare it outside the class.
No - variables and properties always need to be inside a type, whereas
an enum is a type itself.

I did as you suggested but when I try to get a vlaue suing VBScript I
get exception.

This is what I do in VBscript
set eiObj = createobject("EIServices.EI_STAT_SERVICES")' <---
exception here
msgbox eiObj.ieAdapterNotFound

This is what I do in C#
namespace EIServices
{
public enum EI_STAT_SERVICES
{
ieSUCCESS = 32,
ieAdapterNotFound
}
[Guid("673fcdbf-2bdb-4d05-9560-886bdc81294b")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _Network
{
[DispId(1)]
EI_STAT_SERVICES SetDeviceState(string AdapterID, bool
Enable);

}
public class Network
{

}

}

When I create a C# app which uses the dll I get a string instead of
the decimal value of the enum.
what am I doing wrong?

thanks.

Hmmm...... Why are you trying to create an instance of an enum type????
The enum is the type returned by the SetDeviceState method.

Here is what you should do...

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> 32 Then
...

Willy.

Hi Willy,

I don't want to write hard coded the val 32, I want to use it in the
following way:

Set eiObj = createobject("EIServices.Network")
stat = eiObj.SetDeviceState (...,...)
if stat <> stat.ieSUCCESS Then...



Well you will need to use Windows Scripting Host (WSH) for this (search MSDN
for details).
In you script file you need to add a "reference" tag to set a reference to
the typelib generated by regasm (or VS) for your component.
Also you need to watch when using enum names in script, use an _ instead of
a dot, for instance if you have an enum in C# that looks like Color.Green,
then you need to specify Color_Green in script.

Here is a simple sample to illustrate the usage, substitute your own
"typelib" uuid with the one in the sample. You can obtain the typelib guid
by looking into the registry or simply by running oleview on your typelib
(somefile.tlb).

<?XML version="1.0" ?>
<package>
<job>
' uuid of the typelib, dont use reference object = ....!
<reference guid="{98C2D27D-99F6-3DB3-9BB3-508D35BCA248}"/>
<script language="vbscript">
' create an instance of a C# class
set o = WScript.CreateObject("xxxx.yyyy")
' call a method that retruns an enum (Color) value
val = o.GetColor()
If val = Color_Green Then
WScript.Echo "Green"
End If
</script>
</job>
</package>

Hope this helps.

Willy.
 
Top