EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of COM/DCOM Questions #Next question

ENUM type in IDL

Enumeration type is very common in all programming languages. C,C++, Java, VB, and all scripting languages support Enumeration type. IDL language also supports ENUM type. The keyword enum is used to denote a Enumeration type.

ENUM type in DCOM server

We have here an ENUM demo COM server source code. We have defined an ENUM type named as weekday. This has seven values and those are named according to the name of days in a week. We have one function IDayNames::GetDayByValue which takes the enum type weekday as value and returns the corresponding day name as string. This function can be used to convert a day value to string value as needed in GUI.

//======================
//  IDL definition 
//======================
import "oaidl.idl";
import "ocidl.idl";
    
typedef enum weekday {
  Sunday,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
} weekday;

[
  object,
  uuid(176CE97A-7383-4984-BBE1-252807BBC681),
  dual,
  helpstring("IDayNames Interface"),
  pointer_default(unique)
]
interface IDayNames : IDispatch
{
  [id(1), helpstring("GetDayByValue returns day as string")]
  HRESULT GetDayByValue([in] enum weekday dayVal, [out,retval] BSTR *strDay);
};

[
  uuid(4EE5FEA2-DADE-4AD0-8AF2-50111B6B10BA),
  version(1.0),
  helpstring("EnumDemo 1.0 Type Library")
]
library ENUMDEMOLib
{
  importlib("stdole32.tlb");
  importlib("stdole2.tlb");

  [
    uuid(3481762C-74E1-465D-9DA8-818122BB9974),
    helpstring("DayNames Class")
  ]
  coclass DayNames
  {
    [default] interface IDayNames;
  };
};

//======================
//  C++ implementation 
//======================
STDMETHODIMP CDayNames::GetDayByValue(enum weekday dayVal, BSTR *strDay)
{
  WCHAR* Days[] = {
    L"Sunday",
    L"Monday",
    L"Tuesday",
    L"Wednesday",
    L"Thursday",
    L"Friday",
    L"Saturday"
  };

  if ((strDay == NULL) || (dayVal > Saturday) || (dayVal < Sunday))
  {
    return E_INVALIDARG;
  }
  *strDay = SysAllocString(Days[dayVal]);
  if (*strDay == NULL)
  {
    return E_OUTOFMEMORY;
  }
  return S_OK;
}

ENUM type VB client

This is a very simaple VB client. We are creating an object of ENUMDEMOLib.DayNames and calling GetDayByValue function with the enum value as Sunday. This function returns the day as string and we display this in messagebox.

Dim dayobj As ENUMDEMOLib.DayNames
Dim strDay As String

Private Sub Form_Load()

Set dayobj = New ENUMDEMOLib.DayNames
strDay = dayobj.GetDayByValue(Sunday)
MsgBox strDay
Set dayobj = Nothing

End Sub

You have viewed 1 page out of 66. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

# COM DCOM Interview Questions and Answers :: ALL Interview .com
# COM DCOM - COM DCOM Interview Questions
# COM interview questions | TechInterviews
# COM Interview Questions - CodeProject
# Com Dcom Interview Questions - Com and Dcom in Asp Net
# ATL Interview Questions and Answers :: ALL Interview .com
# ActiveX Interview Questions and Answers :: ALL Interview .com

* #1 webmaster Wed 30 Apr/2014 17:27:33(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.