Click here to Skip to main content
16,004,974 members
Home / Discussions / C#
   

C#

 
AnswerRe: Sending a String using SendMessage Pin
lisan_al_ghaib24-Aug-08 8:43
lisan_al_ghaib24-Aug-08 8:43 
GeneralRe: Sending a String using SendMessage Pin
eyalbi00724-Aug-08 10:06
eyalbi00724-Aug-08 10:06 
AnswerRe: Sending a String using SendMessage Pin
Mark Salsbery24-Aug-08 8:57
Mark Salsbery24-Aug-08 8:57 
GeneralRe: Sending a String using SendMessage Pin
eyalbi00724-Aug-08 10:07
eyalbi00724-Aug-08 10:07 
GeneralRe: Sending a String using SendMessage Pin
Mark Salsbery24-Aug-08 10:15
Mark Salsbery24-Aug-08 10:15 
GeneralRe: Sending a String using SendMessage Pin
eyalbi00724-Aug-08 22:01
eyalbi00724-Aug-08 22:01 
GeneralRe: Sending a String using SendMessage Pin
Mark Salsbery25-Aug-08 5:24
Mark Salsbery25-Aug-08 5:24 
AnswerRe: Sending a String using SendMessage [modified] Pin
oobimoo25-Aug-08 3:15
oobimoo25-Aug-08 3:15 
You must use the WM_COPYDATA msg
in the c# proj declare the following
[StructLayout(LayoutKind.Sequential)]
    struct CopyDataStruct
    {
        public IntPtr dwData;
        public int dataSize;
        public IntPtr data;
    }

    enum WinMessage
    {
        WM_COPYDATA = 0x4A
    }

    class NativeAPI
    {
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, WinMessage msg, IntPtr wParam, IntPtr lParam);
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string className, string windowName);
    }

and when you press a button for example
private void button1_Click(object sender, EventArgs e)
        {        
            string s = "This is a message from sharp"; // string to send to the native application
            // native window handle
            IntPtr hCWindow = NativeAPI.FindWindow("RECIEVE_DATA", "Recieve_Data");
            // check and handle the not found (null) case here...

            // GetBytes returns the bytes of the string but not the terminating null char
            byte[] tmpStringData = System.Text.Encoding.Unicode.GetBytes(s);
            // so we copy to a new byte array and adding 2 zeros (for unicode)
            byte[] stringData = new byte[tmpStringData.Length + 2];
            Array.Copy(tmpStringData, stringData, tmpStringData.Length);
            stringData[stringData.Length - 2] = stringData[stringData.Length - 1] = 0;
            
            CopyDataStruct cds = new CopyDataStruct();
            cds.dwData = IntPtr.Zero;  // no need for this example
            cds.dataSize = stringData.Length; 
            // pinning the managed array and getting the address
            GCHandle gch_data = GCHandle.Alloc(stringData, GCHandleType.Pinned);
            cds.data = gch_data.AddrOfPinnedObject();
            // pinning the struct also
            GCHandle gch_cds = GCHandle.Alloc(cds, GCHandleType.Pinned); 
            // when sending a WM_COPYDATA msg wParam is the hWnd of the 
            // sender and lParam a pointer to a COPYDATASTRUCT                   
            NativeAPI.SendMessage(hCWindow, WinMessage.WM_COPYDATA, this.Handle, gch_cds.AddrOfPinnedObject());
            // free the handles
            gch_data.Free();
            gch_cds.Free();
        }


and in the windowproc of the native app
case WM_COPYDATA:
    {
    PCOPYDATASTRUCT pcds = (PCOPYDATASTRUCT)lParam;
    MessageBox(0, (LPCTSTR)pcds->lpData, _T("CopyData message"), 0);
    }
    break;

The windowclass name is "RECIEVE_DATA" and the window name is "Recieve_Data" (for use with the find file in c#, although windowclass name may be null)
The default charset for my configuration is unicode. You must use ASCII encoding to get the bytes of the string in the c# code, otherwise

modified on Monday, August 25, 2008 9:39 AM

GeneralRe: Sending a String using SendMessage Pin
eyalbi00725-Aug-08 3:49
eyalbi00725-Aug-08 3:49 
GeneralRe: Sending a String using SendMessage Pin
oobimoo25-Aug-08 4:09
oobimoo25-Aug-08 4:09 
GeneralRe: Sending a String using SendMessage Pin
eyalbi00725-Aug-08 6:10
eyalbi00725-Aug-08 6:10 
GeneralRe: Sending a String using SendMessage [modified] Pin
oobimoo25-Aug-08 6:34
oobimoo25-Aug-08 6:34 
GeneralRe: Sending a String using SendMessage Pin
Alexandro Maceiras27-Jul-11 3:23
Alexandro Maceiras27-Jul-11 3:23 
Questionc# StreamReader question Pin
dfxiris24-Aug-08 5:22
dfxiris24-Aug-08 5:22 
AnswerRe: c# StreamReader question Pin
Guffa24-Aug-08 11:37
Guffa24-Aug-08 11:37 
QuestionBest Search Engine for .Net Developers Pin
Brij Kishor24-Aug-08 0:18
Brij Kishor24-Aug-08 0:18 
AnswerRe: Best Search Engine for .Net Developers Pin
Vimalsoft(Pty) Ltd24-Aug-08 2:03
professionalVimalsoft(Pty) Ltd24-Aug-08 2:03 
AnswerRe: Best Search Engine for .Net Developers Pin
Abhijit Jana24-Aug-08 2:52
professionalAbhijit Jana24-Aug-08 2:52 
AnswerRe: Best Search Engine for .Net Developers Pin
Brij24-Aug-08 2:55
mentorBrij24-Aug-08 2:55 
AnswerRe: Best Search Engine for .Net Developers Pin
Thomas Stockwell24-Aug-08 5:10
professionalThomas Stockwell24-Aug-08 5:10 
AnswerRe: Best Search Engine for .Net Developers Pin
Paul Conrad24-Aug-08 6:45
professionalPaul Conrad24-Aug-08 6:45 
GeneralRe: Best Search Engine for .Net Developers Pin
Blue_Boy24-Aug-08 10:00
Blue_Boy24-Aug-08 10:00 
GeneralRe: Best Search Engine for .Net Developers Pin
Paul Conrad24-Aug-08 10:03
professionalPaul Conrad24-Aug-08 10:03 
GeneralRe: Best Search Engine for .Net Developers Pin
Blue_Boy24-Aug-08 10:05
Blue_Boy24-Aug-08 10:05 
GeneralRe: Best Search Engine for .Net Developers Pin
Pete O'Hanlon24-Aug-08 10:58
mvePete O'Hanlon24-Aug-08 10:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.