Click here to Skip to main content
15,891,657 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: "Access Violation Error" Using Custom COM Pin
ajitatif angajetor23-Mar-07 4:13
ajitatif angajetor23-Mar-07 4:13 
GeneralRe: "Access Violation Error" Using Custom COM Pin
Roger Stoltz23-Mar-07 4:57
Roger Stoltz23-Mar-07 4:57 
GeneralRe: "Access Violation Error" Using Custom COM Pin
ajitatif angajetor23-Mar-07 6:09
ajitatif angajetor23-Mar-07 6:09 
GeneralRe: "Access Violation Error" Using Custom COM Pin
led mike23-Mar-07 6:50
led mike23-Mar-07 6:50 
GeneralRe: "Access Violation Error" Using Custom COM Pin
ajitatif angajetor23-Mar-07 7:16
ajitatif angajetor23-Mar-07 7:16 
GeneralRe: "Access Violation Error" Using Custom COM Pin
led mike23-Mar-07 11:06
led mike23-Mar-07 11:06 
QuestionSuperclassing edit - base functionality? Pin
Christopher Duncan21-Mar-07 9:35
Christopher Duncan21-Mar-07 9:35 
QuestionHow to simplify accessing the objects' attributes inside of ATL DLL? Pin
xelontr20-Mar-07 4:44
xelontr20-Mar-07 4:44 
Hi,

I'm working on an ATL COM project. The most of the DLL's function is to retrieve and send objects with the software that uses it and do the required job(as in business layer of the 3-layer architecture in object oriented development).

I did have many obstacles while learning this strange library. But now all was gone. I'm good enough to develop what I thought at the beginning. But I have some questions about improving the quality of the written code. When accessing the DLL all code is simple enough to understand and develop. But when accessing an object within another object some strange stuff come in sight.

To explain it simply, I'm gonna give an example which I gave in my another question as well. Take the Course, Department and Student classes again.

Student have attributes as name, surname and these can be accessed with the following methods out of the DLL.

<br />
STDMETHODIMP CStudent::get_name(BSTR* pVal)<br />
{<br />
	CComBSTR bstStr(this->name);<br />
	*pVal=bstStr.Detach();<br />
	return S_OK;<br />
}<br />
<br />
STDMETHODIMP CStudent::put_name(BSTR newVal)<br />
{<br />
	_stprintf(this->name, _T("%s"), (LPCTSTR) newVal);<br />
	return S_OK;<br />
}<br />


There is nothing strange in this code when accessing Student class from outside of the DLL as below in C#

<br />
Student s = new Student();<br />
s.name = "MyName";<br />
String s = s.name;<br />


But what if I send this class' instance to one of the methods of the Department class as below.

<br />
STDMETHODIMP CDepartment::processSomething(IStudent* std)<br />
{<br />
	BSTR stdName;<br />
	std->get_name(&stdName);<br />
	...<br />
	...<br />
	return S_OK;<br />
}<br />


or when accessing an class' attribute which is another class.

<br />
	...<br />
	ICourse* course;<br />
	std->get_course(&course);<br />
	BSTR courseName;<br />
	course->get_name(&courseName);<br />
	...<br />


I mean defining variables and/or creating instances of the classes is taking long. I tried another way to make the code easier and shorter. First I define extra methods for each method to access the object's attributes simply as below.

<br />
STDMETHODIMP CStudent::get_name(BSTR* pVal)<br />
{<br />
	*pVal = this->getName();<br />
	return S_OK;<br />
}<br />
<br />
BSTR CPatient::getName()<br />
{<br />
	CComBSTR bstStr(this->name);<br />
	return bstStr.Detach();<br />
}<br />
<br />
STDMETHODIMP CStudent::put_name(BSTR newVal)<br />
{<br />
	this->putName(newVal);<br />
	return S_OK;<br />
}<br />
<br />
void CPatient::putName(BSTR newVal)<br />
{<br />
	_stprintf(this->name, _T("%s"), (LPCTSTR) newVal);<br />
	return bstStr.Detach();<br />
}<br />


Thus there is two way to access the required method. One for inside of the DLL and one for outside of the DLL. So that when accessing the member of this class within another class is so easy as below

<br />
STDMETHODIMP CDepartment::processSomething(IStudent* std)<br />
{<br />
	CStudent* cstd = static_cast<CStudent*>(std);<br />
	BSTR stdName = cstd->getName();;<br />
	...<br />
	BSTR courseName = cstd->getCourse()->getName(); //getCourse can return CCourse but get_course cannot. It must return ICourse interface.<br />
	...<br />
	return S_OK;<br />
}<br />


Of course one can access the attributes changing them as public instead of private. But we don't want it, do we? Smile | :)

With ICourse the attributes and methods of the class cannot be accessed directly. To access CCourse must be used.

My questions are

*Is casting the ICourse to the CCourse with the static_cast appropriate?
*I searched the web and looked the ATL DLL projects that I found. But I cannot find any example like this. Is the method of accessing the members and writing more methods for them appropriate also?
*Is there any other way to access the attributes of the class?(Without using DLL functions like get_name or get_course)
*Is there any other way to assign IStudent to CStudent to access the attributes of the Student class?

P.S. : I mean sensible not semantically right with appropriate

These post really long. I hope someone patient read it Smile | :)

Thanks to everybody who share ideas about this post.
QuestionOpening Internet Explorer in a new process Pin
rana7419-Mar-07 20:14
rana7419-Mar-07 20:14 
QuestionHow can i add simple object in ATL? Pin
$uresh $hanmugam19-Mar-07 2:37
$uresh $hanmugam19-Mar-07 2:37 
QuestionRe: How can i add simple object in ATL? Pin
prasad_som19-Mar-07 2:47
prasad_som19-Mar-07 2:47 
AnswerRe: How can i add simple object in ATL? Pin
$uresh $hanmugam19-Mar-07 3:24
$uresh $hanmugam19-Mar-07 3:24 
AnswerRe: How can i add simple object in ATL? Pin
prasad_som19-Mar-07 3:32
prasad_som19-Mar-07 3:32 
QuestionError when accessing an object in an object Pin
xelontr14-Mar-07 2:53
xelontr14-Mar-07 2:53 
QuestionWeb services and win 32 dll. Pin
LCI13-Mar-07 5:27
LCI13-Mar-07 5:27 
AnswerRe: Web services and win 32 dll. Pin
led mike13-Mar-07 5:42
led mike13-Mar-07 5:42 
Questionqueries regarding working of an application in win2000 but wrong calculation in winxp Pin
Member 385320113-Mar-07 0:34
Member 385320113-Mar-07 0:34 
QuestionPrinting a form larger than the screen [modified] Pin
Enriad12-Mar-07 19:12
Enriad12-Mar-07 19:12 
QuestionLanguage Problem Pin
xelontr11-Mar-07 23:11
xelontr11-Mar-07 23:11 
AnswerRe: Language Problem Pin
prasad_som11-Mar-07 22:15
prasad_som11-Mar-07 22:15 
GeneralRe: Language Problem Pin
xelontr11-Mar-07 23:32
xelontr11-Mar-07 23:32 
AnswerRe: Language Problem Pin
prasad_som11-Mar-07 23:39
prasad_som11-Mar-07 23:39 
GeneralRe: Language Problem Pin
xelontr12-Mar-07 0:06
xelontr12-Mar-07 0:06 
AnswerRe: Language Problem Pin
prasad_som12-Mar-07 0:09
prasad_som12-Mar-07 0:09 
QuestionATL ActiveX destructor not called Pin
TClarke8-Mar-07 4:53
TClarke8-Mar-07 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.