0

我定义了一个函数

HRESULT AMEPreviewHandler:: CreateHtmlPreview()
{
    ULONG  CbRead;
    const int Size= 115000;
    char Buffer[Size+1];
    HRESULT hr = m_pStream->Read(Buffer, Size, &CbRead ); 
    //this m_pStream is not accessible here even it is declared globally. the program is asking me to 
    // declare it static because this CreateHtmlPreview() function called 
    //inside the Static function (i mean here :-static CreateDialog\WM_Command\CreateHtmlPreview();)
    //but if i declare it static the two problems arised are 
    //(1.) It is not able to access the value of the m_pStream which is defined globally.
    //(2.)If i declare it static globally then there are so many other function which are using this
    // value of m_pStream are not able to access it because they are non static.  

}

它在我的程序中的某处被声明为静态的,如下所示:

static HRESULT CreateHtmlPreview(); //i have declared it static because i am calling this function from DialogProc function.If i dont create it static here it dont work

//The function CreateHtmlPreview() is called inside the DialogProc function like this-

BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam) 
{......
case WM_COMMAND:
{  
    int ctl = LOWORD(wParam);
    int event = HIWORD(wParam);

    if (ctl == IDC_PREVIOUS && event == BN_CLICKED ) 
    {                       
        CreateHtmlPreview(); //here i am calling the function
        return 0;
    }  
}

}

那么可以做些什么来使静态函数定义m_pStream中的非静态值可访问?CreateHtmlPreview()

4

4 回答 4

1

在静态类函数中,您只能访问静态类成员。

于 2013-07-22T14:01:29.570 回答
0

您不能将 m_pStream var 作为函数参数传递吗?

而不是以这种方式定义函数

HRESULT AMEPreviewHandler:: CreateHtmlPreview()
{

    ULONG  CbRead;
    const int Size= 115000;
    char Buffer[Size+1];
    HRESULT hr = m_pStream->Read(Buffer, Size, &CbRead );

}

你可以这样做(你应该定义流类型!)

HRESULT AMEPreviewHandler:: CreateHtmlPreview(stream)
{

    ULONG  CbRead;
    const int Size= 115000;
    char Buffer[Size+1];
    HRESULT hr = stream->Read(Buffer, Size, &CbRead );

}

像这样称呼它

CreateHtmlPreview(m_pStream);
于 2013-07-22T14:04:18.560 回答
0

如果你做CreateHtmlPreview()一个免费的功能呢?
如果你让它只是创建一个 html 预览(而不是从流中读取)怎么办?

void CreateHtmlPreview(const char * buffer, int size)
{
  //...
}

然后从proc中读取数据,并调用它DialogProc

//...
m_pStream->Read(Buffer, Size, &CbRead ); 
CreateHtmlPreview(Buffer, Size);

不过,您可能需要使该函数返回预览以供使用。
你确实说你需要成功

静态的,因为我从 DialogProc 函数调用这个函数

但是,DialogProc 不是静态的(在您发布的代码中),所以我看不出问题是什么。

于 2013-07-22T14:11:44.120 回答
0

DoctorLove 我已经通过使用这个参数访问非静态变量的想法解决了这个问题 - 问题是我没有初始化 WM_INITDIALOG 中的实例现在我像这样打盹 -

case WM_INITDIALOG: 
            {

                instance = (AMEPreviewHandler*)lParam;
                instance->m_pStream;
return0;
}

它工作正常。

于 2013-07-23T10:04:00.000 回答