Thread: Unknown problem

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    2

    Unknown problem

    Hello, this is a program that tries to get information from a bmp file, and i do know that there are API functions for loading bitmaps, but i just want to learn. The program is supposed to open a file in binary mode and i am confused how to do it with API functions, now it is trying to open the file with FILE_FLAG_NO_BUFFERING and then it allocates the buffer with VirtualAlloc, but some files aren't opened properly and it prints random things :---<
    Compiler: Dev-C++ 4.9.4.1/mingw32 2.95.3-6, this is done as a Win32 Console project

    Code:
    #include <stdio.h>
    #include <windows.h>
    typedef struct BMP { /* i have both BITMAPINFOHEADER and BITMAPCOREHEADER because i don't really know which one bitmaps actually use */
    BITMAPFILEHEADER bmfh;
    union u1 {
    BITMAPINFOHEADER bmih;
    BITMAPCOREHEADER bmch;
    }u1;
    BYTE* bmp_data; //palette and data
    };
    int getsectorcount(int x,char* str) { /*a function that returns number of sectors in file*sectorsize */
    DWORD y[4];
    GetDiskFreeSpace(str,&y[0],&y[1],&y[2],&y[3]);
    y[0]=x%y[1];
    if(y[0]!=0)x+=y[1]-y[0];
    return x;
    }
    int main(int narg, char** arg) {
    DWORD i,*x,y;
    HANDLE f1,f2;
    BMP* b1;
    char fname[256],**temp;
    GetFullPathName(arg[1],256,fname,temp);
    f1=CreateFile(fname,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_NO_BUFFERING,NULL);
    if(f1==INVALID_HANDLE_VALUE) { printf("Unable to open file \"%s\"",arg[1]); return -1; };
    i=GetFileSize(f1,NULL);
    fname[3]='\0';
    printf("file size(bytes): %d\nfile size(sectors): %d\n",i,getsectorcount(i,fname));
    x=(DWORD*)VirtualAlloc(NULL,i,MEM_COMMIT,PAGE_READWRITE);
    ReadFile(f1,x,getsectorcount(i,fname),&y,NULL);
    for(y=0;y<i;printf("%02x ",((BYTE*)x)[y++]))if(y%26==0)printf("\n");
    b1=(BMP*)x;
    printf("\nBitmapfileheader data:\nimage type: %x\nfile size: %d\nreserved value 1: %d\nreserved value 2: %d\nimage data offset: %d\n",b1->bmfh.bfType,b1->bmfh.bfSize);
    printf("Bitmapinfoheader data:\nstructure size: %d\nimage height: %d\nimage width: %d\n",b1->u1.bmih.biSize,b1->u1.bmih.biHeight,b1->u1.bmih.biWidth);
    printf("Bitmapcoreheader data:\nstructure size: %d\nimage height: %d\nimage width: %d\n",b1->u1.bmch.bcSize,b1->u1.bmch.bcHeight,b1->u1.bmch.bcWidth);
    VirtualFree(x,i,MEM_DECOMMIT);
    CloseHandle(f1);
    return 0;
    }
    Here is an example of running the program:
    >mircgfx.exe test.bmp
    file size(bytes): 150
    file size(sectors): 512

    42 4d 96 00 00 00 00 00 00 00 76 00 00 00 28 00 00 00 08 00 00 00 08 00 00 00
    01 00 04 00 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 80 00 00 80 00 00 00 80 80 00 80 00 00 00 80 00 80 00
    80 80 00 00 80 80 80 00 c0 c0 c0 00 00 00 ff 00 00 ff 00 00 00 ff ff 00 ff 00
    00 00 ff 00 ff 00 ff ff 00 00 ff ff ff 00 dd dd dd dd dd cc cc dd dc dc cd cd
    dc dd dc cd dc cd dc cd dc dc cd cd dd cc cc dd dd dd dd dd
    Bitmapfileheader data:
    image type: 4d42
    file size: 150
    reserved value 1: 0
    reserved value 2: 2147348480
    image data offset: 0
    Bitmapinfoheader data:
    structure size: 524288
    image height: 65536
    image width: 524288
    Bitmapcoreheader data:
    structure size: 524288
    image height: 8
    image width: 0
    test.bmp is a 8x8 16 color bitmap.
    Last edited by pink_langouste; 12-03-2002 at 10:57 PM.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    1
    int getsectorcount(int x,char* str) { /*a


    > I think you use to have to enter a value , ie. 0-1000 for the order to list in subsequent order. not sure, but there is a command to order progressive and random

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    2
    problem solved :--->

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Unknown problem
    By Mindphuck in forum C Programming
    Replies: 3
    Last Post: 04-08-2006, 02:35 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM