CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Printing to Default Printer

    Something like:
    Code:
    for (int i=0; i<lpEvent->nData; ++i)
    {
        cout << "char " << i << " = " << lpEvent->lpData[i]
                << " = hex = " << hex << (int)lpEvent->lpData[i] << endl;
    }
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  2. #17
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    Works perfect!

    I coudn't get the cout to work, but I did it with fprintf.

    Thanks!

  3. #18
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Printing to Default Printer

    What was the problem with cout?
    Did you include <iostream>?
    Did you put "using namespace std;" at the top of the CPP file?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #19
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    I am making progress with printing to a file, but have run into a problem that I don't understand.

    I am trying to read a MIDI file and print out the contents.

    The code below works for standard type 0 MIDI files (these have only 1 track).

    If I use the same code, but open a type 1 MIDI file (these have more than 1 track), the program crashes.

    Can someone spot my error?

    Code:
    #include <windows.h>
    
    #pragma comment (lib, "winmm.lib")
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct EVENT {
    	BYTE   state;   
    	BYTE   data1;   
    	BYTE   data2;   
    	BYTE   type;    
    	int    nData;   
    	LPBYTE lpData;  
    	DWORD  dwDelta; 
    
    	struct EVENT *lpNext; 
    };
    typedef struct EVENT EVENT;
    typedef struct EVENT *LPEVENT;
    
    HANDLE g_hheap = NULL;
    
    BOOL ReadMidiFile(LPTSTR lpszFileName);
    BOOL ReadTrack(HMMIO hmmio, LPEVENT *lplpEvent);
    void ReadAndReverse(HMMIO hmmio, LPVOID lpData, DWORD dwSize);
    void ReadDelta(HMMIO hmmio, LPDWORD lpdwDelta);
    LPVOID Alloc(DWORD dwSize);
    BOOL PrintOut(LPEVENT lpEvent);
    
    int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
    {
    	g_hheap = HeapCreate(0, 4096, 0);
    	if (g_hheap == NULL)
    		return 0;
    	
    	if (!ReadMidiFile(TEXT("SMFtype0.mid")))
    	//if (!ReadMidiFile(TEXT("SMFtype1.mid")))
    
    		MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
    
    	HeapDestroy(g_hheap);
    
    	return 0;
    }
    
    BOOL ReadMidiFile(LPTSTR lpszFileName)
    {
    	HMMIO   hmmio;
    	WORD    i;
    	WORD    wTrack;
    	WORD    wFormat;
    	WORD    wTime;
    	DWORD   dwMagic;
    	DWORD   dwDataLen;
    	LPEVENT *lplpEvent; 
    
    	hmmio = mmioOpen(lpszFileName, NULL, MMIO_READ);
    	if (hmmio == NULL) {
    		MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
    		return FALSE;
    	}
    
    	mmioRead(hmmio, (HPSTR)&dwMagic, sizeof(DWORD));
    	if (dwMagic != *(LPDWORD)"MThd") {
    		mmioClose(hmmio, 0);
    		return FALSE;
    	}
    
    	ReadAndReverse(hmmio, &dwDataLen, sizeof(DWORD));
    	if (dwDataLen != 6) {
    		mmioClose(hmmio, 0);
    		return FALSE;
    	}
    
    	ReadAndReverse(hmmio, &wFormat, sizeof(WORD));
    	ReadAndReverse(hmmio, &wTrack, sizeof(WORD));
    	ReadAndReverse(hmmio, &wTime, sizeof(WORD));
    	
    	lplpEvent = (LPEVENT *)Alloc(sizeof(DWORD) * wTrack);
    	
    	for (i = 0; i < wTrack; i++) {
    		if (!ReadTrack(hmmio, &lplpEvent[i])) {
    			MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
    			mmioClose(hmmio, 0);
    			return FALSE;
    		}
    	}
    
    	MessageBox(NULL, TEXT("ALL MIDI tracks read"), TEXT("OK"), MB_OK);
    
    	mmioClose(hmmio, 0);
    	
    	return TRUE;
    }
    
    BOOL ReadTrack(HMMIO hmmio, LPEVENT *lplpEvent)
    {
    	BYTE    statePrev = 0; 
    	DWORD   dwLen;
    	DWORD   dwMagic;
    	LPEVENT lpEvent;
    
    	
    	mmioRead(hmmio, (HPSTR)&dwMagic, sizeof(DWORD));
    	if (dwMagic != *(LPDWORD)"MTrk")
    		return FALSE;
    	
    	ReadAndReverse(hmmio, &dwLen, sizeof(DWORD));
    
    	lpEvent = (LPEVENT)Alloc(sizeof(EVENT)); 
    
    	*lplpEvent = lpEvent; 
    	
    	for (;;) {
    		ReadDelta(hmmio, &lpEvent->dwDelta); 
    		
    		mmioRead(hmmio, (HPSTR)&lpEvent->state, sizeof(BYTE)); 
    		if (!(lpEvent->state & 0x80)) { 
    			lpEvent->state = statePrev; 
    			mmioSeek(hmmio, -1, SEEK_CUR); 
    		}
    		
    		switch (lpEvent->state & 0xF0) { 
    
    		case 0x80:
    		case 0x90:
    		case 0xA0:
    		case 0xB0:
    		case 0xE0:
    			mmioRead(hmmio, (HPSTR)&lpEvent->data1, sizeof(BYTE));
    			mmioRead(hmmio, (HPSTR)&lpEvent->data2, sizeof(BYTE));
    			break;
    		case 0xC0:
    		case 0xD0:
    			mmioRead(hmmio, (HPSTR)&lpEvent->data1, sizeof(BYTE));
    			lpEvent->data2 = 0;
    			break;
    		
    		case 0xF0:
    			if (lpEvent->state == 0xF0) { 
    				mmioRead(hmmio, (HPSTR)&lpEvent->nData, sizeof(BYTE));
    
    				lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData + 1); 
    				lpEvent->lpData[0] = lpEvent->state; 
    				mmioRead(hmmio, (HPSTR)(lpEvent->lpData + 1), lpEvent->nData);
    
    				lpEvent->nData++;
    			}
    			else if (lpEvent->state == 0xFF) { 
    				DWORD dw;
    				DWORD tmp;
    				
    				mmioRead(hmmio, (HPSTR)&lpEvent->type, sizeof(BYTE)); 
    
    				dw = (DWORD)-1;
    
    				switch (lpEvent->type) {
    
    				case 0x00: dw = 2; break;
    				case 0x01:
    				case 0x02:
    				case 0x03:
    				case 0x04:
    				case 0x05:
    				case 0x06:
    				case 0x07: break;
    				case 0x20: dw = 1; break; 
    				case 0x21: dw = 1; break; 
    				case 0x2F: dw = 0; break; 
    				case 0x51: dw = 3; break;
    				case 0x54: dw = 5; break;
    				case 0x58: dw = 4; break;
    				case 0x59: dw = 2; break;
    				case 0x7F: break;
    
    				default:
    					MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
    					return FALSE;
    
    				}
    				
    				tmp = dw;
    
    				if (dw != -1) {
    					ReadDelta(hmmio, &dw);
    					if (dw != tmp) {
    						MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
    						return FALSE;
    					}
    				}
    				else 
    					ReadDelta(hmmio, &dw); 
    
    				lpEvent->nData  = dw;
    				lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData);
    				mmioRead(hmmio, (HPSTR)lpEvent->lpData, lpEvent->nData); 
    				
    				if (lpEvent->type == 0x2F) 
    					return TRUE;
    			}
    			else
    				;
    
    			break;
    
    		default:
    			MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
    			return FALSE;
    
    		}
    
    
    
    		PrintOut((LPEVENT)lpEvent);
    
    		
    		statePrev = lpEvent->state; 
    		
    		lpEvent->lpNext = (LPEVENT)Alloc(sizeof(EVENT)); 
    		lpEvent = lpEvent->lpNext;
    		if (lpEvent == NULL)
    			break;
    
    	}
    
    	return FALSE;
    }
    
    void ReadAndReverse(HMMIO hmmio, LPVOID lpData, DWORD dwSize)
    {
    	BYTE   i;
    	BYTE   tmp;
    	LPBYTE lp = (LPBYTE)lpData;
    	LPBYTE lpTail = lp + dwSize - 1;
    
    	mmioRead(hmmio, (HPSTR)lp, dwSize);
    	
    	for (i = 0; i < dwSize / 2; i++) {
    		tmp = *lp;
    		*lp = *lpTail;
    		*lpTail = tmp;
    
    		lp++;
    		lpTail--;
    	}
    }
    
    void ReadDelta(HMMIO hmmio, LPDWORD lpdwDelta)
    {
    	int  i;
    	BYTE tmp;
    	
    	*lpdwDelta = 0;
    
    	for (i = 0; i < sizeof(DWORD); i++) {
    		mmioRead(hmmio, (HPSTR)&tmp, sizeof(BYTE));
    
    		*lpdwDelta = ( (*lpdwDelta) << 7 ) | (tmp & 0x7F);
    
    		if (!(tmp & 0x80)) 
    			break;
    	}
    }
    
    LPVOID Alloc(DWORD dwSize)
    {
    	return HeapAlloc(g_hheap, HEAP_ZERO_MEMORY, dwSize);
    }
    
    BOOL PrintOut(LPEVENT lpEvent)
    {
    	FILE *fp;
    	const char *str = "--------";
    
    	fp = fopen("printout.txt", "a");
    
    	fprintf(fp,"%s\n", str);
    	fprintf(fp,"lpEvent->dwDelta = %d\n", lpEvent->dwDelta);						
    	fprintf(fp,"lpEvent->state   = %X\n", lpEvent->state);						
    	fprintf(fp,"lpEvent->type    = %X\n", lpEvent->type);						
    	fprintf(fp,"lpEvent->data1   = %X\n", lpEvent->data1);
    	fprintf(fp,"lpEvent->data2   = %X\n", lpEvent->data2);
    	fprintf(fp,"lpEvent->nData   = %d\n", lpEvent->nData);
    	fprintf(fp,"lpEvent->lpData  = <%s>\n", lpEvent->lpData);
    	
    	if (lpEvent->lpData[0] == 0xF0)
    	{
    		for (int i=0; i<lpEvent->nData; ++i)
    		{
    			fprintf(fp,"    char  = <%X>\n", (int)lpEvent->lpData[i]);
    		}
    	}
    
    	return TRUE;
    }

  5. #20
    Join Date
    Apr 2004
    Posts
    204

    Re: Printing to Default Printer

    I should add, the program operates properly for both type 0 and type 1 MIDI files,
    it is just my printing code that causes problems.

  6. #21
    Join Date
    Aug 2010
    Posts
    51

    Re: Printing to Default Printer

    hi,


    we should add, the program operates properly for both type 0 and type 1 MIDI files,
    it is just my printing code that causes problems.

    regards,
    phe9oxis,
    http://www.guidebuddha.com

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured