fortran调用API函数SendMessage,函数定义如下
[Fortran] 纯文本查看 复制代码 FUNCTION SendMessage( &
hWnd, &
Msg, &
wParam, &
lParam)
use ifwinty
integer(LRESULT) :: SendMessage ! LRESULT
!DEC$ ATTRIBUTES DEFAULT, STDCALL, DECORATE, ALIAS:'SendMessageA' :: SendMessage
integer(HANDLE) hWnd ! HWND hWnd
integer(UINT) Msg ! UINT Msg
integer(UINT_PTR) wParam ! WPARAM wParam
integer(LONG_PTR) lParam ! LPARAM lParam
END FUNCTION
COPYDATASTRUCT结构体如下:
[Fortran] 纯文本查看 复制代码 TYPE T_COPYDATASTRUCT
SEQUENCE
integer(pointer_len) dwData ! typedefs ULONG_PTR
integer(DWORD) cbData ! knowns DWORD
integer(PVOID) lpData ! knowns PVOID
END TYPE
[Fortran] 纯文本查看 复制代码
program SendMsg_Fortran
use ifwin
implicit none
integer(LRESULT)::ret
integer(HANDLE)::hWnd
!integer,pointer::a(:)
real,dimension(5,3),parameter :: b = &
& (/0.2,0.4,0.1,0.1,0.1, &
& 0.1,0.1,0.1,0.8,0.1, &
& 0.1,0.1,0.1,0.1,0.1 /)
type(T_COPYDATASTRUCT)::COPYDATASTRUCT
COPYDATASTRUCT%dwData = size(b)
COPYDATASTRUCT%cbData = sizeof(b)
COPYDATASTRUCT%lpData = loc(b)
hWnd = FindWindow_G2(NULL,'Form1')!找句柄
ret=SendMessage (hWnd, 74, 0, loc(COPYDATASTRUCT)) !WM_COPYDATA 74
end program
C#接受消息并解析
C# COPYDATASTRUCT接口提定义
[C#] 纯文本查看 复制代码 public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
复写DefWndProc函数
[C#] 纯文本查看 复制代码 const int WM_COPYDATA = 0x004A;
float[] ff;
protected override void DefWndProc(ref Message m)
{
unsafe
{
switch (m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT myData = new COPYDATASTRUCT();//这是创建自定义信息的结构
Type mytype = myData.GetType();
myData = (COPYDATASTRUCT)m.GetLParam(mytype);//这里获取的就是作为LParam参数发送来的信息的结构
ff = new float[(int)myData.dwData];
Marshal.Copy(myData.lpData, ff, 0, (int)myData.dwData);
for (int i = 0; i < ff.Length; i++)
{
textBox1.Text += ff[i].ToString() + "\r\n";
}
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
|