1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include <windows.h> #include <atlbase.h> #include <atlstr.h>
#define LARGE_PATH 520 LSTATUS ParseLnkGetPath(_In_ CStringA csLnk, _Out_ CStringA &csPath) { csLnk.MakeLower(); if (csLnk.GetLength() < 5) return ERROR_INVALID_PARAMETER; if (csLnk.Right(4) != ".lnk") return ERROR_INVALID_PARAMETER; CHAR szTemp[LARGE_PATH] = { 0 }; CHAR szProductCode[39] = { 0 }; CHAR szFeatureId[MAX_FEATURE_CHARS + 1] = { 0 }; CHAR szComponentCode[39] = { 0 }; LSTATUS ret = MsiGetShortcutTargetA( (PCSTR)csLnk, szProductCode, szFeatureId, szComponentCode); if (ret == ERROR_SUCCESS) { DWORD dwSize = LARGE_PATH * sizeof(WCHAR); INSTALLSTATE state = MsiGetComponentPathA( szProductCode, szComponentCode, szTemp, &dwSize); if (state == INSTALLSTATE_LOCAL) { _strlwr_s(szTemp); csPath = szTemp; return ERROR_SUCCESS; } return ERROR_INVALID_PARAMETER; } ret = ERROR_INVALID_PARAMETER; HANDLE hFile = CreateFileA((PCSTR)csLnk, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return ret; DWORD dwFlag = 0; DWORD dwReadLen = 0; if (!ReadFile(hFile, &dwFlag, sizeof(DWORD), &dwReadLen, NULL)) goto Last; if (dwReadLen != sizeof(DWORD)) goto Last; if (dwFlag != 0x0000004CUL) goto Last;
DWORD dwRet = SetFilePointer(hFile, 0x00000014L, NULL, FILE_BEGIN); if (dwRet == INVALID_SET_FILE_POINTER) goto Last;
if (!ReadFile(hFile, &dwFlag, sizeof(DWORD), &dwReadLen, NULL)) goto Last; if (dwReadLen != sizeof(DWORD)) goto Last;
dwRet = SetFilePointer(hFile, 0x0000004CL, NULL, FILE_BEGIN); if (dwRet == INVALID_SET_FILE_POINTER) goto Last;
if (dwFlag & 0x00000001UL) { WORD wSize = 0; if (!ReadFile(hFile, &wSize, sizeof(WORD), &dwReadLen, NULL)) goto Last; if (dwReadLen != sizeof(WORD)) goto Last;
dwRet = SetFilePointer(hFile, (LONG)wSize, NULL, FILE_CURRENT); if (dwRet == INVALID_SET_FILE_POINTER) goto Last; }
dwRet = SetFilePointer(hFile, 0x00000010L, NULL, FILE_CURRENT); if (dwRet == INVALID_SET_FILE_POINTER) goto Last;
if (!ReadFile(hFile, &dwFlag, sizeof(DWORD), &dwReadLen, NULL)) goto Last; if (dwReadLen != sizeof(DWORD)) goto Last;
dwRet = SetFilePointer(hFile, -0x00000010L - (LONG)sizeof(DWORD) + (LONG)dwFlag, NULL, FILE_CURRENT); if (dwRet == INVALID_SET_FILE_POINTER) goto Last;
int i = 0; char ch = 0; memset(szTemp, 0, sizeof(szTemp)); do { if (!ReadFile(hFile, &ch, sizeof(char), &dwReadLen, NULL)) goto Last; *(szTemp + i++) = ch; } while (ch != '\0');
_strlwr_s(szTemp); csPath = szTemp; ret = ERROR_SUCCESS; Last: CloseHandle(hFile); return ret; }
|