>using System; >using System.Collections.Generic; >using System.Text; >using System.Windows.Forms; >using System.Runtime.InteropServices; > >namespace ConsoleApplication6 >{ > class Program > { > static void Main(string[] args) > { > Form f = new MyForm(); > f.Show(); > f.Hide(); > ApplicationContext ctx = new ApplicationContext(); > Application.Run(ctx); > } > } > > class MyForm : Form > { > const int WM_COPYDATA = 0x004a; > > public MyForm() > { > Text = "my_unique_id"; > } > > protected override void WndProc(ref System.Windows.Forms.Message m) > { > switch (m.Msg) > { > case WM_COPYDATA: > COPYDATASTRUCT mystr = new COPYDATASTRUCT(); > Type mytype = mystr.GetType(); > mystr = (COPYDATASTRUCT)m.GetLParam(mytype); > MessageBox.Show(mystr.Data); > break; > } > base.WndProc(ref m); > } > } > [StructLayout(LayoutKind.Sequential)] > public struct COPYDATASTRUCT > { > public Int32 ID; > public int Length; > public string Data; > } >} >>Here's the corresponding VFP code.
>* Find handle >Local lnHandle >Declare Long FindWindow in Win32API String, String >lnHandle = FindWindow(NULL,"my_unique_id") > >*Declare API functions >Declare Long HeapAlloc in win32api Long, Long, Long >Declare Long GetProcessHeap in win32api >Declare Long HeapFree in win32api Long, Long, Long >#define WM_COPYDATA 0x4A > >* Create a COPYDATASTRUCT structure * >Local lcStruct, lnMem, lcData, lnIdentifier >lcData = "Hello World"+Chr(0) >lnIdentifier = 1 >lnMem = HeapAlloc( GetProcessHeap(), 0, Len(m.lcData)+12 ) >lcStruct = ; >toInt(m.lnIdentifier,4) + ; >toInt(Len(m.lcData),4) + ; >toInt(m.lnMem+12,4) + ; >m.lcData >Sys(2600,m.lnMem,Len(m.lcStruct),m.lcStruct) > >*Send data to the other application * >Local lnRetVal >Declare Long SendMessage in win32api Long, Long, Long, Long >lnRetVal = SendMessage( m.lnHandle, WM_COPYDATA, _VFP.Hwnd, lnMem ) > >* Free up memory * >HeapFree( GetProcessHeap(), 0, m.lnMem ) >>Execute the Console application first, then the VFP application. Anything passed from the VFP application is displayed in a messagebox by the C# application. You might have to use ALT+TAB since the C# application does not automatically come to front. In the console you can press Ctrl+C to terminate the application.