Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

NPAPI plugin fopen failing

Hi Experts,

I've got an NPAPI plugin that calls the following:

FILE * f = fopen("c:\\test.txt", "r");

I'm getting a bad pointer.  Is there something wrong with calling fopen from a plugin??

Thanks,
Mike
0
thready
Asked:
thready
3 Solutions
 
jkrCommented:
Technically not. But, you might hit a permission problem with files stored in the root directory of the system drive. Furthermore, checking the return value is always a good idea, since 'fopen()' (http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx) returns NULL when it encouters a problem. E.g.

FILE * f = fopen("c:\\test.txt", "r");

if (!f) {

  // check error code in 'errno' (http://msdn.microsoft.com/en-us/library/t3ayayh1.aspx)
}

Open in new window

0
 
sarabandeCommented:
to add to before comment:


I'm getting a bad pointer
as explained by jkr the return value is NULL in case of failure. the debugger may name that a 'bad pointer' but actually a NULL pointer is not 'bad' itself but only if you expect a valid pointer to be returned by a function. a non-zero pointer should always be a valid file pointer.

the errno can be checked by including <errno.h> and code like

char szerr[1024] = { '\0' };
if (f == NULL)
{
    switch (errno)
    {
            case 2: strcpy(szerr, "file not found"); break;
            case 3: strcpy(szerr, "path not found"); break;
            case 4: strcpy(szerr, "too many open files"); break;
            case 5: strcpy(szerr, "access denied"); break;

Open in new window


a complete list of all error codes could be found in winerror.h but the 4 error codes handled in the sample are the most likely ones after fopen.

Sara
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
Including to the above comments....
We can use ::GetLastError() win32 API to get the error code.
0
 
threadyAuthor Commented:
Thanks everyone
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now