🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by Mike Collins Mon, 23 Jul 2007 23:08
PHANDLE, HANDLE comparisions and vectors
#1512
Author: Mike Collins
Date: Mon, 23 Jul 2007 23:08
77 lines
3527 bytes
Hi all got a bit of a vague question regarding PHANDLE's and HANDLES.  I'm 
doing some work with dll injection and api hooking on a native level.

I'm hooking one function, NtCreateSection and i need to store one of the 
values returned from this function, in a vector, for future checking.  The 
value is defined / returned as a PHANDLE.

I've declared a vectore of HANDLE's and assign the PHANDLE thus:

std::vector<HANDLE>vecFileWatch;

NTSTATUS NTAPI NtCreateSectionCallback(PHANDLE SectionHandle, ACCESS_MASK 
DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes,
                                                                         PLARGE_INTEGER 
SectionSize, ULONG Protect, ULONG Attributes, HANDLE FileHandle)
{
   NTSTATUS iStatus;
   iStatus = NtCreateSectionNxt(SectionHandle, DesiredAccess, 
ObjectAttributes, SectionSize, Protect, Attributes, FileHandle);
   if (NT_SUCCESS(iStatus))
     vecFileWatch.push_back(SectionHandle);  //Always seems to assign 1 into 
the vector...

   return iStatus;
}

- i'm not sure how to assign or cast this though because i always end up 
with the value 1 in the vector.

Later on, i need to scan this vector and compare the stored value against an 
actual HANDLE, which is performed from another native api hook, 
NtCreateProcessEx:

NTSTATUS NTAPI NtCreateProcessExCallback(PHANDLE ProcessHandle, ACCESS_MASK 
DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, HANDLE ParentProcess, 
BOOLEAN InheritObjectTable, HANDLE SectionHandle, HANDLE DebugPort, HANDLE 
ExceptionPort, DWORD dwSaferFlags)
{
   NTSTATUS iStatus;
   iStatus = NtCreateProcessExNxt(ProcessHandle, DesiredAccess, 
ObjectAttributes, ParentProcess, InheritObjectTable, SectionHandle, 
DebugPort,
                                                      ExceptionPort, 
dwSaferFlags);
   if (NT_SUCCESS(iStatus))
   {
      if (!vecFileWatch.empty())
      {
         // Vector index position...
         int iRtn = -1;
         unsigned int i = 0;

         // while loop used to ensure that we can exit as soon as a value is 
found.
         while ((i < vecFileWatch.size())&&(iRtn < 0))
         {
            // Comparison of HANDLES always fails even though they appear to 
be 1!
            if (vecFileWatch[i] == SectionHandle)
               iRtn = i;
            else
               ++i;
         }

         if (iRtn > -1)
            TFileMonitor *FileMon = new TFileMonitor(false, ProcessHandle);
      }
   }
   return iStatus;
}

can anyone give me some pointers on how to assign / cast between the PHANDLE 
and HANDLE and how to perform the necessary comparison?

Many thanks in advance,

Mike C
Re: PHANDLE, HANDLE comparisions and vectors
#1513
Author: Remy Lebeau (TeamB)
Date: Mon, 23 Jul 2007 22:55
32 lines
1848 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message
news:46a52487$1@newsgroups.borland.com...

>      vecFileWatch.push_back(SectionHandle);  //Always seems to assign 1
into
> the vector...

I'm surprised that compiles at all, considering that push_back() would be
expecting a HANDLE instead of a PHANDLE.  You probably do not have STRICT
defined.

PHANDLE is a pointer to a HANDLE, so to push it into your vector properly,
you need to deference the pointer to get the actual HANDLE value:

    vecFileWatch.push_back(*SectionHandle);

> while loop used to ensure that we can exit as
> soon as a value is found.

You should use the STL's find() function instead of looping manually:

    #include <algorithm>

    if( std::find(vecFileWatch.begin(), vecFileWatch.end(), SectionHandle)
!= vecFileWatch.end() )
        // SectionHandle was found ...
    else
        // SectionHandle not found ...


Gambit
Re: PHANDLE, HANDLE comparisions and vectors
#1514
Author: Mike Collins
Date: Tue, 24 Jul 2007 01:23
28 lines
1878 bytes
Hi Remy, thanks for the reply.  Feel kind off embarrassed that i asked such 
a basic question based on the fact that i'm messing around with what is 
seemingly quite an advanced project.  I would swear blind that i did that 
originally but i must have just got mixed up.  I thought that there mite be 
some funny casting required for PHANDLE. lol.

Just out of interest, how do you specify the STRICT option - do you just 
stick it in the conditional defines?

I will go hang my head in shame now,

thanks again,

Mike C




"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:46a5320b$1@newsgroups.borland.com...
>
> "Mike Collins" <its@TheBottomOfThePost> wrote in message
> news:46a52487$1@newsgroups.borland.com...
>
>>      vecFileWatch.push_back(SectionHandle);  //Always seems to assign 1
> into
>> the vector...
>
Re: PHANDLE, HANDLE comparisions and vectors
#1515
Author: Remy Lebeau (TeamB)
Date: Tue, 24 Jul 2007 02:06
11 lines
1256 bytes
"Mike Collins" <its@TheBottomOfThePost> wrote in message
news:46a54424@newsgroups.borland.com...

> Just out of interest, how do you specify the STRICT
> option - do you just stick it in the conditional defines?

Yes.


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads