I am writing a program which renames AVI file (add extension - needed for some reason) and then gets frame from it and stores it in BMP file.
Here are the important parts of code:
#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 pfi开发者_Go百科le;
SetDlgItemTextA(hWnd, IDC_EDIT2, selectedAVI);
AVIFileInit(); // opens AVIFile library
//further AVI processing functions are here
AVIFileExit(); // releases AVIFile library
}
Now please explain me why I am getting this:
in EDIT1: correct path to file (such as "D:\test\testAVI.avi")
in EDIT2: "îţîţîîţîţîîţîţî" and so on, ending with "ţ™Öj8p"
I think it should show the same strings because it reads from the same global variable. What am I doing wrong? How do it right?
Thank you
Because selectedAVI
is a pointer and has no storage of its own, and the thing it's pointing to (which does have the storage) is part of a local variable (the OPENFILENAME
structure in OpenDialog
), which has gone out of scope and been overwritten by the time you get to IDC_EDIT2
.
To fix this, change it to an array of length MAX_PATH
and use strcpy
or similar to assign to it. Then it should work the way you want.
精彩评论