-3

packetlistener.exe: I have a c application that actively listens and receives packets.

comwrapper.dll: A C++ COM dll that wraps the unmanaged c executable

guiapp.exe: A .NET gui that references the comwrapper.dll and uses it to display information and handle events.

The way I am currently launch the application:

  1. Start packetlistener.exe with command line argument.
  2. Start guiapp.exe

I have spent the last couple of hours trying to figure out how to package this nicely into one process.

What I have tried to do is compile packetlistener.exe as a dll and then include it in the guiapp project. However, I am completely lost regarding starting packetlistener as part of guiapp.

I am new to windows development and feel like I am doing something wrong. Am I going about this the wrong way?

lpacyk
  • 1
  • 2
  • If you have the source code of the C-program either create a DLL out of it. Write a COM interface to it, or embed it into a C++/CLI code and use it directly as a .NET assembly. But what is your question? JUst having one executable? Directly use the code without having a separate process? – xMRi Oct 06 '17 at 05:51
  • I would like to just have one executable without two separate processes. The C program runs as an executable and the COM interface is able to query it for data and send commands but only if the executable is already running. Can I convert the C application into a DLL and then start it from within my C# program? The c program is actually open sourced by apple. https://opensource.apple.com/source/mDNSResponder/mDNSResponder-765.50.9/ – lpacyk Oct 06 '17 at 17:34

2 Answers2

0

You can start a process from a .net application using Process.Start:

Process.Start("packetlistener.exe", "-arg1 -arg2");
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Does this requires "packetlistener.exe" to be in a separate file? Is it possible to build packetlistener.exe as an dll and then start it with .net? – lpacyk Oct 06 '17 at 01:22
  • Yes. If you use process.start, it needs to be a separate exe on disk. – John Koerner Oct 06 '17 at 01:23
  • I've just done this and it opens a seperate cmd prompt. I was wondering how I can merge these into one application. – lpacyk Oct 06 '17 at 01:25
  • There are lots of options to control how Process.Start works. Read the [ProcessStartInfo](https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx) docs. You can launch the process and not show a window and redirect the output to your application. If you want to try and consume the library via COM, you can try that, but COM does add complexity. – John Koerner Oct 06 '17 at 01:28
  • I have a COM that interacts with "packetlistener.exe" but the COM itself does not start it. Which is the main problem. In fact packetlistener.exe used to be a service that I am now running as a console application – lpacyk Oct 06 '17 at 01:34
0

So I ended up figuring out what I wanted to do.

I compiled my C application into a DLL and exposed a function that would get it started.

I then used PInvoke to call that function in a separate thread so that it can start gathering and sending packets.

The COM DLL magically seems to referrence the C program thats running in its own thread.

Now since I am unable to PInvoke a DLL thats embedded I had to unpack it in a temp file you can learn about that in here.

How can I extract a file from an embedded resource and save it to disk?.

In the end I was left with one executable.

lpacyk
  • 1
  • 2