Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to use the function ShellExecuteW to open a PDF file using the Win32 API in C++

2 Answers

0 votes
#include <windows.h>

int main() {
    ShellExecuteW(
        nullptr,            // No parent window
        L"open",            
        L"C:\\Users\\USER_NAME\\A_Directory\\letter_0093457341801.pdf",
        nullptr,            // Parameters
        nullptr,            // Working directory
        SW_SHOWNORMAL       // Window display option
    );
}



/*
run:



*/

 



answered Jan 24 by avibootz
0 votes
#include <windows.h>
#include <iostream> // wcerr

int wmain(int argc, wchar_t* argv[]) {
    // Path to the PDF file (must be a wide string)
    // You can use an absolute path or a relative path
    LPCWSTR pdfPath = L"C:\\Users\\USER_NAME\\A_Directory\\letter_0093457341801.pdf";

    // Check if the file exists before trying to open it
    DWORD fileAttr = GetFileAttributesW(pdfPath);
    if (fileAttr == INVALID_FILE_ATTRIBUTES || (fileAttr & FILE_ATTRIBUTE_DIRECTORY)) {
        std::wcerr << L"Error: File not found: " << pdfPath << std::endl;
        return 1;
    }

    // Open the PDF file with the default associated application
    HINSTANCE result = ShellExecuteW(
        nullptr,      // No parent window
        L"open",      // Operation: "open", "print", etc.
        pdfPath,      // File to open
        nullptr,      // Parameters (not needed for PDF)
        nullptr,      // Default directory
        SW_SHOWNORMAL // Show window normally
    );

    // ShellExecuteW returns a value > 32 if successful
    if ((INT_PTR)result <= 32) {
        std::wcerr << L"Failed to open PDF. Error code: " << (INT_PTR)result << std::endl;
        return 1;
    }
}




/*
run:



*/

 



answered Jan 24 by avibootz
edited Jan 24 by avibootz

Related questions

...