Solved

NPAPI plugin fopen failing

Posted on 2014-02-11
4
408 Views
Last Modified: 2014-02-16
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
Comment
Question by:thready
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 167 total points
ID: 39851826
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
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 167 total points
ID: 39853395
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
 
LVL 8

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 166 total points
ID: 39855088
Including to the above comments....
We can use ::GetLastError() win32 API to get the error code.
0
 
LVL 1

Author Closing Comment

by:thready
ID: 39863537
Thanks everyone
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now