이 블로그 검색

2014년 6월 19일 목요일

system32폴더와 syswow64폴더


64비트 윈도우즈에서 GetSystemDirectory 함수를 호출하면 어떤 결과가 나올까.

만약 32비트 프로세스에서 실행을 한다면 c:\windows\system32 가 나오게 된다.
그리고, 64비트 프로세스에서도 마찬가지로 c:\windows\system32 가 나오게 된다.

하지만 이건 같은것이 아니다.
32비트 프로세스에서 c:\windows\system32 폴더에 뭔가를 쓰게 되면 실질적으로는 c:\windows\syswow64 폴더에 써지게 된다. 즉, 내부적으로 저렇게 리다이렉트가 되는 것이다.

경로를 코드상에 c:\windows\system32 로 하드코딩한 경우도 있을 것이고 GetSystemDirectory 같은 거로 구했을 수도 있을 것이다.
두 경우 다 보이기에는 c:\windows\system32 로 보이지만, 운영체제 내부적으로 syswow64 폴더로 리다이렉트를 해주게 된다. 이렇게 64비트 운영체제에서 32비트 프로세스가 호환되도록 해놓은 것이다.

64비트 프로세스에서는 c:\windows\system32 가 코드상으로나 물리적으로나 시스템폴더가 된다.

용어상으로는 32비트 프로세스는 system32, 64비트 프로세스는 syswow64 일거 같으나, 위와 같이 전혀 그렇지가 않은 것이다.
SysWow64 폴더는 32비트 프로세스를 위한 폴더인 것이다.

간단한 테스트로, 탐색기로 c:\windows\system32에 있는 cmd.exe 와 c:\windows\syswow64 에 있는 cmd.exe 를 실행시킨다.
작업 관리자로 보면 system32에 있는 cmd.exe는 64비트로 실행되고 있고, syswow64에 있는 cmd.exe는 32비트로 실행된다.
syswow64에 있는 cmd.exe 에서 경로를 cd c:\windows\system32 로 옮겨 본다.
그런 후 그 폴더로 파일을 하나 복사해본다.
탐색기로 그 복사된 파일을 찾아보면 c:\windows\system32 폴더에는 없고, syswow64 폴더에 복사된 것을 확인할 수 있다.
물론, syswow64의 cmd.exe 에서는 system32 에 파일이 복사된 것으로 나온다... 즉, 리다이렉트가 된 것이어서 프로세스에서는 자신이 system32에 있는것으로 판단하지 syswow64 경로에 있다고 판단을 못하는 것이다.

마찬가지로 system32에 있는 cmd.exe로 꼭같은 작업을 해보면 정상적으로 c:\windows\system32에 파일이 복사됨을 알 수가 있다.



2014년 6월 15일 일요일

레지스트리 시작프로그램 등록

//////////////////////////////////////////////////////////
//  Function Name  
//      SetRegistyStartProgram
//
//  Parameters 
//      bAutoExec[in]     : TRUE이면 시작프로그램 레지스트리에 등록, FALSE면 해제
//      lpValueName[in]   : 설정할 값의 이름
//      lpExeFileName[in] : 실행시킬 프로그램 Full 경로 (NULL 일수 있음, 단, bAutoExec값이 FALSE이여야 함)
//
//  Return Values
//      시작프로그램 레지스트리에 등록/헤제 성공이면 TRUE, 실패면 FALSE     
//
BOOL CAutoRunProgramDlg::SetRegistyStartProgram(BOOL bAutoExec, LPCSTR lpValueName, LPCSTR lpExeFileName)
{
    HKEY hKey;
    LONG lRes;
    if(bAutoExec)
    {
        if(lpValueName == NULL || lpExeFileName == NULL)
            return FALSE;
  //lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0L, KEY_ALL_ACCESS, &hKey);
  //lRes = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0L, KEY_WRITE, &hKey);
  lRes = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0L, KEY_WRITE, &hKey);
        if( lRes == ERROR_SUCCESS )
        {
   lRes = ::RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (BYTE*)lpExeFileName, lstrlen(lpExeFileName)); 
   ::RegCloseKey(hKey);
   if(lRes == ERROR_SUCCESS) 
    AfxMessageBox("성공적으로 시작 프로그램에서 등록됐습니다.");
   else
   {
    AfxMessageBox("Error");
    return FALSE;
   }
  }
  else if(lRes == ERROR_ACCESS_DENIED)
  {
   AfxMessageBox("이 소프트웨어를 설치하기 위해서는 이 컴퓨터에 대한 충분한 권한이 있어야 합니다.");
   return FALSE;
  }
  else
  {
   AfxMessageBox("Error");
   return FALSE;
  }
    }
    else 
    {
  lRes = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey);
        if( lRes != ERROR_SUCCESS )
  {
   return FALSE;
  }
        lRes = RegDeleteValue(hKey, lpValueName);      
        RegCloseKey(hKey);
        if(lRes == ERROR_SUCCESS) 
   AfxMessageBox("성공적으로 시작 프로그램에서 삭제됐습니다.");
  else if(lRes == ERROR_FILE_NOT_FOUND)
  {
   AfxMessageBox("프로그램이 시작 프로그램에 등록되어 있지 않습니다.");
   return FALSE;
  }
  else
  {
   AfxMessageBox("시작 프로그램에서 삭제하지 못했습니다.");
   return FALSE;
  }
    }

    return TRUE;


ex >

 SetRegistyStartProgram(TRUE, "AAA", "C:\\Test\\A.exe");    // 등록
 SetRegistyStartProgram(FALSE, "AAA", NULL);    // 해제

2014년 6월 12일 목요일

vc++ 프로그램 콘솔창(consol) 실행시 자동으로 숨기기

Method #1
---------------------------------------------
---------------------------------------------

1. 프로젝트 속성 -> 링커 -> 시스템 -> 하위시스템 -> Windows(/SUBSYSTEM:WINDOWS)로 변경

2. main 함수 변경
 int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow )

3. #include <windows.h> 추가


Method #2
----------------------------------------
------------------------------------------
Method #1은 .net 2003에서 안통하는 방법.
따라서 아래의 방법을 사용합니다.


#define _WIN32_WINNT 0x0500  // 필수
#include <windows.h>
#include <iostream>


int main()
{

HWND hWnd = GetConsoleWindow();
        ShowWindow( hWnd, SW_HIDE );
....... 블라블라블라........
 return 0;
}

제어판 EXE 등록 방법

밑 두곳 참고하면 가능하다. MSDN은 진리

http://msdn.microsoft.com/ko-kr/library/bb757044.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/hh127450(v=vs.85).aspx

How to Register Executable Control Panel Items

For Control Panel items that are implemented as .exe files, no special exports or message handling is required. Any .exe file can be registered as a command object to appear with an entry point in the Control Panel folder.
An example is used here to demonstrate the registration requirements. The example shows how to register a Control Panel item called My Settings as a command object so that it appears in the Control Panel window. The My Settingswindow also appears when the command MyApp.exe /settings is run.

Instructions

Step 1:

Generate a GUID for the Control Panel item. The GUID uniquely identifies the Control Panel item. In this example,{0052D9FC-6764-4D29-A66F-2F3BD9E2BB40} is the GUID of the Control Panel item.

Step 2:

Using the GUID as a name, add a subkey to the registry as follows.
HKEY_LOCAL_MACHINE
   SOFTWARE
      Microsoft
         Windows
            CurrentVersion
               Explorer
                  ControlPanel
                     NameSpace
                        {0052D9FC-6764-4D29-A66F-2F3BD9E2BB40}
                           (Default) = My Settings
The data for the Default entry is simply the REG_SZ name of the Control Panel item. The Default entry can be useful to identify the GUID entry, but it is optional.

Step 3:

Using the GUID as a name, add a subkey and its entries to the registry as follows.
HKEY_CLASSES_ROOT
   CLSID
      {0052D9FC-6764-4D29-A66F-2F3BD9E2BB40}
         (Default) = My Settings
         LocalizedString = @%ProgramFiles%\MyCorp\MyApp.exe,-9
         InfoTip = @%ProgramFiles%\MyCorp\MyApp.exe,-5
         System.ApplicationName = MyCorporation.MySettings
         System.ControlPanel.Category = 1,8
         System.Software.TasksFileUrl = %ProgramFiles%\MyCorp\MyApp\MyTaskLinks.xml
  • Default. REG_SZ. The display name for the Control Panel item.
  • LocalizedString. Optional. REG_SZ or REG_EXPAND_SZ. The module name and string table ID of the localized name of the Control Panel item. The format is an "at" sign (@) followed by the name of the .exe or .dll that contains the Multilingual User Interface (MUI) string table. Environment variables can be used as a substitute for a part of the path. The path and file name is followed by a comma (,) and a hyphen (-), followed by the ID in the string table.
    If the module does not have a string table, then this entry can simply be the display name string. If you use only the display name string rather than a string table, the name does not adjust to the current display language.
  • InfoTip. REG_SZ or REG_EXPAND_SZ. A description of the Control Panel item. This information is shown in an InfoTip that is displayed when the mouse hovers over the item's icon. The syntax is the same as that used for LocalizedString, including the option of simply providing a string rather than a string table reference.
  • System.ApplicationName. REG_SZ. The canonical name of the item. The command of form control.exe /name System.ApplicationName opens the item; for example, control.exe /name MyCorporation.MySettings. See Executing Control Panel Items for more information on the use of Control.exe.
  • System.ControlPanel.Category. REG_SZ. A value that declares the Control Panel categories where the item appears. Multiple categories are separated by commas. In the case of the example above, the entry specifies that the My Settingsitem should appear in both the Appearance and Personalization and Programs categories. See Assigning Control Panel Categories for possible category values.
  • System.Software.TasksFileUrl. REG_SZ or REG_EXPAND_SZ. The path of the XML file that defines task links. This can be a direct file path as shown in the example, or an embedded resource specified as a module name and resource ID such as "%ProgramFiles%\MyCorp\MyApp\MyApp.exe,-31".

Step 4:

Under that same GUID subkey, add the following subkey to the registry to provide the path of the file that contains the icon and the resource ID of the image within that file.
HKEY_CLASSES_ROOT
   CLSID
      {0052D9FC-6764-4D29-A66F-2F3BD9E2BB40}
         DefaultIcon
            (Default) = %ProgramFiles%\MyCorp\MyApp.exe,-2
Note that while the syntax is otherwise similar to the LocalizedString and InfoTip entries discussed earlier, no '@' character is used as a prefix in the REG_SZ or REG_EXPAND_SZ entry that specifies the path.

Step 5:

Add the following information to the registry to provide the command that is called by the system when the user opens the Control Panel.
HKEY_CLASSES_ROOT
   CLSID
      {0052D9FC-6764-4D29-A66F-2F3BD9E2BB40}
         Shell
            Open
               Command
                  (Default) = [REG_EXPAND_SZ] %ProgramFiles%\MyCorp\MyApp.exe /Settings

Related topics