2008/08/20 10:52

GetCurrentDirectory()를 이용한 Windows에서 현재경로 받아오기..


Windows에서 현재 실행되는 프로그램의 현재 작업 디렉토리를 알아오기 위해 "GetCurrentDirectory()"를 사용한다.

첫번 째 인자로 경로를 표시할 문자열의 길이를 넣어주고, 두번 째 인자로 실제 받아온 경로를 보여준다.

간단히 예를 들면...
char Path[255];

GetCurrentDirectory(255, Path);
printf("Current Path is %s\n", Path);

이렇게 하면 콘솔창에서 현재 위의 프로그램의 경로를 확인할 수 있다.

MSDN을 검색하니 다음과 같이 되어있다.

GetCurrentDirectory Function

Retrieves the current directory for the current process.

Syntax

DWORD WINAPI GetCurrentDirectory(
__in DWORD nBufferLength,
__out LPTSTR lpBuffer
);

Parameters

nBufferLength [in]

The length of the buffer for the current directory string, in TCHARs. The buffer length must include room for a terminating null character.

lpBuffer [out]

A pointer to the buffer that receives the current directory string. This null-terminated string specifies the absolute path to the current directory.

To determine the required buffer size, set this parameter to NULL and the nBufferLength parameter to 0.

Return Value

If the function succeeds, the return value specifies the number of characters that are written to the buffer, not including the terminating null character.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If the buffer that is pointed to by lpBuffer is not large enough, the return value specifies the required size of the buffer, in characters, including the null-terminating character.

Remarks

Each process has a single current directory that consists of two parts:

  • A disk designator that is either a drive letter followed by a colon, or a server name followed by a share name (\\servername\sharename)
  • A directory on the disk designator

To set the current directory, use the SetCurrentDirectory function.

In certain rare cases, if the specified directory is on the current drive, the function might omit the drive letter and colon from the path. Therefore, the size that is returned by the function might be two characters less than the size of the specified string, not including the terminating null character. This behavior might occur in edge situations such as in a services application. If you need the drive letter, make a subsequent call to GetFullPathName to retrieve the drive letter.

Examples

For an example, see Changing the Current Directory.

Requirements

Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header

Declared in WinBase.h; include Windows.h.

Library

Use Kernel32.lib.

DLL

Requires Kernel32.dll.

Unicode/ANSI

Implemented as GetCurrentDirectoryW (Unicode) and GetCurrentDirectoryA (ANSI).



크리에이티브 커먼즈 라이선스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
Trackback 0 Comment 0
2008/08/19 16:56

Windows 프로그램을 시작 프로그램으로 등록하기


MS Windows를 부팅할 때 자동으로 시작하는 프로그램을 볼 수 있다.
그리고 실제 자신이 작성한 프로그램도 그런 프로그램들 처럼 Windows가 시작할 때 자동으로 실행시킬 수 있다.

방법은 여러가지가 있다.

첫번 째는 가장 쉬운 방법으로 "시작 프로그램에 바로가기 복사"를 하는 방법이다.
"C:\Documents and Settings\사용자 폴더\시작 메뉴\프로그램\시작프로그램"에 자동으로 시작하기를 원하는 프로그램의 바로가기를 복사하면 된다.

두번 째는 "win.ini 파일에 등록"하는 방법이다.
"시작 -> 실행 -> sysedit" 를 실행 하여, 자동으로 실행하고자 하는 프로그램을 등록하면 된다.
load - Windows가 부팅할 때 자동으로 시작하는 프로그램을 등록한다.
run - load와 같이 자동  시작 프로그램을 등록한다.

세번 째는 "registry에 등록"하는 방법이다.
"HKEY_LOCAL_MACHINE의 Software\Microsoft\Windows\CurrentVersion\run"가 시작프로그램들이 위치하는 레지스트리의 위치이다. 이곳에 자동으로 시작하는 프로그램을 등록하면 된다.

아래와 같이 하면 자신이 작성하는 프로그램에 직접 소스코드를 넣어서 레지스트리에 등록하여 Windows가 실행 시 자동으로 실행하게 할 수도 있다.
#include <windows.h>
#include <assert.H>

bool cs_util_register_start_Prog  (const char*  sValue, const char*   sPath)
{
    HKEY hKey;
    DWORD dwDisp;
    DWORD dwSize;

   if( sValue == NULL || sPath == NULL)
   {
       return false;
    }
 
    assert(sValue != NULL);
    assert(sPath != NULL);

    if( RegCreateKeyEx (HKEY_LOCAL_MACHINE,
                                 "Software\\Microsoft\\Windows\\CurrentVersion\\run",
                                 0, NULL,REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
                                 NULL, &hKey, &dwDisp) != ERROR_SUCCESS)
    {
        if(hKey != NULL)RegCloseKey(hKey);
        return false;
    }
     dwSize = strlen(sPath);

    if( RegSetValueEx (hKey, sValue, 0, REG_SZ ,(LPBYTE)sPath, dwSize ) != ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        return false;
    }

    RegCloseKey(hKey);
    RegFlushKey(hKey);

    return true;
}

....

void main()
{
    cs_util_register_start_Prog("레지스트리에 등록할 이름","등록할 프로그램의 경로");
    // ex) cs_util_register_start_Prog("test_program","c:\\test_program.exe");
    ....
}

대충 이렇게 해서 테스트 해보니.. 위의 루틴을 삽입한 프로그램을 한 번 실행 시키게 되면, 레지스트리에 위의 프로그램이 등록이 되더군...

대략 설치 파일에 이런 부분을 넣으면 편할 듯 하지만, 단순히 실행파일을 시작과 동시에 실행시키기엔 첫번 째 방법이 가장 쉽고 편할듯... ^^;

참고 : http://blog.naver.com/tija98?Redirect=Log&logNo=120037882771

크리에이티브 커먼즈 라이선스
Creative Commons License
이올린에 북마크하기(0) 이올린에 추천하기(0)
Trackback 0 Comment 0