작업하다보니 단축아이콘도 만들어야 겠네.. -_-;;
마우스로 드래그만 하면 되는데 플그램으로 짜려면 왤케 이것 저것 해야하는게 많은건지..
MSDN에서 권장하는 IShellLink 인터페이스를 이용하여 제작합니다.

클래스로 만들 이유는 없는데, 이것 저것 추가될깨 있어어 그냥 껍데기를 씌웠습니다.
참고로 일반적인 헤더 외에, 아래꺼 추가되야 할겁니다.
#include <atlbase.h>
#include <comdef.h>

VC++ 6.0 SP6, 및 Platfrom SDK 2004년 버전에서 테스트됨.

헤더파일
-----------------------------------------------------------------------------------
class CFileShell 
{
public:
 CFileShell();
 virtual ~CFileShell();

    // 지정한 파일로 주어진 특수 경로에 단축아이콘을 만든다.
    BOOL CreateShortCut(const char* filepath, int nFolder, const char* dispname = NULL, const char* description = NULL, const char* workdir = NULL);

    // 특수한 경로에 주어진 이름의 폴더를 만든다.
    CString CreateSpecialForder(int nFolder, const char* forder_name);
};

1. CreateShortCut
   filepath         전체경로를 가진 파일 이름
   nFolder         CSIDL Costants 를 지칭합니다. 참고 SHGetSpecialFolderPath
   dispname      단축아이콘의 이름, 설정하기 않으면 원본 파일이름으로 대체
   description   단축아이콘 설명
   workdir          아이콘이 실행될 경로, 설정하지 않으면 원본 파일의 경로로 대체

2. CreateSpecialForder
   nFolder          CSIDL Costants 를 지칭합니다. 참고 SHGetSpecialFolderPath  
   forder_name  생성할 폴더의 이름



소스 파일
-----------------------------------------------------------------------------------
BOOL CFileShell::CreateShortCut(const char* filepath, int nFolder, const char* dispname, const char* description, const char* workdir)
{
    char TargetPath[MAX_PATH];
    CString fromPath=filepath, fromName, fromExt, ShortCutName;

    if(!SHGetSpecialFolderPath(NULL, TargetPath, nFolder, FALSE))
        return FALSE;

    HRESULT hr;
     CComPtr<IShellLink>   pISL;

    hr = pISL.CoCreateInstance( CLSID_ShellLink );
     if(FAILED(hr))
        return FALSE;

    // 단축 아이콘의 대상이 되는 파일을 설정한다.
    hr = pISL->SetPath ( filepath );
     if(FAILED(hr))
        return FALSE;

    // 주어진 소스 파일의 경로를 이용하여, 전체 패스, 이름, 확장자를 나눈다.
    int find = fromPath.ReverseFind('\\');
    if(find == -1)
        return FALSE;

    fromName = fromPath.Right(fromPath.GetLength()-find-1);
    fromPath = fromPath.Left(find+1);

    find = fromName.ReverseFind('.');
    if(find != -1)
    {
        fromExt  = fromName.Right(fromName.GetLength()-find-1);
        fromName = fromName.Left(find);
    }

    // 별도로 디스플에이용 이름을 주면 그것을 사용한다.
    if(dispname)
        fromName = dispname;

    // 별도로 수행 경로를 주면 그것을 이용한다.
    if(workdir)
        fromPath = workdir;


    // 단축 아이콘에 대한 설명을 단다.
    if(description)
    {
        hr = pISL->SetDescription ( description );
        if(FAILED(hr))
            return FALSE;
    }
   
    // 워킹 폴더를 설정한다.
    hr = pISL->SetWorkingDirectory ( fromPath );
    if(FAILED(hr))
        return FALSE;

   
    WCHAR ShortCutNameW[256] = {0};
    ShortCutName = fromPath + fromName + ".lnk";

    MultiByteToWideChar(CP_ACP, MB_COMPOSITE, ShortCutName, ShortCutName.GetLength(), ShortCutNameW, 256*sizeof(WCHAR));

    CComQIPtr<IPersistFile> pIPF( pISL );
     hr = pIPF->Save ( ShortCutNameW, FALSE );
     if ( FAILED(hr) )
        return FALSE;

    return TRUE;
}

CString CFileShell::CreateSpecialForder(int nFolder, const char* forder_name)
{
    char TargetPath[MAX_PATH];
    if(!SHGetSpecialFolderPath(NULL, TargetPath, nFolder, FALSE))
        return "";

    CString FullName = TargetPath;
    FullName += "\\";
    FullName += forder_name;

    if(!CreateDirectory(FullName, NULL))
    {
        DWORD le = GetLastError();
        if(le != ERROR_ALREADY_EXISTS)
            return "";
    }

    return FullName;
}

+ Recent posts