Problem with calling VB ActiveX from callback 
Author Message
 Problem with calling VB ActiveX from callback

Hi, ALL!

Please help me with solving the following problem:
I have :
   - Some DLL written on plain C
   - VB ActiveX that is intended to work with database
   - ATL ActiveX that should wrap the DLL and provide him some
callbacks.

ATL ActiveX has a special method Initialize, that makes some specific
work and passes to the DLL pointer to a callback.
The callback should call some VB ActiveX method.

I can successfuly create instance of VB ActiveX in Initialize function
of ATL ActiveX by wrapping VB type library and using appropriate methods

of COleDispatchDriver (such as AttachDispatch, CreateDispatch etc.).

But when I'm trying to pass a pointer to VB ActiveX instance to
callback  (through call to DLL, global variable, memory mapped file or
whatever)- it is arriving in a non-consistent form. (i.e., calls to
methods cause page faults).
When I tried to create VB ActiveX instance in callback function "on
place", I succeeded to make it 3-4 time  and after that I get error
messages.

Thanks in advance.
Your help will be very appreciated!



Mon, 10 Mar 2003 03:00:00 GMT  
 Problem with calling VB ActiveX from callback
First, make sure that you are not in the following situations:
- VB Activex is in one process, the ATL activeX in another
- the callback is not class member pointer (and you do not
have the 'this' pointer)
- your objects live long enough to complete the call
- are you passing a pointer to the VB object or the pointer
to an interface that you defined ?

More information needed for a better newsgroup debugging !

FReD



Quote:

> Hi, ALL!

> Please help me with solving the following problem:
> I have :
>    - Some DLL written on plain C
>    - VB ActiveX that is intended to work with database
>    - ATL ActiveX that should wrap the DLL and provide him some
> callbacks.

> ATL ActiveX has a special method Initialize, that makes some specific
> work and passes to the DLL pointer to a callback.
> The callback should call some VB ActiveX method.

> I can successfuly create instance of VB ActiveX in Initialize function
> of ATL ActiveX by wrapping VB type library and using appropriate
methods

> of COleDispatchDriver (such as AttachDispatch, CreateDispatch etc.).

> But when I'm trying to pass a pointer to VB ActiveX instance to
> callback  (through call to DLL, global variable, memory mapped file or
> whatever)- it is arriving in a non-consistent form. (i.e., calls to
> methods cause page faults).
> When I tried to create VB ActiveX instance in callback function "on
> place", I succeeded to make it 3-4 time  and after that I get error
> messages.

> Thanks in advance.
> Your help will be very appreciated!

Sent via Deja.com http://www.deja.com/
Before you buy.


Mon, 10 Mar 2003 03:00:00 GMT  
 Problem with calling VB ActiveX from callback

Hi!

Quote:

> First, make sure that you are not in the following situations:
> - VB Activex is in one process, the ATL activeX in another

Not, they are in one process. The process calls for ATL activeX that itself
creates instance of VB ActiveX.

Quote:

> - the callback is not class member pointer (and you do not
> have the 'this' pointer)

The callback is just function

Quote:

> - your objects live long enough to complete the call

I hope :-). At least I do not destroys them explicitly

Quote:
> - are you passing a pointer to the VB object or the pointer to an
> interface that you defined ?

I'm creating a class wrapper for VB activeX from a type library and passing
pointer to it.

Quote:
> More information needed for a better newsgroup debugging !

Now some pieces of code:

DLL: =====================================================

BOOL _stdcall InitCB(PCBStruct pCB)
{
  HANDLE hThread;

  g_bTerminate=FALSE;
  hThread=CreateThread(NULL,0,MyThreadProc,(LPVOID)pCB,0,NULL);
  if(!hThread)
    return FALSE;

  return TRUE;

Quote:
}

DWORD WINAPI MyThreadProc(LPVOID lpvParam)
{
   PCBStruct pCB;

   VB* pVB;// pointer to class that is wrapper of VB ActiveX
   TCBFunction fCB;

   pCB=(PCBStruct)lpvParam;
   pVB=pCB->pVB; //pointer to VB ActiveX instance
   fCB=pCB->fCB; //callback function located in ATL ActiveX

   do {
      fCB(pVB);
      Sleep(1000);
   }
   while(!g_bTerminate);

   return 1;

Quote:
}

End of DLL =======================================================

ATL ActiveX====================================================

//ATL ActiveX class definition
class CATL_MFC_AX {
...
public VB m_VB //instance of VB activeX

Quote:
}

typedef int (_stdcall *TInitCB)(PCBStruct pCB);

//Callback function

void _stdcall MyCB(VB* pVB)
{
  pVB->Foo(); //CRASH!!!! Pointer to Foo is corrupted!

Quote:
}

STDMETHODIMP CATL_MFC_AX::Initialize()
{
 AFX_MANAGE_STATE(AfxGetStaticModuleState())

   g_hLib=LoadLibrary("MyDLL.dll");
  if(g_hLib){
     g_fInitCB=(TInitCB)GetProcAddress(g_hLib,"InitCB");
     if(!g_fInitCB)
            MessageBox(NULL,"Failed to GetProcAddress.
InitCB","ATL_AX",MB_OK);
    else {
             COleException e;
      CLSID clsid;
      if (CLSIDFromProgID(OLESTR("VBObj.A"), &clsid) != NOERROR)
   {
     AfxMessageBox("UNABLE 2 CLSIDFromProgID");
     return S_FALSE;
   }

 // try to get the active VB activeX before creating a new one
   LPUNKNOWN lpUnk;
   LPDISPATCH lpDispatch;
   if (GetActiveObject(clsid, NULL, &lpUnk) == NOERROR)
   {
     HRESULT hr = lpUnk->QueryInterface(IID_IDispatch,(LPVOID*)&lpDispatch);

     lpUnk->Release();
     if (hr == NOERROR)
      m_VB.AttachDispatch(lpDispatch, TRUE);
   }

 // if not dispatch ptr attached yet, need to create one
       if (m_VB.m_lpDispatch == NULL &&
      !m_VB.CreateDispatch(clsid, &e))
   {
     AfxMessageBox("UNABLE_TO_CREATE");
     return S_FALSE;
   }
      AfxMessageBox("Inside Initialize");

        m_VB.Foo();//WORKING HERE!

      TCBStruct cb;

   cb.fCB=MyCB;
   cb.pVB=&m_VB;

                 g_fInitCB(&cb);
       }
    }
  else
       MessageBox(NULL,"Failed to LoadLibrary","ATL_AX",MB_OK);

 return S_OK;

Quote:
}

End of ATL ActiveX =======================================================

Main VB process ====================================================
.......
Dim MyATL As Object
Set MyATL = CreateObject("ATLObject.A")
MyATL.Initialize
'Crash
MyAtl.SomeFunction
...........

End of Main VB process ====================================================

Comment :
   As I just said, I tried to pass pVB using many ways - global variable,
Memory mapped files etc.

Quote:



> > Hi, ALL!

> > Please help me with solving the following problem:
> > I have :
> >    - Some DLL written on plain C
> >    - VB ActiveX that is intended to work with database
> >    - ATL ActiveX that should wrap the DLL and provide him some
> > callbacks.

> > ATL ActiveX has a special method Initialize, that makes some specific
> > work and passes to the DLL pointer to a callback.
> > The callback should call some VB ActiveX method.

> > I can successfuly create instance of VB ActiveX in Initialize function
> > of ATL ActiveX by wrapping VB type library and using appropriate
> methods

> > of COleDispatchDriver (such as AttachDispatch, CreateDispatch etc.).

> > But when I'm trying to pass a pointer to VB ActiveX instance to
> > callback  (through call to DLL, global variable, memory mapped file or
> > whatever)- it is arriving in a non-consistent form. (i.e., calls to
> > methods cause page faults).
> > When I tried to create VB ActiveX instance in callback function "on
> > place", I succeeded to make it 3-4 time  and after that I get error
> > messages.

> > Thanks in advance.
> > Your help will be very appreciated!

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Mon, 10 Mar 2003 03:00:00 GMT  
 Problem with calling VB ActiveX from callback
I should have said 'thread' instead of 'process' because
it is exactly what you are trying to do (CreateThread). And it is
why it is working the first time.
If your goal is to create a timer that will call your ATL method
every 1000 ms: that is not the way to go !
The best solution is to create a standard windows timer (in-process).

If you really want to pass pointers accross threads, then search
for marshalling in the documentation.
Ex: VB/ATL <-> *own* marshalling <-> thread/dll

If you need such a 'server' then the easiest way could be to
create a real COM wrapper around your dll functions
Ex: VB/ATL <-> standard marshalling <-> ATL Server/dll

FReD



Quote:

> Hi!


> > First, make sure that you are not in the following situations:
> > - VB Activex is in one process, the ATL activeX in another

> Not, they are in one process. The process calls for ATL activeX that
itself
> creates instance of VB ActiveX.

> > - the callback is not class member pointer (and you do not
> > have the 'this' pointer)

> The callback is just function

> > - your objects live long enough to complete the call

> I hope :-). At least I do not destroys them explicitly

> > - are you passing a pointer to the VB object or the pointer to an
> > interface that you defined ?

> I'm creating a class wrapper for VB activeX from a type library and
passing
> pointer to it.

> > More information needed for a better newsgroup debugging !

> Now some pieces of code:

> DLL: =====================================================

> BOOL _stdcall InitCB(PCBStruct pCB)
> {
>   HANDLE hThread;

>   g_bTerminate=FALSE;
>   hThread=CreateThread(NULL,0,MyThreadProc,(LPVOID)pCB,0,NULL);
>   if(!hThread)
>     return FALSE;

>   return TRUE;
> }

> DWORD WINAPI MyThreadProc(LPVOID lpvParam)
> {
>    PCBStruct pCB;

>    VB* pVB;// pointer to class that is wrapper of VB ActiveX
>    TCBFunction fCB;

>    pCB=(PCBStruct)lpvParam;
>    pVB=pCB->pVB; //pointer to VB ActiveX instance
>    fCB=pCB->fCB; //callback function located in ATL ActiveX

>    do {
>       fCB(pVB);
>       Sleep(1000);
>    }
>    while(!g_bTerminate);

>    return 1;
> }

> End of DLL =======================================================

> ATL ActiveX====================================================

> //ATL ActiveX class definition
> class CATL_MFC_AX {
> ...
> public VB m_VB //instance of VB activeX

> }
> typedef int (_stdcall *TInitCB)(PCBStruct pCB);

> //Callback function

> void _stdcall MyCB(VB* pVB)
> {
>   pVB->Foo(); //CRASH!!!! Pointer to Foo is corrupted!
> }

> STDMETHODIMP CATL_MFC_AX::Initialize()
> {
>  AFX_MANAGE_STATE(AfxGetStaticModuleState())

>    g_hLib=LoadLibrary("MyDLL.dll");
>   if(g_hLib){
>      g_fInitCB=(TInitCB)GetProcAddress(g_hLib,"InitCB");
>      if(!g_fInitCB)
>             MessageBox(NULL,"Failed to GetProcAddress.
> InitCB","ATL_AX",MB_OK);
>     else {
>              COleException e;
>       CLSID clsid;
>       if (CLSIDFromProgID(OLESTR("VBObj.A"), &clsid) != NOERROR)
>    {
>      AfxMessageBox("UNABLE 2 CLSIDFromProgID");
>      return S_FALSE;
>    }

>  // try to get the active VB activeX before creating a new one
>    LPUNKNOWN lpUnk;
>    LPDISPATCH lpDispatch;
>    if (GetActiveObject(clsid, NULL, &lpUnk) == NOERROR)
>    {
>      HRESULT hr = lpUnk->QueryInterface(IID_IDispatch,(LPVOID*)
&lpDispatch);

>      lpUnk->Release();
>      if (hr == NOERROR)
>       m_VB.AttachDispatch(lpDispatch, TRUE);
>    }

>  // if not dispatch ptr attached yet, need to create one
>        if (m_VB.m_lpDispatch == NULL &&
>       !m_VB.CreateDispatch(clsid, &e))
>    {
>      AfxMessageBox("UNABLE_TO_CREATE");
>      return S_FALSE;
>    }
>       AfxMessageBox("Inside Initialize");

>         m_VB.Foo();//WORKING HERE!

>       TCBStruct cb;

>    cb.fCB=MyCB;
>    cb.pVB=&m_VB;

>                  g_fInitCB(&cb);
>        }
>     }
>   else
>        MessageBox(NULL,"Failed to LoadLibrary","ATL_AX",MB_OK);

>  return S_OK;
> }

> End of ATL ActiveX

=======================================================
Quote:

> Main VB process ====================================================
> .......
> Dim MyATL As Object
> Set MyATL = CreateObject("ATLObject.A")
> MyATL.Initialize
> 'Crash
> MyAtl.SomeFunction
> ...........

> End of Main VB process

====================================================

- Show quoted text -

Quote:

> Comment :
>    As I just said, I tried to pass pVB using many ways - global
variable,
> Memory mapped files etc.



> > > Hi, ALL!

> > > Please help me with solving the following problem:
> > > I have :
> > >    - Some DLL written on plain C
> > >    - VB ActiveX that is intended to work with database
> > >    - ATL ActiveX that should wrap the DLL and provide him some
> > > callbacks.

> > > ATL ActiveX has a special method Initialize, that makes some
specific
> > > work and passes to the DLL pointer to a callback.
> > > The callback should call some VB ActiveX method.

> > > I can successfuly create instance of VB ActiveX in Initialize
function
> > > of ATL ActiveX by wrapping VB type library and using appropriate
> > methods

> > > of COleDispatchDriver (such as AttachDispatch, CreateDispatch
etc.).

> > > But when I'm trying to pass a pointer to VB ActiveX instance to
> > > callback  (through call to DLL, global variable, memory mapped
file or
> > > whatever)- it is arriving in a non-consistent form. (i.e., calls
to
> > > methods cause page faults).
> > > When I tried to create VB ActiveX instance in callback
function "on
> > > place", I succeeded to make it 3-4 time  and after that I get
error
> > > messages.

> > > Thanks in advance.
> > > Your help will be very appreciated!

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 11 Mar 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Atl NT service calling VB ActiveX dll had problem when clean-up

2. VB script calling activeX control problem

3. VC calling VB ActiveX cuases Problems--HELP!

4. VB callback function, called from a VC DLL, crashes

5. Problem using VB 5 activex DLL in a VC++ activex control

6. Call a C++ ActiveX method from a VB exe program

7. VB image list handle isn't fully accessible in VC++ ActiveX thru COM call

8. calling VB activex control from VC

9. How do you call VB ActiveX DLL functions from VC

10. Call/Use VB ActiveX Dll in Visual C

11. calling vb activex dll

12. How can I call a VB ActiveX dll from VC++

 

 
Powered by phpBB® Forum Software