|
As Mike Dimmick said there are no 100% sure methods to verify a pointer. Nevertheless here are some basic techniques that could be used to increase the odds.
Some basic check that works on all pointers:
<br />
VERIFY(NULL != lpSomePointer)<br />
VERIFY(0 == (lpSomePointer & 3));<br />
At this point it *might* be a pointer because the number is an even number which is not NULL.
Microsoft supplies the functions IsBadCodePtr() and IsBadWritePtr() for checking the memory location. An example of using it on a function pointer might be:
if(FALSE == IsBadCodePtr((FARPROC)(LPVOID)pInstructions))<br />
{<br />
}
While using it on a pointer to a writable address might look like:
if(FALSE== IsBadWritePtr((LPVOID)pInstructions,1))<br />
{<br />
}
If you wanted to create your own verification to check if the pointer is pointing to a memory address which is executable... for a function pointer you might do something like:
MEMORY_BASIC_INFORMATION mbi;<br />
if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)pInstructions,&mbi,sizeof(MEMORY_BASIC_INFORMATION)))<br />
{<br />
if(mbi.Protect & PAGE_EXECUTE_READ && mbi.State & MEM_COMMIT && mbi.AllocationProtect & PAGE_EXECUTE_WRITECOPY && mbi.Type & MEM_IMAGE)<br />
{<br />
(*this.*MyMemberFunction)(someval);<br />
}<br />
}
If you wanted to write some extremely hacked-up NON-PORTABLE and compiler specific code to do some additional checking on a C++ member function you might look for some prologue asm instructions:
For example:
push esi
push edi
<br />
unsigned char ASM[] = "\x56\x57\x8B\x7C\x24\x0C";<br />
PDWORD pAddress = (PDWORD)&SomeFucntion;<br />
DWORD_PTR pInstructions = *pAddress;<br />
if(memcmp((LPVOID)pInstructions,ASM,sizeof(ASM))<br />
{<br />
}<br />
There is no guarantee that your compiler will produce a specific asm prologue... or the function may become _inline in which case this technique would fail.
I cant really think of anymore methods... and to be honest the ones listed above do not make any guarantees.
Best Wishes,
-Randor (David Delaune)
|
|
|
|
|
Hello Randor,
thanks for your info!
regards
termal!
|
|
|
|
|
Don't use IsBadXxxPtr() , they don't actually do what their names say. Check out Raymond Chen's and Larry Osterman's blogs as they have talked about why you should avoid them.
|
|
|
|
|
Don't use IsBadXxxPtr() , they don't actually do what their names say. Check out Raymond Chen's and Larry Osterman's blogs as they have talked about why you should avoid them.
|
|
|
|
|
The client connect to the sever but a exceptions ocurr in this funtcion.
CAsyncSocket::DoCallBack(WPARAM wParam, LPARAM lParam)
{
pSocket = CAsyncSocket::LookupHandle((SOCKET)wParam, FALSE);
if (pSocket == NULL)
{
// Must be in the middle of an Accept call
pSocket = CAsyncSocket::LookupHandle(INVALID_SOCKET, FALSE);
/***************************************************/
ASSERT(pSocket != NULL);// in this statement
//why the pSocket is NULL
/***************************************************************/
pSocket->m_hSocket = (SOCKET)wParam;
CAsyncSocket::DetachHandle(INVALID_SOCKET, FALSE);
CAsyncSocket::AttachHandle(pSocket->m_hSocket, pSocket, FALSE);
}
}
msn:xxblinux@hotmail.com
|
|
|
|
|
|
|
the diff b/w long pointer to string and pointer to string and where each one is used
|
|
|
|
|
what is your question exactly ?
anyway for what i understand, long pointer specifies that the pointer is as wide as a long
|
|
|
|
|
hari prasad sathpadi wrote: the diff b/w long pointer to string and pointer to string and where each one is used
There is no difference on 32 (or 64) bit Windows. It is a legacy from 16-bit days.
|
|
|
|
|
In modern Windows, none at all. LPSTR and PSTR are fully interchangeable.
On 16-bit Windows (and DOS), you had two sorts of pointer, near and far . A near pointer was 16 bits and could only reference data in the same segment as currently set, while a far pointer was 32 bits and could reference data in any segment. Data in a DLL would never be in your program's segment, so you'd need a far pointer to address it.
Segmented addressing was a really big pain and it was a huge relief to most programmers when 32-bit Windows moved to a flat 32-bit virtual address space.
A 'long' pointer in 16-bit Windows was a far pointer (32-bits, the same size as the long type). LPSTR was a typedef for char far* .
The old datatypes survive in 32-bit and now 64-bit Windows largely for source-level compatibility with 16-bit code. Consider it an odd quirk.
|
|
|
|
|
Hello,
I would like to compare two strings: i used:
if (s1==s2) {
I have no problem to compile, but even when s1=s2 it doesnt work...
|
|
|
|
|
|
ok. thx. but i have look about explanation about this command and it seems that it copies string, it doesn´t compare them.... Am i right? How am i suppose to use it?
|
|
|
|
|
if you want to compare them use strcmp or use a CString object to create your string(the "==" operator is overwritten by CString). Direct comparison doesn't works for arrays.
|
|
|
|
|
s1 is CString
then can use
CompareNoCase
Compare
|
|
|
|
|
in fact i think i did not express the problem correctly: i don´t want to egalise two string, but to compare two strings. ( sorry for my english)
|
|
|
|
|
Yes, if S1 and S2 are CString, operators (<, <=, >=, >, ==, and !=) works very well.
If they are char streams, use strcmp function. Operator overloading is not done for char streams.
|
|
|
|
|
in fact i think i did not express the problem correctly: i don´t want to egalise two string, but to compare two strings. ( sorry for my english)
|
|
|
|
|
in fact we understood rightly.
== operaotr compares not equalizes.
|
|
|
|
|
mandanani wrote: == operaotr compares not equalizes
then why does the code:
<br />
c1='aaa bbb ccc';<br />
f1='aaa bbb ccc'; <br />
cout<<c1<<endl;cout<<f1<<endl;<br />
if (c1 == f1) <br />
{a1=1;}<br />
cout<<a1;<br />
<br />
<br />
return on screen:
c
c
0
|
|
|
|
|
I think you have to understand how strings function. What are c1 and f1 ? char pointers, std::string or CString ? Without knowing that we can't really help you.
If these are standard char pointers, you need to use strcpy to be able to store one string in the array (but first, it must be allocated with enough size). Then, you can compare two strings using strcmp function.
Everything has already been told before.
So a code example:
char string1[255];<br />
char string2[255];<br />
<br />
strcpy(string1,"Test");<br />
strcpy(string2,"Test");<br />
if (strcmp(string1,string2) == 0)<br />
cout << "Strings are equal";<br />
else<br />
cout << "Strings are different";
Check the documentation of the functions on MSDN to be sure you understood correctly the concept.
|
|
|
|
|
but what does equalize mean for you ??
and in what does it differ from comparing 2 strings ?
also, don't hesitate to tell what kind of string you use (me know that you use std::string, but most people who replied here didn't know that, hence the CString references) to avoid bad answers
|
|
|
|
|
toxcct wrote: me know that you use std::string
How do you know that ??
|
|
|
|
|
Cedric Moonen wrote: How do you know that ??
I suspect he made an assumption based on the fact the code was using the '==' operator to attempt the comparison, but could obviously be wrong.
|
|
|
|