/*

CLASS {NATIVE_Shortcut_info|_CSharp},
NATIVE:

*/

//#include <stdafx.h>
#include <windows.h>

#include <objbase.h>
#include <shlobj.h>

//#include <shobjidl.h>
//#include <shlguid.h>
#include <strsafe.h>

std::string AL4_shortcut::Resolve_link(/*in*/ LPCTSTR lpszLinkFile)
{
    TCHAR lpszPath[MAX_PATH];
    int iPathBufferSize = MAX_PATH;
	*lpszPath = 0;

	// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
	// has already been called.
	IShellLink* psl = NULL;
	HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
	if (SUCCEEDED(hres))
	{
		// Get a pointer to the IPersistFile interface.
		IPersistFile* ppf = NULL;
		hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
		if (SUCCEEDED(hres))
		{
			// Add code here to check return value from MultiByteWideChar
			// for success.

			// Load the shortcut.
#ifdef _UNICODE
			hres = ppf->Load(lpszLinkFile, STGM_READ);
#else
			WCHAR wsz[MAX_PATH] = {0};
			// Ensure that the string is Unicode.
			MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH);
			hres = ppf->Load(wsz, STGM_READ);
#endif

			if (SUCCEEDED(hres))
			{
				// Resolve the link.
				hres = psl->Resolve(/*hwnd*/0, 0);

				if (SUCCEEDED(hres))
				{
					// Get the path to the link target.
					TCHAR szGotPath[MAX_PATH] = {0};
					hres = psl->GetPath(szGotPath, MAX_PATH, NULL, SLGP_SHORTPATH);

					if (SUCCEEDED(hres))
					{
						memcpy(lpszPath,szGotPath,iPathBufferSize);
					}
				}
			}

			// Release the pointer to the IPersistFile interface.
			ppf->Release();
		}

		// Release the pointer to the IShellLink interface.
		psl->Release();
	}
	return lpszPath;
}

/*
CONST STR : NATIVE_Import_native_shortcut_info = @
            ""

            NATIVE_Resolve_link = @
            "   RESULT = AL4_shortcut::Resolve_link(X_Path);" .



END
*/
