用vc的wizard做一个运行时不可见的a.ocx,里面加两个方法,long GetData()
{
   return globaldata;
}
void SetData( long data )
{
   globaldata = data;
}在ctl的cpp文件的最上面加入
long globaldata=0;  // 全局变量再用wizard建立一exe,把ocx加入工程,在dialog上放入两个一样的这个ocx,名字分别叫m_1,m_2,然后写如下程序
void CMy112Dlg::OnOK() 
{
m_1.SetData( 100 );   // 第一句
long a = m_2.GetData();  // 第二句
}
运行到第二句后,a=2, 我没有看花眼吧???这是怎么回事啊?有什么办法可以避免这个问题吗?应该ocx中要用到如下的回调函数:
LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);
所以,在函数中,我要有一些参数才能工作的,所以要用到全局变量的,但出现了这样的问题,请问我该怎么解决??up有分

解决方案 »

  1.   

    long globaldata=0;  // 全局变量
    globaldata不能在m-1,m-2里面共享,
    如果要共享的话需要新建立一个共享段,把globaldata放在里面
      

  2.   

    我觉得没有共享到吧?m1,m2的值不同啊???你这两个函数在类里面吗?
    那么我们在这个类的构造函数中再次初始化你的globaldata=0;应该可以解决吧。
      

  3.   

    globaldata是一个全局变量,没有放在类中啊,m1m2的值应该是不同的,但是现在相同了,也就是说,如果我设置了m1的那个globaldata,则我取出的m2的globaldata与m1的相同,我建议楼上的几位可以试试,因为这是我试下来的结果,不排除我的电脑中病毒或者vc坏了
      

  4.   

    我现在把ocx的代码贴上来
    // My555Ctl.cpp : Implementation of the CMy555Ctrl ActiveX Control class.#include "stdafx.h"
    #include "555.h"
    #include "My555Ctl.h"
    #include "My555Ppg.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    IMPLEMENT_DYNCREATE(CMy555Ctrl, COleControl)
    long globaldata=0; // 变量在这里申明的/////////////////////////////////////////////////////////////////////////////
    // Message mapBEGIN_MESSAGE_MAP(CMy555Ctrl, COleControl)
    //{{AFX_MSG_MAP(CMy555Ctrl)
    // NOTE - ClassWizard will add and remove message map entries
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG_MAP
    ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Dispatch mapBEGIN_DISPATCH_MAP(CMy555Ctrl, COleControl)
    //{{AFX_DISPATCH_MAP(CMy555Ctrl)
    DISP_FUNCTION(CMy555Ctrl, "GetData", GetData, VT_I4, VTS_NONE)
    DISP_FUNCTION(CMy555Ctrl, "SetData", SetData, VT_EMPTY, VTS_I4)
    //}}AFX_DISPATCH_MAP
    DISP_FUNCTION_ID(CMy555Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
    END_DISPATCH_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Event mapBEGIN_EVENT_MAP(CMy555Ctrl, COleControl)
    //{{AFX_EVENT_MAP(CMy555Ctrl)
    // NOTE - ClassWizard will add and remove event map entries
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_EVENT_MAP
    END_EVENT_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Property pages// TODO: Add more property pages as needed.  Remember to increase the count!
    BEGIN_PROPPAGEIDS(CMy555Ctrl, 1)
    PROPPAGEID(CMy555PropPage::guid)
    END_PROPPAGEIDS(CMy555Ctrl)
    /////////////////////////////////////////////////////////////////////////////
    // Initialize class factory and guidIMPLEMENT_OLECREATE_EX(CMy555Ctrl, "MY555.My555Ctrl.1",
    0xe10b47da, 0x9f9f, 0x4a1c, 0x9c, 0x37, 0xac, 0x4c, 0x34, 0xf7, 0x78, 0xc4)
    /////////////////////////////////////////////////////////////////////////////
    // Type library ID and versionIMPLEMENT_OLETYPELIB(CMy555Ctrl, _tlid, _wVerMajor, _wVerMinor)
    /////////////////////////////////////////////////////////////////////////////
    // Interface IDsconst IID BASED_CODE IID_DMy555 =
    { 0x8e0b7fa5, 0xe19b, 0x4127, { 0xa3, 0xd2, 0x8a, 0x8b, 0x30, 0x3a, 0x8c, 0xeb } };
    const IID BASED_CODE IID_DMy555Events =
    { 0x5600eab5, 0xf696, 0x4e54, { 0xb8, 0x81, 0xe, 0x5b, 0xa3, 0xe5, 0xc0, 0xd1 } };
    /////////////////////////////////////////////////////////////////////////////
    // Control type informationstatic const DWORD BASED_CODE _dwMy555OleMisc =
    OLEMISC_SETCLIENTSITEFIRST |
    OLEMISC_INSIDEOUT |
    OLEMISC_CANTLINKINSIDE |
    OLEMISC_RECOMPOSEONRESIZE;IMPLEMENT_OLECTLTYPE(CMy555Ctrl, IDS_MY555, _dwMy555OleMisc)
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::CMy555CtrlFactory::UpdateRegistry -
    // Adds or removes system registry entries for CMy555CtrlBOOL CMy555Ctrl::CMy555CtrlFactory::UpdateRegistry(BOOL bRegister)
    {
    // TODO: Verify that your control follows apartment-model threading rules.
    // Refer to MFC TechNote 64 for more information.
    // If your control does not conform to the apartment-model rules, then
    // you must modify the code below, changing the 6th parameter from
    // afxRegApartmentThreading to 0. if (bRegister)
    return AfxOleRegisterControlClass(
    AfxGetInstanceHandle(),
    m_clsid,
    m_lpszProgID,
    IDS_MY555,
    IDB_MY555,
    afxRegApartmentThreading,
    _dwMy555OleMisc,
    _tlid,
    _wVerMajor,
    _wVerMinor);
    else
    return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::CMy555Ctrl - ConstructorCMy555Ctrl::CMy555Ctrl()
    {
    InitializeIIDs(&IID_DMy555, &IID_DMy555Events); // TODO: Initialize your control's instance data here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::~CMy555Ctrl - DestructorCMy555Ctrl::~CMy555Ctrl()
    {
    // TODO: Cleanup your control's instance data here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::OnDraw - Drawing functionvoid CMy555Ctrl::OnDraw(
    CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    // TODO: Replace the following code with your own drawing code.
    pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
    pdc->Ellipse(rcBounds);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::DoPropExchange - Persistence supportvoid CMy555Ctrl::DoPropExchange(CPropExchange* pPX)
    {
    ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
    COleControl::DoPropExchange(pPX); // TODO: Call PX_ functions for each persistent custom property.}
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::OnResetState - Reset control to default statevoid CMy555Ctrl::OnResetState()
    {
    COleControl::OnResetState();  // Resets defaults found in DoPropExchange // TODO: Reset any other control state here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::AboutBox - Display an "About" box to the uservoid CMy555Ctrl::AboutBox()
    {
    CDialog dlgAbout(IDD_ABOUTBOX_MY555);
    dlgAbout.DoModal();
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl message handlers// 两个方法在这里long CMy555Ctrl::GetData() 
    {
    return globaldata;
    }void CMy555Ctrl::SetData(long data) 
    {
    globaldata = data;
    }头文件没有动过,只修改了cpp
      

  5.   

    我现在把ocx的代码贴上来
    // My555Ctl.cpp : Implementation of the CMy555Ctrl ActiveX Control class.#include "stdafx.h"
    #include "555.h"
    #include "My555Ctl.h"
    #include "My555Ppg.h"
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    IMPLEMENT_DYNCREATE(CMy555Ctrl, COleControl)
    long globaldata=0; // 变量在这里申明的/////////////////////////////////////////////////////////////////////////////
    // Message mapBEGIN_MESSAGE_MAP(CMy555Ctrl, COleControl)
    //{{AFX_MSG_MAP(CMy555Ctrl)
    // NOTE - ClassWizard will add and remove message map entries
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG_MAP
    ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
    END_MESSAGE_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Dispatch mapBEGIN_DISPATCH_MAP(CMy555Ctrl, COleControl)
    //{{AFX_DISPATCH_MAP(CMy555Ctrl)
    DISP_FUNCTION(CMy555Ctrl, "GetData", GetData, VT_I4, VTS_NONE)
    DISP_FUNCTION(CMy555Ctrl, "SetData", SetData, VT_EMPTY, VTS_I4)
    //}}AFX_DISPATCH_MAP
    DISP_FUNCTION_ID(CMy555Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
    END_DISPATCH_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Event mapBEGIN_EVENT_MAP(CMy555Ctrl, COleControl)
    //{{AFX_EVENT_MAP(CMy555Ctrl)
    // NOTE - ClassWizard will add and remove event map entries
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_EVENT_MAP
    END_EVENT_MAP()
    /////////////////////////////////////////////////////////////////////////////
    // Property pages// TODO: Add more property pages as needed.  Remember to increase the count!
    BEGIN_PROPPAGEIDS(CMy555Ctrl, 1)
    PROPPAGEID(CMy555PropPage::guid)
    END_PROPPAGEIDS(CMy555Ctrl)
    /////////////////////////////////////////////////////////////////////////////
    // Initialize class factory and guidIMPLEMENT_OLECREATE_EX(CMy555Ctrl, "MY555.My555Ctrl.1",
    0xe10b47da, 0x9f9f, 0x4a1c, 0x9c, 0x37, 0xac, 0x4c, 0x34, 0xf7, 0x78, 0xc4)
    /////////////////////////////////////////////////////////////////////////////
    // Type library ID and versionIMPLEMENT_OLETYPELIB(CMy555Ctrl, _tlid, _wVerMajor, _wVerMinor)
    /////////////////////////////////////////////////////////////////////////////
    // Interface IDsconst IID BASED_CODE IID_DMy555 =
    { 0x8e0b7fa5, 0xe19b, 0x4127, { 0xa3, 0xd2, 0x8a, 0x8b, 0x30, 0x3a, 0x8c, 0xeb } };
    const IID BASED_CODE IID_DMy555Events =
    { 0x5600eab5, 0xf696, 0x4e54, { 0xb8, 0x81, 0xe, 0x5b, 0xa3, 0xe5, 0xc0, 0xd1 } };
    /////////////////////////////////////////////////////////////////////////////
    // Control type informationstatic const DWORD BASED_CODE _dwMy555OleMisc =
    OLEMISC_SETCLIENTSITEFIRST |
    OLEMISC_INSIDEOUT |
    OLEMISC_CANTLINKINSIDE |
    OLEMISC_RECOMPOSEONRESIZE;IMPLEMENT_OLECTLTYPE(CMy555Ctrl, IDS_MY555, _dwMy555OleMisc)
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::CMy555CtrlFactory::UpdateRegistry -
    // Adds or removes system registry entries for CMy555CtrlBOOL CMy555Ctrl::CMy555CtrlFactory::UpdateRegistry(BOOL bRegister)
    {
    // TODO: Verify that your control follows apartment-model threading rules.
    // Refer to MFC TechNote 64 for more information.
    // If your control does not conform to the apartment-model rules, then
    // you must modify the code below, changing the 6th parameter from
    // afxRegApartmentThreading to 0. if (bRegister)
    return AfxOleRegisterControlClass(
    AfxGetInstanceHandle(),
    m_clsid,
    m_lpszProgID,
    IDS_MY555,
    IDB_MY555,
    afxRegApartmentThreading,
    _dwMy555OleMisc,
    _tlid,
    _wVerMajor,
    _wVerMinor);
    else
    return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::CMy555Ctrl - ConstructorCMy555Ctrl::CMy555Ctrl()
    {
    InitializeIIDs(&IID_DMy555, &IID_DMy555Events); // TODO: Initialize your control's instance data here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::~CMy555Ctrl - DestructorCMy555Ctrl::~CMy555Ctrl()
    {
    // TODO: Cleanup your control's instance data here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::OnDraw - Drawing functionvoid CMy555Ctrl::OnDraw(
    CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    // TODO: Replace the following code with your own drawing code.
    pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
    pdc->Ellipse(rcBounds);
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::DoPropExchange - Persistence supportvoid CMy555Ctrl::DoPropExchange(CPropExchange* pPX)
    {
    ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
    COleControl::DoPropExchange(pPX); // TODO: Call PX_ functions for each persistent custom property.}
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::OnResetState - Reset control to default statevoid CMy555Ctrl::OnResetState()
    {
    COleControl::OnResetState();  // Resets defaults found in DoPropExchange // TODO: Reset any other control state here.
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl::AboutBox - Display an "About" box to the uservoid CMy555Ctrl::AboutBox()
    {
    CDialog dlgAbout(IDD_ABOUTBOX_MY555);
    dlgAbout.DoModal();
    }
    /////////////////////////////////////////////////////////////////////////////
    // CMy555Ctrl message handlers// 两个方法在这里long CMy555Ctrl::GetData() 
    {
    return globaldata;
    }void CMy555Ctrl::SetData(long data) 
    {
    globaldata = data;
    }头文件没有动过,只修改了cpp
      

  6.   

    我试了
    m_1.SetData( 100 );   // 第一句
    long a = m_2.GetData();  // 第二句
    a读出来为100,结果是对的。Win2k+VC6下globaldata 为全局变量,而ocx是dll,是进程内对象,多个ocx是共享一个进程空间的,所以两个对象的globaldata是同一地址单无,结果为100才用。楼主看看你哪儿搞错了。建立一个共享段是进程间共享数据才用的方法。
      

  7.   

    因为这个ocx里面,我要用到setwindowlong去替换winproc,在新的winproc里我要用到一些参数,我没办法啊,只好用全局变量,有没有什么办法吗?
      

  8.   

    如果你需要两个对象的这个属性不是同一个,那么应该改为成员变量啊,不要用全局变量。
    long globaldata=0; // 变量在这里申明的
    的声明放在头文件类里面。