Tuesday, April 14, 2009

[WIN32] DLL 자신의 경로 얻는 방법

1. 개요
DLL 자신의 경로를 얻는 방법을 설명한다.

2. 본문
샘플 코드는 다음과 같다.

1) ATL COM DLL의 경우

TCHAR szPath[MAX_PATH + 1] = {0};
HINSTANCE hInst = _AtlBaseModule.GetModuleInstance();

::GetModuleFileName(hInst, szPath, MAX_PATH);

2) WIN32 DLL의 경우

// Global var to contain DLL module handle
HMODULE g_hDLL = NULL;


BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        g_hDLL = hModule;
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
    break;
    }
    return TRUE;
}

void somefunc()
{
    MA_CHAR pszLibDirPath[MAX_PATH + 1] = {0};

    ::GetModuleFileName(g_hDLL, pszLibDirPath, MAX_PATH);
}

Monday, April 13, 2009

[C++] STL string을 이용한 Path 분리 방법

1. 개요
STL string을 이용하여 File Path의 경로를 분리한다.

2. 본문
샘플 코드는 다음과 같다.

// File Path와 File Name 분리
char szFullPath[MAX_PATH] = "c:\\test\\test.txt";
string strFullPath(szFullPath);
string strFilePath, strFileName;
int nFind = strFullPath.rfind("\\") + 1;
strFilePath = strFullPath.substr(0, nFind);
strFileName = strFullPath.substr(nFind, strFullPath.length() - nFind);


// 파일 확장자 바꾸기
char szFullPath[MAX_PATH] = "c:\\test\\test.txt";
string strFilePath(szFullPath);
string strModExt("xml");
string strReName;
int nExt = strFilePath.rfind("txt");
int nName = strFilePath.rfind("\\") + 1;

strReName = strFilePath.substr(0, nName);
strReName += strFilePath.substr(nName, nExt - nName);
strReName += strModExt;