none
Using EDITSTREAM CALLBACK RRS feed

  • Question

  • Hallo everyone! I've been having trouble using the EDIT STREAM CALLBACK.When I try to run my application, it fails with unhand-led exception access violation.Here my code.Any help will be highly appreciated.

    case ID_BUTTONA:
       
                DWORD_PTR dwCookie;
        dwCookie = 0;
        DWORD   dwError;
        dwError = 0;
        EDITSTREAMCALLBACK EditStreamCallback;
        EditStreamCallback = NULL ;
        EDITSTREAM es;  
        hFile = (HANDLE)dwCookie;   
    es.dwCookie    = (DWORD_PTR)hFile;
    es.dwError =    dwError;
    es.pfnCallback = EditStreamCallback;


        LPBYTE pbBuff;
        pbBuff = NULL;
        LONG cb;
        PLONG pcb;
        pcb = 0;
        DWORD   write;
        

         WriteFile(hFile, &pbBuff, write, (DWORD *)&pcb, NULL);//
    dwpfn=GetLastError();
          swprintf_s(dwj,100,L"%d",dwpfn);
                            MessageBox(hWnd, dwj
                                       ,
                                       L"Problem with write file",
                                                             MB_OK);
    EditStreamCallback( (DWORD_PTR)hFile ,pbBuff,cb,pcb);
    SendMessage(richedit, EM_STREAMOUT, SF_RTF, (LPARAM)&es);


        hFile = CreateFile(L"Again.rtf",GENERIC_WRITE,
                                  FILE_SHARE_WRITE, 0, CREATE_NEW,
                                  FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile=INVALID_HANDLE_VALUE)
        dw2=GetLastError();
          swprintf_s(dw3,100,L"%d",dw2);
                            MessageBox(hWnd, dw3
                                       ,
                                       L"INVALID_HANDLE_VALUE",
                                       MB_OK);

                case IDM_OPEN:
        
           hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
            COINIT_DISABLE_OLE1DDE);
        if (SUCCEEDED(hr))
        {
            {
              MessageBox(hWnd1, L"com is registered"
                                       ,
                                       L"Error 2",
                                       MB_OK);
                        }    
            IFileOpenDialog *pFileOpen;
            hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
                    IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
            if (SUCCEEDED(hr))
            {
                hr = pFileOpen->Show(NULL);

                if (SUCCEEDED(hr))
                {
                    IShellItem *pItem;
                    hr = pFileOpen->GetResult(&pItem);
                    if (SUCCEEDED(hr))
                    {
                        PWSTR pszFilePath;
                        hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                        if (SUCCEEDED(hr))
                        {
                            MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
                            
    {
        HANDLE hFile1 = CreateFile(pszFilePath, GENERIC_READ,
                                  0, NULL, OPEN_EXISTING,
                                  FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hFile1 = INVALID_HANDLE_VALUE)
        {dwe=GetLastError();
          swprintf_s(dwe5,100,L"%d",dwe);
                            MessageBox(hWnd1, dwe5
                                       ,
                                       L"Error INVALID_HANDLE_VALUE",
                                       MB_OK);
      ReadFile(hFile1, &pbBuff1, sizeof(cb1), (DWORD *)&pcb1, NULL);
    }

    pcb1 = 0;
    cb1 = 100;
    dwCookie    = (DWORD_PTR)hFile1;
    pfnCallback1 = NULL ;
    dwError= 0;
    pfnCallback1(dwCookie,pbBuff1,cb1,pcb1);
    {
    EDITSTREAM es1 = { 0 };
    es1.pfnCallback = pfnCallback1;
    es1.dwCookie    = (DWORD_PTR)hFile1;
    es1.dwError =    dwError;


    if (SendMessage(richedit1, EM_STREAMIN, SF_RTF, (LPARAM)&es1) && es1.dwError == 0)
    CoTaskMemFree(pszFilePath);
            CloseHandle(hFile1);
        }
    }
                        }
                        pItem->Release();
                    }
                }
                pFileOpen->Release();
            }
            CoUninitialize();
        }

    // Basics.cpp : Defines the entry point for the application.
    //

    #include "stdafx.h"
    #include "Basics.h"
    #include "windows.h"
    #include "Winuser.h"
    #include <stdio.h>
    #include "Richedit.h"
    #include <shobjidl.h>

    #define MAX_LOADSTRING 100
    #define ID_BUTTONA      200
    #define  ID_BUTTONB 201


    // Global Variables:
    HINSTANCE hInst;                                // current instance
    TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

    // Forward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK    WndProc1(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);

         // TODO: Place code here.
        MSG msg;
        HACCEL hAccelTable;

        // Initialize global strings
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadString(hInstance, IDC_BASICS, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);

        // Perform application initialization:
        if (!InitInstance (hInstance, nCmdShow))
        {
            return FALSE;
        }

        hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BASICS));

        // Main message loop:
        while (GetMessage(&msg, NULL, 0, 0))
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        return (int) msg.wParam;
    }



    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage are only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        WNDCLASSEX wcex;

        wcex.cbSize = sizeof(WNDCLASSEX);

        wcex.style            = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra        = 0;
        wcex.cbWndExtra        = 0;
        wcex.hInstance        = hInstance;
        wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BASICS));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_BASICS);
        wcex.lpszClassName    = szWindowClass;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

         WNDCLASSEX wcex1;    
        wcex1.cbSize = sizeof(WNDCLASSEX);
        wcex1.style            = CS_HREDRAW | CS_VREDRAW;
        wcex1.lpfnWndProc    = WndProc1;
        wcex1.cbClsExtra        = 0;
        wcex1.cbWndExtra        = 0;
        wcex1.hInstance        = hInstance;
        wcex1.hIcon            = NULL;
        wcex1.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex1.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
        wcex1.lpszMenuName    = MAKEINTRESOURCE ( IDC_BASICS);
        wcex1.lpszClassName    = L"NEW CLASS";
        wcex1.hIconSm        = NULL;

        RegisterClassEx(&wcex1);

        return RegisterClassEx(&wcex);
    }

    //
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;

       hInst = hInstance; // Store instance handle in our global variable

       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

       if (!hWnd)
       {
          return FALSE;
       }

       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);

       return TRUE;
    }

    //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND    - process the application menu
    //  WM_PAINT    - Paint the main window
    //  WM_DESTROY    - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
        HWND hwndButton;
        HWND hwndButtona;
        HWND richedit;
        HANDLE hFile;
        HINSTANCE hInstance;

        switch (message)
        {
            DWORD dw;
        WCHAR dw1[100];
        DWORD dw2;
        WCHAR dw3[100];
        DWORD dwpfn;
        WCHAR dwj[100];
       case WM_CREATE:
                 hwndButton = CreateWindow(
        L"BUTTON",  // Predefined class; Unicode assumed
        L"OK",      // Button text
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
        200,         // x position
        160,         // y position
        150,        // Button width
        100,        // Button height
        hWnd,     // Parent window
        NULL,       // No menu.
        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
        NULL);      // Pointer not needed.
                 LoadLibrary(L"Msftedit.dll");
        richedit = CreateWindowEx(
        WS_EX_ACCEPTFILES | WS_EX_CLIENTEDGE,
        MSFTEDIT_CLASS,
        L"MSFTEDIT_CLASS",
        WS_CHILD | WS_VISIBLE |ES_AUTOHSCROLL | ES_LEFT| ES_SELECTIONBAR| ES_SUNKEN | ES_SAVESEL | ES_AUTOVSCROLL ,
        0,
        0,                  // starting x- and y-coordinates
        0,        // width of client area
        0,  
        hWnd,
        NULL,    
        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
         NULL);
    if (!richedit)
        dw=GetLastError();
          swprintf_s(dw1,100,L"%d",dw);
                            MessageBox(hWnd, dw1
                                       ,
                                       L"Error richedit",
                                       MB_OK);
     SendMessage(richedit, WM_CONTEXTMENU, 0, 0);
      SetWindowPos(
        richedit,
        NULL,
        180,
        265,
        190,
        25,
        NULL);
               hwndButtona = CreateWindow(
        L"BUTTON",  // Predefined class; Unicode assumed
        L"OK",      // Button text
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
        380,         // x position
        265,         // y position
        25,        // Button width
        25,        // Button height
        hWnd,     // Parent window
        (HMENU) ID_BUTTONA,       // No menu.
        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
        NULL);      // Pointer not needed.
            break;

        
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
                case ID_BUTTONA:
       
                DWORD_PTR dwCookie;
        dwCookie = 0;
        DWORD   dwError;
        dwError = 0;
        EDITSTREAMCALLBACK EditStreamCallback;
        EditStreamCallback = NULL ;
        EDITSTREAM es;  
        hFile = (HANDLE)dwCookie;   
    es.dwCookie    = (DWORD_PTR)hFile;
    es.dwError =    dwError;
    es.pfnCallback = EditStreamCallback;


        LPBYTE pbBuff;
        pbBuff = NULL;
        LONG cb;
        PLONG pcb;
        pcb = 0;
        DWORD   write;
        

         WriteFile(hFile, &pbBuff, write, (DWORD *)&pcb, NULL);//
    dwpfn=GetLastError();
          swprintf_s(dwj,100,L"%d",dwpfn);
                            MessageBox(hWnd, dwj
                                       ,
                                       L"Problem with write file",
                                                             MB_OK);
    EditStreamCallback( (DWORD_PTR)hFile ,pbBuff,cb,pcb);
    SendMessage(richedit, EM_STREAMOUT, SF_RTF, (LPARAM)&es);


        hFile = CreateFile(L"Again.rtf",GENERIC_WRITE,
                                  FILE_SHARE_WRITE, 0, CREATE_NEW,
                                  FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile=INVALID_HANDLE_VALUE)
        dw2=GetLastError();
          swprintf_s(dw3,100,L"%d",dw2);
                            MessageBox(hWnd, dw3
                                       ,
                                       L"INVALID_HANDLE_VALUE",
                                       MB_OK);                                                                   

                                                                         
    {
       HWND hWnd1;
       hWnd1 = CreateWindow(L"NEW CLASS", L"Window 2", NULL,
          0, 0, 980, 700, NULL, NULL, hInstance, NULL);
       dw2=GetLastError();
          swprintf_s(dw3,100,L"%d",dw2);
                            MessageBox(hWnd, dw3
                                       ,
                                       L"INVALID_HANDLE_VALUE",
                                       MB_OK);
       if (!hWnd1)
       {
          MessageBox(hWnd,  L"CreateWindow failed"
                                       ,
                                       L"Error CreateWindow",
                                       MB_OK);
       }

       ShowWindow(hWnd1, SW_SHOW);
       UpdateWindow(hWnd1);

       ShowWindow(hWnd, SW_HIDE);

       return TRUE;
    }
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }

    LRESULT CALLBACK WndProc1(HWND hWnd1, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
        HWND richedit1;
        DWORD dwe;
        WCHAR dwe5[100];
        DWORD dw4;
        WCHAR dw5[100];
        LPBYTE pbBuff1;
        LONG cb1;
        PLONG pcb1;
        DWORD_PTR dwCookie;
        DWORD   dwError;
        EDITSTREAMCALLBACK pfnCallback1;
        HRESULT hr;
        HWND hwndButtonb;
        switch (message)

        {
        case WM_CREATE:
            LoadLibrary(L"Msftedit.dll");
        richedit1 = CreateWindowEx(
            WS_EX_ACCEPTFILES | WS_EX_CLIENTEDGE, MSFTEDIT_CLASS,
        L"MSFTEDIT_CLASS",
        WS_CHILD | WS_VISIBLE |ES_AUTOHSCROLL | ES_LEFT| ES_SELECTIONBAR| ES_SUNKEN | ES_SAVESEL | ES_MULTILINE | ES_AUTOVSCROLL ,
        0,
        0,                  // starting x- and y-coordinates
        0,        // width of client area
        0,  
        hWnd1,
        NULL,    
        (HINSTANCE)GetWindowLong(hWnd1, GWL_HINSTANCE),
         NULL);

         SetWindowPos(
        richedit1,
        NULL,
        0,
        0,
        980,
        700,
        NULL);

    if (!richedit1)
        {dw4=GetLastError();
          swprintf_s(dw5,100,L"%d",dw4);
                            MessageBox(hWnd1, dw5
                                       ,
                                       L"Error richedit",
                                       MB_OK);
    }
       
             hwndButtonb = CreateWindow(
        L"BUTTON",  // Predefined class; Unicode assumed
        L"OK",      // Button text
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles
        380,         // x position
        265,         // y position
        25,        // Button width
        25,        // Button height
        richedit1,     // Parent window
        (HMENU) ID_BUTTONB,       // No menu.
        (HINSTANCE)GetWindowLong(richedit1, GWL_HINSTANCE),
        NULL);      // Pointer not needed.
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_OPEN:
        
           hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
            COINIT_DISABLE_OLE1DDE);
        if (SUCCEEDED(hr))
        {
            {
              MessageBox(hWnd1, L"com is registered"
                                       ,
                                       L"Error 2",
                                       MB_OK);
                        }    
            IFileOpenDialog *pFileOpen;
            hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
                    IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
            if (SUCCEEDED(hr))
            {
                hr = pFileOpen->Show(NULL);

                if (SUCCEEDED(hr))
                {
                    IShellItem *pItem;
                    hr = pFileOpen->GetResult(&pItem);
                    if (SUCCEEDED(hr))
                    {
                        PWSTR pszFilePath;
                        hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                        if (SUCCEEDED(hr))
                        {
                            MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
                            
    {
        HANDLE hFile1 = CreateFile(pszFilePath, GENERIC_READ,
                                  0, NULL, OPEN_EXISTING,
                                  FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hFile1 = INVALID_HANDLE_VALUE)
        {dwe=GetLastError();
          swprintf_s(dwe5,100,L"%d",dwe);
                            MessageBox(hWnd1, dwe5
                                       ,
                                       L"Error INVALID_HANDLE_VALUE",
                                       MB_OK);
      ReadFile(hFile1, &pbBuff1, sizeof(cb1), (DWORD *)&pcb1, NULL);
    }

    pcb1 = 0;
    cb1 = 100;
    dwCookie    = (DWORD_PTR)hFile1;
    pfnCallback1 = NULL ;
    dwError= 0;
    pfnCallback1(dwCookie,pbBuff1,cb1,pcb1);
    {
    EDITSTREAM es1 = { 0 };
    es1.pfnCallback = pfnCallback1;
    es1.dwCookie    = (DWORD_PTR)hFile1;
    es1.dwError =    dwError;


    if (SendMessage(richedit1, EM_STREAMIN, SF_RTF, (LPARAM)&es1) && es1.dwError == 0)
    CoTaskMemFree(pszFilePath);
            CloseHandle(hFile1);
        }
    }
                        }
                        pItem->Release();
                    }
                }
                pFileOpen->Release();
            }
            CoUninitialize();
        }


                break;
            default:
                return DefWindowProc(hWnd1, message, wParam, lParam);
            }
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd1, &ps);
            // TODO: Add any drawing code here...
            EndPaint(hWnd1, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd1, message, wParam, lParam);
        }
        return 0;
    }

    // Message handler for about box.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        UNREFERENCED_PARAMETER(lParam);
        switch (message)
        {
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;
            }
            break;
        }
        return (INT_PTR)FALSE;
    }

    Wednesday, October 15, 2014 9:46 AM

Answers

All replies