jd9288
asked on
Pointer to NETRESOURCE
Hi, this is a little complicated...
I'm passing a pointer to NETRESOURCE to my class to be used later, like so:
NETRESOURCE *nr; //valid at this point
HTREEITEM ht;
CDomains * e = new CDomains( FALSE, &ht, nr );
Then in the class constructor I'm making a copy of the pointer:
CDomains( BOOL bWMI, HTREEITEM *hProvider, NETRESOURCE * nr = NULL, LPVOID r = NULL )
{
m_hProvider = new HTREEITEM;
memcpy(m_hProvider, hProvider, sizeof(HTREEITEM));
if ( nr ) {
m_pnr = new NETRESOURCE;
memcpy(m_pnr, nr, sizeof(NETRESOURCE));
} else {
m_pnr = NULL;
}
}
Now comes the tricky part. This class inherits from another class that has Start() function. I call it next:
e->Start();
The Start() function of the class CDomains inherits from:
void CSuper::Start()
{
HANDLE hThread = NULL;
InterlockedIncrement( &g->ThreadCount );
DWORD id;
hThread = CreateThread( NULL, THREAD_STACK_SIZE, Enumerate, this, CREATE_SUSPENDED, &id );
DWORD dwWaitResult = WaitForSingleObject( g->hSemaphore, INFINITE); //wait
switch( dwWaitResult )
{
case WAIT_OBJECT_0:
{
if ( hThread ) {
// thread created
SetThreadPriority( hThread, THREAD_PRIORITY_LOWEST );
ResumeThread( hThread );
}
}
break;
case WAIT_TIMEOUT:
case WAIT_ABANDONED:
{
//report error
TRACE(_T("startThreadProce ssLog SEMAPHORE Timed Out"));
}
break;
}
CloseHandle( hThread );
}
This goes well, the parameter passed to Enumerate function (this) contains the inheriting class CDomain
with all variables still valid.
The Enumerate function:
DWORD WINAPI CSuper::Enumerate( LPVOID This )
{
CSuper * o = ( CSuper *)This;
o->Enumerate();
return 0;
}
This where the things go sour. All the variables in o->CDomains are still valid,
numeric values in o->CDomains->m_pnr are still valid, but all the string
members of that struct are now invalid pointers.
Please help, I'm willing to give more points if necessary.
I'm passing a pointer to NETRESOURCE to my class to be used later, like so:
NETRESOURCE *nr; //valid at this point
HTREEITEM ht;
CDomains * e = new CDomains( FALSE, &ht, nr );
Then in the class constructor I'm making a copy of the pointer:
CDomains( BOOL bWMI, HTREEITEM *hProvider, NETRESOURCE * nr = NULL, LPVOID r = NULL )
{
m_hProvider = new HTREEITEM;
memcpy(m_hProvider, hProvider, sizeof(HTREEITEM));
if ( nr ) {
m_pnr = new NETRESOURCE;
memcpy(m_pnr, nr, sizeof(NETRESOURCE));
} else {
m_pnr = NULL;
}
}
Now comes the tricky part. This class inherits from another class that has Start() function. I call it next:
e->Start();
The Start() function of the class CDomains inherits from:
void CSuper::Start()
{
HANDLE hThread = NULL;
InterlockedIncrement( &g->ThreadCount );
DWORD id;
hThread = CreateThread( NULL, THREAD_STACK_SIZE, Enumerate, this, CREATE_SUSPENDED, &id );
DWORD dwWaitResult = WaitForSingleObject( g->hSemaphore, INFINITE); //wait
switch( dwWaitResult )
{
case WAIT_OBJECT_0:
{
if ( hThread ) {
// thread created
SetThreadPriority( hThread, THREAD_PRIORITY_LOWEST );
ResumeThread( hThread );
}
}
break;
case WAIT_TIMEOUT:
case WAIT_ABANDONED:
{
//report error
TRACE(_T("startThreadProce
}
break;
}
CloseHandle( hThread );
}
This goes well, the parameter passed to Enumerate function (this) contains the inheriting class CDomain
with all variables still valid.
The Enumerate function:
DWORD WINAPI CSuper::Enumerate( LPVOID This )
{
CSuper * o = ( CSuper *)This;
o->Enumerate();
return 0;
}
This where the things go sour. All the variables in o->CDomains are still valid,
numeric values in o->CDomains->m_pnr are still valid, but all the string
members of that struct are now invalid pointers.
Please help, I'm willing to give more points if necessary.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>Could you please tell me why it would still work sometimes
>>the way I had it?
Because the strings are 'there' for a while, at least during an enumeration. You never know when the system will free the momry it used to store the strings in.
BTW, you could extend the above to
class CNetResource : public NETRESOURCE {
public:
CNetResource () { /* ... */ }
CNetResource ( const NETRESOURCE& nr) { /* ... */ }
CNetResource& operator=(const NETRESOURCE& nr) { /* ... */ }
};
to 'automate' that copying process.
>>the way I had it?
Because the strings are 'there' for a while, at least during an enumeration. You never know when the system will free the momry it used to store the strings in.
BTW, you could extend the above to
class CNetResource : public NETRESOURCE {
public:
CNetResource () { /* ... */ }
CNetResource ( const NETRESOURCE& nr) { /* ... */ }
CNetResource& operator=(const NETRESOURCE& nr) { /* ... */ }
};
to 'automate' that copying process.
ASKER
Thank you very much, I think I'm getting there.
ASKER
I thought I'm missing something.
Could you please tell me why it would still work sometimes
the way I had it?