C# messaging

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GTi

    C# messaging

    Do anyone have a simple solution how I can send a simple message (int)
    from a C program to a C# program/service ?
    Preferred both ways C <-> C#

    (Windows messages - how ?)

    It must be extremely easy and in-memory (no disk media).

    Best regards,
    Newbie GTi


  • dlgproc

    #2
    RE: C# messaging

    Is your C program Windows based or DOS based?

    If both the C and C# are Windows based (they create a window) you can send
    and receive regular windows messages between the 2 applications using
    SendMessage().

    If your C program is DOS based, you may be able to launch it as a child
    process from your C# program and redirect it’s STDIN and STDOUT to send and
    receive text from it.

    Hope this helps.
    Regards,
    DlgProc

    "GTi" wrote:
    [color=blue]
    > Do anyone have a simple solution how I can send a simple message (int)
    > from a C program to a C# program/service ?
    > Preferred both ways C <-> C#
    >
    > (Windows messages - how ?)
    >
    > It must be extremely easy and in-memory (no disk media).
    >
    > Best regards,
    > Newbie GTi
    >
    >
    >[/color]

    Comment

    • GTi

      #3
      Re: C# messaging

      Sorry, didn't mention that.
      Both are windows applications.
      (C# will not be a win service)

      I was thinking of windows messages (SendMessage)
      But how is this in C#? Unable to find any doc about it.
      Sending, receiving messages.
      RegisterWindowM essage() i C#

      [DllImport("user 32.dll")]
      int RegisterWindowM essage(????? lpString);
      What should be used instead of LPCTSTR?


      "dlgproc" <dlgproc@discus sions.microsoft .com> skrev i melding
      news:4D8E9D17-D8B7-4862-9630-C7402C672CA4@mi crosoft.com...[color=blue]
      > Is your C program Windows based or DOS based?
      >
      > If both the C and C# are Windows based (they create a window) you can send
      > and receive regular windows messages between the 2 applications using
      > SendMessage().
      >
      > If your C program is DOS based, you may be able to launch it as a child
      > process from your C# program and redirect it's STDIN and STDOUT to send
      > and
      > receive text from it.
      >
      > Hope this helps.
      > Regards,
      > DlgProc
      >
      > "GTi" wrote:
      >[color=green]
      >> Do anyone have a simple solution how I can send a simple message (int)
      >> from a C program to a C# program/service ?
      >> Preferred both ways C <-> C#
      >>
      >> (Windows messages - how ?)
      >>
      >> It must be extremely easy and in-memory (no disk media).
      >>
      >> Best regards,
      >> Newbie GTi
      >>
      >>
      >>[/color][/color]


      Comment

      • dlgproc

        #4
        Re: C# messaging

        You can use a string there instead of LPCTSTR in C#.

        [DllImport("user 32.dll")]
        public static extern int RegisterWindowM essage(string lpString);

        The string you specify here needs to be a unique string in the Windows
        environment so that you can differentiate your message from any other Window
        message.

        So in your C app, call RegisterWindowM essage(“MY MESSAGE NAME HERE”) You
        will get an int returned to you so save it.

        In your C# app call RegisterWindowM essage(“MY MESSAGE NAME HERE”) (you MUST
        use the same exact string as you did in your C app). You will get an int
        returned to you so save it.

        The ints you get in your C app and your C# app will be the exact same
        number. This number will probably change the next time you boot Windows so
        don’t hard code it.

        Now you can send your unique message between your 2 apps specifying the int
        you got from calling RegisterWindowM essage as the message number in
        SendMessage();

        Put the numeric data you want to send back and forth between your 2 apps in
        the wParam or lParam parameters of SendMessage() and you’re on your way.

        The other thing you need to do is determine how each app is going to find
        the window handle of the other app so it can send the message to it. You can
        do this one of 2 ways:

        First way:
        Hard code the window class and window caption (title bar text) of the other
        app in your app and use FindWindow() to get the window handle. (easy but
        crude)

        Second way:
        Create another message using RegisterWindowM essage. Post this message as a
        broadcast message (set hWnd in PostMessage to 0). All windows on the system
        will get your message. If an app doesn’t recognize your message, it will
        ignore it. But you will set up your other App to recognize it. When posting
        this message, put the handle to the window in lParam. Now when your other app
        gets the broadcast message, it can get the hWnd of the sender. Now you can
        send the same message back to the sender with receiver’s hWnd. Now both apps
        have the window handle to the partner and you can start communicating.

        Best of luck.
        DlgProc

        "GTi" wrote:
        [color=blue]
        > Sorry, didn't mention that.
        > Both are windows applications.
        > (C# will not be a win service)
        >
        > I was thinking of windows messages (SendMessage)
        > But how is this in C#? Unable to find any doc about it.
        > Sending, receiving messages.
        > RegisterWindowM essage() i C#
        >
        > [DllImport("user 32.dll")]
        > int RegisterWindowM essage(????? lpString);
        > What should be used instead of LPCTSTR?
        >
        >
        > "dlgproc" <dlgproc@discus sions.microsoft .com> skrev i melding
        > news:4D8E9D17-D8B7-4862-9630-C7402C672CA4@mi crosoft.com...[color=green]
        > > Is your C program Windows based or DOS based?
        > >
        > > If both the C and C# are Windows based (they create a window) you can send
        > > and receive regular windows messages between the 2 applications using
        > > SendMessage().
        > >
        > > If your C program is DOS based, you may be able to launch it as a child
        > > process from your C# program and redirect it's STDIN and STDOUT to send
        > > and
        > > receive text from it.
        > >
        > > Hope this helps.
        > > Regards,
        > > DlgProc
        > >
        > > "GTi" wrote:
        > >[color=darkred]
        > >> Do anyone have a simple solution how I can send a simple message (int)
        > >> from a C program to a C# program/service ?
        > >> Preferred both ways C <-> C#
        > >>
        > >> (Windows messages - how ?)
        > >>
        > >> It must be extremely easy and in-memory (no disk media).
        > >>
        > >> Best regards,
        > >> Newbie GTi
        > >>
        > >>
        > >>[/color][/color]
        >
        >
        >[/color]

        Comment

        • GTi

          #5
          Re: C# messaging

          Thanks dlgproc,
          [color=blue]
          > Put the numeric data you want to send back and forth between your 2 apps
          > in
          > the wParam or lParam parameters of SendMessage() and you're on your way.[/color]

          But what about if the C APP needs to send message to my C# app?
          How can I wait/recieve Windows Messages in my C# app?
          There is no GetMessage in C# (as I know of)....



          "dlgproc" <dlgproc@discus sions.microsoft .com> wrote in message
          news:10A96F18-166C-4376-99C8-1AE90172BE3D@mi crosoft.com...[color=blue]
          > You can use a string there instead of LPCTSTR in C#.
          >
          > [DllImport("user 32.dll")]
          > public static extern int RegisterWindowM essage(string lpString);
          >
          > The string you specify here needs to be a unique string in the Windows
          > environment so that you can differentiate your message from any other
          > Window
          > message.
          >
          > So in your C app, call RegisterWindowM essage("MY MESSAGE NAME HERE") You
          > will get an int returned to you so save it.
          >
          > In your C# app call RegisterWindowM essage("MY MESSAGE NAME HERE") (you
          > MUST
          > use the same exact string as you did in your C app). You will get an int
          > returned to you so save it.
          >
          > The ints you get in your C app and your C# app will be the exact same
          > number. This number will probably change the next time you boot Windows so
          > don't hard code it.
          >
          > Now you can send your unique message between your 2 apps specifying the
          > int
          > you got from calling RegisterWindowM essage as the message number in
          > SendMessage();
          >
          > Put the numeric data you want to send back and forth between your 2 apps
          > in
          > the wParam or lParam parameters of SendMessage() and you're on your way.
          >
          > The other thing you need to do is determine how each app is going to find
          > the window handle of the other app so it can send the message to it. You
          > can
          > do this one of 2 ways:
          >
          > First way:
          > Hard code the window class and window caption (title bar text) of the
          > other
          > app in your app and use FindWindow() to get the window handle. (easy but
          > crude)
          >
          > Second way:
          > Create another message using RegisterWindowM essage. Post this message as a
          > broadcast message (set hWnd in PostMessage to 0). All windows on the
          > system
          > will get your message. If an app doesn't recognize your message, it will
          > ignore it. But you will set up your other App to recognize it. When
          > posting
          > this message, put the handle to the window in lParam. Now when your other
          > app
          > gets the broadcast message, it can get the hWnd of the sender. Now you can
          > send the same message back to the sender with receiver's hWnd. Now both
          > apps
          > have the window handle to the partner and you can start communicating.
          >
          > Best of luck.
          > DlgProc
          >
          > "GTi" wrote:
          >[color=green]
          >> Sorry, didn't mention that.
          >> Both are windows applications.
          >> (C# will not be a win service)
          >>
          >> I was thinking of windows messages (SendMessage)
          >> But how is this in C#? Unable to find any doc about it.
          >> Sending, receiving messages.
          >> RegisterWindowM essage() i C#
          >>
          >> [DllImport("user 32.dll")]
          >> int RegisterWindowM essage(????? lpString);
          >> What should be used instead of LPCTSTR?
          >>
          >>
          >> "dlgproc" <dlgproc@discus sions.microsoft .com> skrev i melding
          >> news:4D8E9D17-D8B7-4862-9630-C7402C672CA4@mi crosoft.com...[color=darkred]
          >> > Is your C program Windows based or DOS based?
          >> >
          >> > If both the C and C# are Windows based (they create a window) you can
          >> > send
          >> > and receive regular windows messages between the 2 applications using
          >> > SendMessage().
          >> >
          >> > If your C program is DOS based, you may be able to launch it as a child
          >> > process from your C# program and redirect it's STDIN and STDOUT to send
          >> > and
          >> > receive text from it.
          >> >
          >> > Hope this helps.
          >> > Regards,
          >> > DlgProc
          >> >
          >> > "GTi" wrote:
          >> >
          >> >> Do anyone have a simple solution how I can send a simple message (int)
          >> >> from a C program to a C# program/service ?
          >> >> Preferred both ways C <-> C#
          >> >>
          >> >> (Windows messages - how ?)
          >> >>
          >> >> It must be extremely easy and in-memory (no disk media).
          >> >>
          >> >> Best regards,
          >> >> Newbie GTi
          >> >>
          >> >>
          >> >>[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          Working...