블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2731)
Unity3D (814)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (57)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (51)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
03-28 00:02

C#에서 managed C++로 윈도우 핸들을 보내는 방법.

C++ part
public ref class TmMainWindow
{
    public:
        TmMainWindow(int windowHandle)
        {
            mWindowHandle = (HWND)windowHandle;
        }
    private:
        HWND mWindowHandle;
};


C# part
public partial class MainForm : Form
{
    private TmMainWindow mMainWindow;

    private void MainForm_Load(object sender, System.EventArgs e)
    {
        mMainWindow = new TmMainWindow(this.Handle.ToInt32()); 
        // 이부분이 윈도우 핸들을 넘기는 방법. 64bit os는 ToInt64일듯.
    }
};


위와 같이 하면 된다.
여담이지만, WeifenLuo 라이브러리를 쓰다가 constructor에서 윈도우 핸들값을 얻는 경우 값이 잘 못 나왔다. Load event 에서 window handle을 얻는 것은 괜찮았다. 이것 때문에 고생 좀 했다.



반응형
Posted by blueasa
, |

윈도우에 열려있는 창을 제어하기 위한 핸들값을 가져오는 API 함수
창의 클래스명이나 캡션명은 Spy++ 로 알 수 있다.
※주의사항
함수명의 대소문자가 틀리면 안된다.
FindWindowEx() 함수를 FindWindowEX() 로 했더니 에러 발생.

[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
-> 창의 클래스명이나 창 캡션명으로 최상위 핸들값을 가져옴.
예)
int i = FindWindow(null, "Windows Messenger");  // 창의 캡션명으로 찾기
int j = FindWindow("MSBLClass", null);  // 창의 클래스명으로 찾기

[DllImport("user32.dll")]
public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
-> 인자값으로 받은 핸들의 자식 핸들을 가져옴. lpsz1 은 클래스명, lpsz2 는 캡션명.
예)
int hw2 = FindWindowEx(hw1, 0, "PluginHostClass", null);  // PluginHostClass 의 핸들값 가져옴.
int hw3 = FindWindowEx(hw2, 0, "MSBLGeneric", null);  // MSBLGeneric 클래스의 핸들값.
int hw4 = FindWindowEx(hw3, 0, MSBLGeneric", "Task List");  // MSBLGeneric 클래스이며 Task List 캡션명의 핸들값. 
반응형
Posted by blueasa
, |