당분간 명령 줄 문자열과 현재 마우스 커서 좌표를 WPF 응용 프로그램에 보내야하는 네이티브 C++ 응용 프로그램이 있습니다. 메시지는 보내고받을 수 있습니다. 그러나 C#의 IntPtr
인스턴스를 구조체로 변환 할 수 없습니다.WM_COPYDATA를 사용하여 C++에서 WPF로 구조체 보내기
시도 할 때 응용 프로그램이 예외없이 중단되거나 변환하는 코드 줄이 건너 뛰고 루프의 다음 메시지가 수신됩니다. 이것은 아마도 네이티브 예외가 발생했음을 의미하지만 그 이유를 알지 못합니다.
다음은 C++ 프로그램입니다. 당분간 나는 커맨드 라인 문자열을 무시하고 가짜 커서 좌표를 사용하여 정상적으로 작동하는지 확인합니다.
#include "stdafx.h"
#include "StackProxy.h"
#include "string"
typedef std::basic_string<WCHAR, std::char_traits<WCHAR>> wstring;
struct StackRecord
{
//wchar_t CommandLine[128];
//LPTSTR CommandLine;
//wstring CommandLine;
__int32 CursorX;
__int32 CursorY;
};
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
COPYDATASTRUCT data;
ZeroMemory(&data, sizeof(COPYDATASTRUCT));
StackRecord* record = new StackRecord();
wstring cmdLine(lpCmdLine);
//record.CommandLine = cmdLine;
record->CursorX = 5;
record->CursorY = 16;
data.dwData = 12;
data.cbData = sizeof(StackRecord);
data.lpData = record;
HWND target = FindWindow(NULL, _T("Window1"));
if(target != NULL)
{
SendMessage(target, WM_COPYDATA, (WPARAM)(HWND) target, (LPARAM)(LPVOID) &data);
}
return 0;
}
그리고 여기에 메시지를받는 WPF 응용 프로그램의 일부가 있습니다. IF 문 안의 두 번째 줄은 건너 뛰었습니다. 모든 것이 충돌하지 않으면 끝납니다.
public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == Interop.WM_COPYDATA)
{
var data = (Interop.CopyDataStruct)Marshal.PtrToStructure(lParam, typeof(Interop.CopyDataStruct));
var record = (Interop.StackRecord)Marshal.PtrToStructure(data.lpData, typeof(Interop.StackRecord));
MessageBox.Show(String.Format("X: {0}, Y: {1}", record.CursorX, record.CursorY));
}
return IntPtr.Zero;
}
다음은 구조체에 대한 C# 정의입니다. 나는 마샬링 속성으로 끝없이 놀았으며 어디에도 없습니다.
internal static class Interop
{
public static readonly int WM_COPYDATA = 0x4A;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct CopyDataStruct
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
public struct StackRecord
{
//[MarshalAs(UnmanagedType.ByValTStr)]
//public String CommandLine;
public Int32 CursorX;
public Int32 CursorY;
}
}
아이디어가 있으십니까?
이 동작은 동일) : 늦었 어. –