全局变量的行为在不同的功能中变化

时间:2011-04-15 02:17:21

标签: c++ winapi string global-variables filenames

我正在编写一个重命名AVI文件的程序(添加扩展名 - 由于某种原因需要),然后从中获取框架并将其存储在BMP文件中。

以下是代码的重要部分:

#include "stdafx.h"
#include "ImageProcessor2.h"
#include <windows.h>
#include <Commdlg.h>
#include <Commctrl.h>
#include <stdio.h>
#include <string>
#include "vfw.h"
#include <atlstr.h>

// Global Variables...
HINSTANCE g_hInst;
LPSTR selectedAVI=NULL;

INT_PTR CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);
void OpenDialog(HWND);
void GetFrame(HWND);

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine, int nCmdShow)
{
    g_hInst = hInstance;
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL,(DLGPROC)DialogProc);
    return 0;
}


INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch ( uMsg )
    {
        case WM_INITDIALOG:
            SetClassLongPtr(hWnd, GCLP_HICONSM, 
                (LONG_PTR)LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_IMAGEPROCESSOR2)));
            break;
        case WM_COMMAND:
            switch ( LOWORD(wParam) )
            {
                case IDCANCEL:
                    EndDialog(hWnd, LOWORD(wParam));
                    break;
                case IDB_LOADVID:
                    OpenDialog(hWnd);
                    break;
                case IDB_GETFRAME:
                    if (selectedAVI==0)
                        {
                            MessageBox(NULL, "Select the file by pressing Load button first", "Info", MB_OK);
                            break;
                        }
                    else
                    GetFrame(hWnd);
                    break;
            }
            break;
        break;
    }
    return FALSE;
}

void OpenDialog(HWND hWnd) 
{
  OPENFILENAME ofn;
  TCHAR szFile[MAX_PATH];
  char* renamedfile;

  ZeroMemory(&ofn, sizeof(ofn));
  ofn.lStructSize = sizeof(ofn);
  ofn.lpstrFile = szFile;
  ofn.lpstrFile[0] = '\0';
  ofn.hwndOwner = hWnd;
  ofn.nMaxFile = sizeof(szFile);
  ofn.lpstrFilter = TEXT("All files(*.*)\0*.*\0");
  ofn.nFilterIndex = 1;
  ofn.lpstrInitialDir = NULL;
  ofn.lpstrFileTitle = NULL;
  ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;


    if (GetOpenFileName(&ofn))
        {
            //call to rename function
            ofn.lpstrFile=renamedfile;
            MCIWndCreate(GetDlgItem(hWnd, IDC_ANIRENDER), (HINSTANCE)g_hInst,WS_CHILD|WS_VISIBLE|WS_SYSMENU|MCIWNDF_NOOPEN|MCIWNDF_NOMENU|MCIWNDF_NOTIFYALL, ofn.lpstrFile);
            selectedAVI=ofn.lpstrFile;
            SetDlgItemTextA(hWnd, IDC_EDIT1, selectedAVI);
        }
}

//rename function is here

void GetFrame(HWND hWnd)
{
    LONG hr; 
    PAVIFILE pfile;
    SetDlgItemTextA(hWnd, IDC_EDIT2, selectedAVI);

    AVIFileInit();          // opens AVIFile library 
    //further AVI processing functions are here
    AVIFileExit();          // releases AVIFile library 
}

现在请解释我为什么会这样:

EDIT1中的

:文件的正确路径(例如“D:\ test \ testAVI.avi”)

在EDIT2中:“îţîţîîţîţîî”等等,以“ţ™Öj8p”结尾

我认为它应该显示相同的字符串,因为它从同一个全局变量读取。 我究竟做错了什么?怎么回事?

谢谢

1 个答案:

答案 0 :(得分:3)

因为selectedAVI是一个指针,并且没有自己的存储,并且它指向的东西(确实有存储)是局部变量的一部分(OPENFILENAME结构在{{ 1}}),已超出范围,并在到达OpenDialog时被覆盖。

要解决此问题,请将其更改为长度为IDC_EDIT2的数组,并使用MAX_PATH或类似内容分配给它。然后它应该按照你想要的方式工作。