Thread: Read a Microsoft LNK file using C?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Read a Microsoft LNK file using C?

    Hi,
    Can anyone point me in the direction of how to identify the file a Windows *.lnk file refers to using C? I know I could use IShellLink in C++, but the program is C only. C++ is not an option.

    Thanks in advance.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, COM can be used with C too, IIRC. Don't have any links or tutorials, though.
    But a little googling for COM in C wouldn't be too hard to find, I think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
    #pragma comment( lib, "ole32.lib" ) 
    #include <windows.h>
    #include <stdio.h>
    #include <shobjidl.h>
    #include <objbase.h>
    #include <shlguid.h>
    
    HRESULT GetPath(HWND hwnd, char *pLinkFile, char *pReturnPath) 
    { 
        HRESULT hres; 
        IShellLink* psl; 
        char szPath[MAX_PATH] = {0}; 
        char szDescription[MAX_PATH] = {0}; 
        WIN32_FIND_DATA wfd; 
    
        hres = CoCreateInstance(CLSID_ShellLink, NULL, 
            CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); 
        if (SUCCEEDED(hres))
    	{ 
            IPersistFile* ppf; 
            hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 
            if (SUCCEEDED(hres))
    		{ 
                WCHAR wLinkFile[MAX_PATH]; 
                MultiByteToWideChar(CP_ACP, 0, pLinkFile, -1, wLinkFile, 
                    MAX_PATH); 
                hres = ppf->Load(wLinkFile, STGM_READ); 
                if (SUCCEEDED(hres))
    			{ 
                    hres = psl->Resolve(hwnd, 0); 
                    if (SUCCEEDED(hres))
    				{ 
                        hres = psl->GetPath(szPath, 
                            MAX_PATH, (WIN32_FIND_DATA *)&wfd, 
                            SLGP_SHORTPATH ); 
    						if (FAILED(hres)) 
                       		printf("GetPath failed\n");
                        hres = psl->GetDescription(szDescription, MAX_PATH); 
                        if (FAILED(hres)) 
                        printf("GetDescription failed\n");
                        strcpy(pReturnPath, szPath); 
                    } 
                } 
                ppf->Release(); 
            } 
            psl->Release(); 
        } 
        return hres; 
    }
    
    int main(void)
    {
        char szReturn[512] = {0};   
        CoInitialize(NULL); 
        GetPath(NULL, "c:\\Docume~1\\Alluse~1\\desktop\\Wireshark.lnk" , szReturn);
    	printf("%s\n", szReturn);
        CoUninitialize(); 
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Apps that act "differently" in XP SP2
    By Stan100 in forum Tech Board
    Replies: 6
    Last Post: 08-16-2004, 10:38 PM
  4. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM