我有兩個mdi應用程序,它們都從同一個數據庫中檢索它們的數據。 這兩個應用程序需要能夠互相發送消息以保持同步。 來回傳遞的消息只包含一個字符串,告訴接收應用程序應該查看數據庫中的哪一部分數據(作業號和一些其他相關信息)。 這兩個應用程序都有一個消息處理程序,當每個程序啓動時實例化。c#和VB6 mdi應用程序之間的消息交互
當消息從VB6應用程序發送到C#應用程序時,它會看到消息,並且行爲恰當,但是當我從C#應用程序向VB6應用程序發送相同類型的消息時,它似乎看到消息事件,但在解包時只能看到部分數據,然後忽略該消息。
我想我可能會格式化C#結尾上的錯誤。 這裏是發送消息的方法:
namespace InterProcessMessaging
{
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;//a pointer to a number use this to identify your message
public IntPtr lpData;//a pointer to the address of the data
public IntPtr cbData;//a pointer to the number of bytes to be transferred
}
public class clsMessaging : System.Windows.Forms.NativeWindow, IDisposable
{
//API function to send async. message to target application
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr SendMessageA(IntPtr hwnd, Int32 wMsg, Int32 wParam, COPYDATASTRUCT lParam);
public void SendMessageToVB6(string sendMsg, string WindowsAppTitle)
{
try
{
IntPtr hwndTarget = FindWindow(null, WindowsAppTitle);
IntPtr pDWData = Marshal.AllocHGlobal(sizeof(Int32));//a pointer to a number used this to identify your message
Marshal.StructureToPtr(3, pDWData, true);//place the value 3 at this location
IntPtr pLPData = Marshal.StringToHGlobalAnsi(sendMsg.Trim());//a pointer to the address of the data
IntPtr pCBData = Marshal.AllocHGlobal(sizeof(Int32));//a pointer to the number of bytes to be transferred
Marshal.StructureToPtr(sendMsg.Trim().Length+1, pCBData, true);//place the size of the string at this location
COPYDATASTRUCT cds;//a structure containing the three pointers above
cds.dwData = pDWData;//a pointer to a number used this to identify your message (3)
cds.lpData = pLPData;//a pointer to the address of the data
cds.cbData = pCBData;//a pointer to the number of bytes to be transferred
if (!System.IntPtr.Zero.Equals(hwndTarget))
{
SendMessageA(hwndTarget, 74, 0, cds);
}
}
catch (Exception ex)
{
Debug.Print(ex.InnerException.ToString());
}
}
}
}
我同意,並已在當地也有類似的建議。大部分這是繼承。 – 2010-12-02 22:12:16
我不知道你是否可以更多地闡述Windows 7上的Windows消息傳遞的侷限性。當傳統應用程序遷移到Windows 7 - 64位環境時,出現了這種情況。我現在想要做的就是讓它工作,這樣我就可以花時間來替換它,可能是基於Web的應用程序。 – 2010-12-03 13:45:23