-
May 17th, 2015, 08:34 PM
#1
Problem with empty/null string/char...
Good evening,
I'm making a DLL in Visual C++ 6.
And calling it from a VB6 program.
The problem, is that one of the routines (in VC++ 6) is of the type:
short int __stdcall file_open (char *file_name)
Everything goes right - even if the file doesn't exist. I've placed a line to insure the file is opened or not.
The problem is when I send an empty or null string to the DLL. VB6 crashes...
I've tryed every possible ways I could think of to deal with this problem - comparing file_name to "", "\0", NULL, etc in my C++ code...
I'm missing something here.
Can someone please help me out?
Kind regards,
JKepler
-
May 17th, 2015, 10:55 PM
#2
Re: Problem with empty/null string/char...
Originally Posted by jkepler
I'm missing something here.
Only you know since you're not telling how you compare.
Have you printed what file_name points at so you know it is '\0' indeed?
Are you sure you are comparing what file_name points at and not the pointer itself?
Try using strcmp() if you aren't already.
-
May 18th, 2015, 05:44 AM
#3
Re: Problem with empty/null string/char...
Just a note: many c-functions that take a char * have undefined behavior if you send a null. Example:
Code:
char * p =0;
int n = strlen(p);
-
May 18th, 2015, 06:05 AM
#4
Re: Problem with empty/null string/char...
Hi,
Thank you all for the help.
I've found out the following: the function was defined by short int __stdcall
I've looked at the results of the return, and they were weird. So I've changed it to int __stdcall (no short).
The result is, that if I place the following:
if (!file_name)
{
return 1;
}
it works perfectly. With short, gives a tremendous error.
Kind regards,
JKepler
-
May 18th, 2015, 10:42 AM
#5
Re: Problem with empty/null string/char...
You should check explicitly for
Code:
if (file_name==NULL)
{
//take some action
}
ahoodin
To keep the plot moving, that's why.
-
May 18th, 2015, 06:20 PM
#6
Re: Problem with empty/null string/char...
Good evening,
Sorry for the late reply ahhodin.
I've done as you told. Everything is running smoothly
Meanwhile, I had a similar problem with a general routine for closing the same file.
"short int" is definetly not the answer - it messes up everything in major calls...
Either way, thank you very much for all the help.
Kind regards,
JKepler
-
May 19th, 2015, 07:42 AM
#7
Re: Problem with empty/null string/char...
Originally Posted by jkepler
...
"short int" is definetly not the answer - it messes up everything in major calls...
BTW, what value is your function short int __stdcall file_open (char *file_name) supposed to return?
Where does this value come from?
Victor Nijegorodov
-
May 19th, 2015, 08:05 AM
#8
Re: Problem with empty/null string/char...
Hi,
0 - if it opens the file sucessfully (file exists and opens succefully)
1 - if it fails (corrupted file or doesn't exist)
if (file_name == NULL)
{
return 1;
}
if ((BUFFER = fopen (file_name, "rb")) == NULL)
{
return 1;
}
else
{
// continue to deal with sucess or more troubles... :/
return 0; // all went well
Cheers,
JKepler
-
May 19th, 2015, 12:46 PM
#9
Re: Problem with empty/null string/char...
"short int" is definetly not the answer - it messes up everything in major calls...
Don't return a short int. Return a LONG. You should be returning and passing Windows API types, so that you don't get into the trouble where
Code:
sizeof(Windows Type) != sizeof( C++ type )
The Windows API types are universally accepted by the various languages that purport to be able to call external DLL functions, so you should be using them and not explicit C++ types. Probably you are having problems because the integer type that you coded your VB6 function to accept is not the same size in bytes as "short int", causing a return value issue with the stack.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 19th, 2015 at 12:53 PM.
-
May 19th, 2015, 12:48 PM
#10
Re: Problem with empty/null string/char...
Thanks Paul I've realized that in the worst way.
Kind regards,
JKepler
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|