CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Problem with empty/null string/char...

  1. #1
    Join Date
    May 2009
    Posts
    29

    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

  2. #2
    Join Date
    Jul 2013
    Posts
    576

    Re: Problem with empty/null string/char...

    Quote Originally Posted by jkepler View Post
    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.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,717

    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);

  4. #4
    Join Date
    May 2009
    Posts
    29

    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

  5. #5
    Join Date
    Mar 2001
    Posts
    2,527

    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.

  6. #6
    Join Date
    May 2009
    Posts
    29

    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

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,759

    Re: Problem with empty/null string/char...

    Quote Originally Posted by jkepler View Post
    ...
    "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

  8. #8
    Join Date
    May 2009
    Posts
    29

    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

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    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.

  10. #10
    Join Date
    May 2009
    Posts
    29

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)