View unanswered posts | View active topics It is currently Mon Apr 21, 2014 8:09 am



Reply to topic  [ 12 posts ] 
 Problems with CreateFile 
Author Message

Joined: Tue Nov 04, 2003 8:01 am
Posts: 10
Location: TN
Post Problems with CreateFile
I'm making a simple program that loads a DIB and displays it on the screen using CreateDIBSection and Bitblt. I wanted to practice file I/O so I decided to make my own bitmap loading function. Anyway, it didn't work. Thanks to the wonders of error handling (never again will I forsake you) I traced the problem back to the CreateFile function call. Here's the short version of my code.

char *blankbm = "test.bmp";

BITMAPFILEHEADER *BitmapLoader(HWND hwnd,char *strg)
{
....

bmhnd = CreateFile(strg , GENERIC_READ, NULL, NULL, OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, NULL);

....
}

As you can see I tried to hard code the filename into the program (I wanted to keep it simple for the time being). I think this might be where things went bad. Unfortunately, I'm not sure how to fix it. Any takers?


Thu Dec 04, 2003 11:21 pm
Profile

Joined: Fri Oct 10, 2003 7:01 am
Posts: 3
Post Problems with CreateFile
Maybe you want to change it to:

char blankbm[] = "test.bmp";


Fri Dec 05, 2003 4:26 am
Profile

Joined: Sat Apr 21, 2001 7:01 am
Posts: 53
Location: Lexington, MA, USA
Post Problems with CreateFile
That char * is perfectly fine. I don't know what's not working, but try adding a SetLastError(0); call before CreateFile() and then call GetLastError(); after CreateFile(). See this page for a list of error codes.


Fri Dec 05, 2003 5:17 pm
Profile ICQ

Joined: Wed Mar 29, 2000 8:01 am
Posts: 2
Post Problems with CreateFile
Don't know if this will be of any help, but here are my $.02.

I'm assuming that somewhere in your BitmapLoader() function you are strg equal to blankbm.

Is test.bmp in the same directory as the .exe? I ask because I ran into a problem once running the .exe outside of the debugger (in Visual C/C++). The executable was in the debug directory and the datafile was in the project folder. If I'm not mistaken the debugger corrects for this and will find the datafile, but a standalone execution won't.

Other than this, I'm drawing a blank. Is bmhnd equal to INVALID_HANDLE_VALUE? What is GetLastError() returning?

Hope this was of some help.
Tony


Sun Dec 07, 2003 2:41 pm
Profile

Joined: Tue Nov 04, 2003 8:01 am
Posts: 10
Location: TN
Post Problems with CreateFile
Figured out the problem. It was my fault. LOL I forgot the function was called twice.(doh!) But now there is a new problem. Whenever I try to run the program I get these windows that popup saying that there was a problem running the program and windows wants to send Microshlong some kind of error report. [img]images/smiles/icon_sad.gif[/img] Not good. Everything compiles O.K., and the only real warnings I get, other than the standard ones telling you that prevInstance and szCmdLine are unused, is one about a suspicious pointer conversion. And I know it's cool because I swipped it from Charles Petzold's book. So do any of you know of any comon programming mistakes that cause this to happen? (I probably did something stupid again, oh well.)


Tue Dec 09, 2003 10:28 pm
Profile
Site Admin
User avatar

Joined: Fri Aug 08, 2003 7:01 am
Posts: 20
Location: Utah, USA
Post Problems with CreateFile
Those windows error pop ups could happen anywhere for a number of reasons. I suggest debuging your code while running through every line in your program and see what line the error occurs. Some times the error might not be on that line especially if you overwrote some arrays and screwed up the stack.

I've had that happen before and it never caused a problem until the next time I malloc'd some more memory and it told my program that windows had run out of memory! Heheh


Wed Dec 10, 2003 12:06 am
Profile

Joined: Sat Apr 21, 2001 7:01 am
Posts: 53
Location: Lexington, MA, USA
Post Problems with CreateFile
You're probably trashing some memory somewhere. Make sure you're not going out of bounds on an array or freeing memory twice.


Wed Dec 10, 2003 12:27 am
Profile ICQ

Joined: Tue Nov 04, 2003 8:01 am
Posts: 10
Location: TN
Post Problems with CreateFile
I decided to break down and learn to use some debugging tools. I just downloaded borland's old turbo debugger from their website. Do any of you know of any other "free" debuggers that you would recomend?


Sat Dec 13, 2003 4:09 am
Profile

Joined: Thu Sep 12, 2002 7:01 am
Posts: 10
Location: Clovis, CA
Post Problems with CreateFile
Personally, I would suggest checking into remote debugging or dual monitor debugging.


Sun Dec 14, 2003 3:54 am
Profile WWW

Joined: Tue Nov 04, 2003 8:01 am
Posts: 10
Location: TN
Post Problems with CreateFile
Borland's turbo debugger is way out dated. It thinks the windows API is a bug! LOL Plus I can't make heads or tales of what it's trying to tell me. What's a good debugger for a beginner to use?


Mon Dec 15, 2003 3:22 am
Profile

Joined: Thu Sep 12, 2002 7:01 am
Posts: 10
Location: Clovis, CA
Post Problems with CreateFile
It's probably a good idea to learn the debugger that comes with whatever developement studio that you are using. It may take you a while to learn what all of the common error codes mean, but it will save you time in the long run. Plus you can always research your particular error message online if you need to...and reference manuals are your best friend.


Tue Dec 16, 2003 12:01 am
Profile WWW

Joined: Tue Nov 04, 2003 8:01 am
Posts: 10
Location: TN
Post Problems with CreateFile
Finally!! I've found the problem. (I think [img]images/smiles/icon_razz.gif[/img] ) I downloaded the latest version of DEV c++, which is an awsome setup by the way, and used their debugger to help me locate the culprit.

hbm = CreateDIBSection(cdc,((BITMAPINFO *)pbmfh1++),DIB_RGB_COLORS,pbits,NULL,0);

As you can see I'm trying to create a dib section. I'm just using standard 24 bit bitmaps for the time being. To get the bitmap info header the function calls for I increment the pointer to a file header, esentially jumping over the bitmap file header structure, and then typcasting it as an info header. I'm guessing this may be the culprit, but I'm not sure. What do you guys think? Also, the other two error report messages were in my CreateFile function. I forgot my FILE_SHARE_READ flag. (doh!) Anyway, thanks for all the help. I feel a little embarassed spending so much time on such a simple program, but I've learned a ton from it. Now, if I can just get it to work, I'll have it made (yeah right).


Tue Dec 16, 2003 1:14 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.