Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Contents of a Pointer

Status
Not open for further replies.

raghu75

Programmer
Nov 21, 2000
66
IN
I have this piece of code below and the problem is of checking whether the pointer contents are valid or not....

Iunknown * pUnk = (Iunknown *) GetWindowLongPtr(hWnd,0);
pUnk->GetView();
my code is crashing at line #2 ie.punk->GetView();
This is because its contents are not valid....
how to check for the validity of the contents of the pUnk pointer...
 
is your IUnknown a COM interface or a simple class what you defined in your way? As I know, the interface IUnknown has only three methods. They are QueryInterface,AddRef,Release. John Fill
 

Wont this one work ? It is simple...

If (pUnk !=NULL)
{
pUnk->GetView();
}
else
{
// error handling code here

...
}


abp
 
Actually i gave Iunknown as an example.my actual code is as below.
IMSHELL * pShell = (IMSHELL *) GetWindowLongPtr(hWnd,0);
pShell->ProcessWnd(hWnd,lparam,wparam);

it is crashing at line #2 since the pShell contains some garbage..how to check for the validity of pShell.
Also IMSHELL is an interface derived from IOleWindow.
 
abp,
This one will work only if pUnk is not a COM interface. Do you know what is it a COM interface? John Fill
 
IMSHELL is derived from IoleWindow ..it contains some garbage and hence != NULL fails and it still crashes at line #2
 
JohnFill,
how can it crash in compile time..
it will crash only while executing..

raghu,
you should have function in that class which
tells you abt the type of Class, with CLASSID.
you compare the class id before calling the functions.

lak
 
Try following. Maybe it'll work
IUnknown * pUnk = NULL;//to initialize at the first.
pUnk = (IUnknown *) GetWindowLongPtr(hWnd,0);
pUnk->GetView();
if(pUnk)
{
...
}else
{
...
} John Fill
 
I am sorry to state that none of the answers are holding.
I will explain the problem again in order clear the situation,
Firstly i create a window and Using SetWindowLongPtr() API of WINDOWS i set it.Then i call GetWindowLongPtr() API .I assign this to my pointer.But this pointer always contains the IoleWIndow Interface and some garbage value.This is making the call to the function to crash,because it is neither null nor valid.IT ALWAYS CONTAINS SOME GARBAGE VLAUE.HOW TO CHECK THE VALIDITY OF MY POINTER WHETHER IT CONTAINS VALID VALUE OR NOT.
 
You can use SEH frame to analyze. If function crashes then the code is invalid.
The syntax is
__try
{
pUnk->GetView();
}__except(EXCEPTION_EXECUTE_HANDLER)
{
....
}
John Fill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top