BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Full Debug vs Release

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students)
View previous topic :: View next topic  
Author Message
DebugRelease
Guest





PostPosted: Tue Apr 10, 2007 8:11 am    Post subject: Full Debug vs Release Reply with quote



Hi,

I have the next code:

bool __fastcall TForm1::OpenCloseTray(bool bOpen, TCHAR cDrive)
{
// Open the device (drive) that we want to affect
AnsiString cs;

wsprintf( cs.c_str(), "\\\\.\\%c:", cDrive);

HANDLE hDrive = CreateFile(cs.c_str(), GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

// Make sure the device was found and opened successfully
if(hDrive == INVALID_HANDLE_VALUE || GetLastError() != NO_ERROR)
return false;

BOOL bStatus; // Let the caller know if it worked or not
DWORD dwDummy; // We don't really need this info

if(bOpen) // Open the tray
bStatus = DeviceIoControl(hDrive, IOCTL_STORAGE_EJECT_MEDIA, NULL,
NULL, NULL, 0, &dwDummy, NULL);
else // Close the tray
bStatus = DeviceIoControl(hDrive, IOCTL_STORAGE_LOAD_MEDIA,
NULL, NULL, NULL, 0, &dwDummy, NULL);
CloseHandle(hDrive);
return bStatus?true:false;
}

this function works great when its compiled with 'Full Debug' option, but it doesnt work when its compiled with 'Release' option. Can you tell me why and how can resolve this problem?

Thanks Smile
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Apr 10, 2007 8:11 am    Post subject: Re: Full Debug vs Release Reply with quote



"DebugRelease" <dr (AT) spamme (DOT) com> wrote in message
news:461b07a8$1 (AT) newsgroups (DOT) borland.com...

Quote:
AnsiString cs;

wsprintf( cs.c_str(), "\\\\.\\%c:", cDrive);

That is very dangerous. You haven't allocated any memory for the
AnsiString. You are trying to format a string into a buffer that does
not exist. I suggest you use a fixed-length buffer instead. There is
no need for a dynamic buffer in this situation. Also, you are using
TCHAR for the input, which is either 'char' or 'wchar_t' depending on
whether UNICODE is defined, but you are not using TCHAR for the
formatted output at all. So if you ever do try to compile the code
with UNICODE enabled, it is going to fail to compile.

Use this instead:

bool __fastcall TForm1::OpenCloseTray(bool bOpen, TCHAR cDrive)
{
TCHAR cs[8];
wsprintf(cs, TEXT("\\\\.\\%c:"), cDrive);

// Or simply:
// LPTSTR cs = TEXT("\\\\.\\%c:");
// cs[4] = cDrive;

HANDLE hDrive = CreateFile(cs, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

Quote:
this function works great when its compiled with 'Full Debug'
option,
but it doesnt work when its compiled with 'Release' option.

You need to be more specific. What exactly is not working for you?
Other than the obvious issue with misusing memory buffers, that is.


Gambit
Back to top
DebugRelease
Guest





PostPosted: Tue Apr 10, 2007 9:29 pm    Post subject: Re: Full Debug vs Release Reply with quote



Thanks, it was the big problem!!! Great lesson!!!

But How can I turn on the UNICODE option in the IDE? And I dont understand why when I running the program in debug mode it works ( I mean it open the CD Drive ) but when I run the program in release mode it doesnt work ( it doesnt open the CD Drive ) ?

Thanks!!!

"Remy Lebeau \(TeamB\)" <no.spam (AT) no (DOT) spam.com> wrote:
Quote:

"DebugRelease" <dr (AT) spamme (DOT) com> wrote in message
news:461b07a8$1 (AT) newsgroups (DOT) borland.com...

AnsiString cs;

wsprintf( cs.c_str(), "\\\\.\\%c:", cDrive);

That is very dangerous. You haven't allocated any memory for the
AnsiString. You are trying to format a string into a buffer that does
not exist. I suggest you use a fixed-length buffer instead. There is
no need for a dynamic buffer in this situation. Also, you are using
TCHAR for the input, which is either 'char' or 'wchar_t' depending on
whether UNICODE is defined, but you are not using TCHAR for the
formatted output at all. So if you ever do try to compile the code
with UNICODE enabled, it is going to fail to compile.

Use this instead:

bool __fastcall TForm1::OpenCloseTray(bool bOpen, TCHAR cDrive)
{
TCHAR cs[8];
wsprintf(cs, TEXT("\\\\.\\%c:"), cDrive);

// Or simply:
// LPTSTR cs = TEXT("\\\\.\\%c:");
// cs[4] = cDrive;

HANDLE hDrive = CreateFile(cs, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

this function works great when its compiled with 'Full Debug'
option,
but it doesnt work when its compiled with 'Release' option.

You need to be more specific. What exactly is not working for you?
Other than the obvious issue with misusing memory buffers, that is.


Gambit

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Apr 10, 2007 10:08 pm    Post subject: Re: Full Debug vs Release Reply with quote

"DebugRelease" <dr (AT) spamme (DOT) com> wrote in message
news:461bad56$1 (AT) newsgroups (DOT) borland.com...

Quote:
But How can I turn on the UNICODE option in the IDE?

Simply add UNICODE to the Conditionals list in the Project Options.

Do note, though, that the VCL is largely not Unicode-capable, though.
So be careful if/when you decide to turn on that switch.

Quote:
And I dont understand why when I running the program in
debug mode it works

It doesn't.

Quote:
I mean it open the CD Drive

It can't be, using the code you showed earlier. That code was not
formatting the drive letter correctly in the first place, so
CreateFile(), if not even wsprintf(), would always fail before you
could get that far.


Gambit
Back to top
DebugRelease
Guest





PostPosted: Wed Apr 11, 2007 12:07 am    Post subject: Re: Full Debug vs Release Reply with quote

Thanks for all your help, but I swear it compiles and open the CD Drive when I turn on the Full Debug (I mean I just push the "Full Debug" button), but when I turn on the Release option (the "Release" button) it compiles but it doesnt work, it just doesnt open or close the CD Drive, so I just I got confused...

Thanks again!!! :D

"Remy Lebeau \(TeamB\)" <no.spam (AT) no (DOT) spam.com> wrote:
Quote:

"DebugRelease" <dr (AT) spamme (DOT) com> wrote in message
news:461bad56$1 (AT) newsgroups (DOT) borland.com...

But How can I turn on the UNICODE option in the IDE?

Simply add UNICODE to the Conditionals list in the Project Options.

Do note, though, that the VCL is largely not Unicode-capable, though.
So be careful if/when you decide to turn on that switch.

And I dont understand why when I running the program in
debug mode it works

It doesn't.

I mean it open the CD Drive

It can't be, using the code you showed earlier. That code was not
formatting the drive letter correctly in the first place, so
CreateFile(), if not even wsprintf(), would always fail before you
could get that far.


Gambit

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students) All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.