Which approach is better: Process.Start or call the DLL directly?

In our team, we were faced with a choice: we need to call external third-party code and process its output from our C # code.

Third-party code is available in two forms: a set of dll and one exe file (which probably calls these dll ). And possible approaches may be as follows: use the Process.Start to run the executable file and get its output. And one more thing is to call the dll directly.

I'm trying to figure out which approach we should use.

On the one hand, calling an executable file is simple, but on the other, it does not feel reliable.

On the one hand, calling dll seems to be the more correct way to complete the task, and on the other, it can be a very difficult task to provide C# binding for all functions that we have in native C code.

But I need to conduct a more substantial analysis on this topic in order to make a final decision. I was faced with the same question before someone, perhaps you could share your location.

It would be very helpful!

EDIT . I am talking about video conversion in this particular case. I need to get a video stream from a user and convert it to one video format for everyone. To complete the task, you can call ffmpeg , and everything is fine until something happens, and I need to either restart the encoding or take some action. I could not estimate how long it would take, and if I need to convert several videos in parallel ffmpeg will not be as flexible as I plan ...

At least I see it now. There may be more problems logging in.

+7
source share
6 answers

There are several considerations:

  • Do you have a source for dll?
  • How many are you going to call these DLLs?
  • How complicated are the DLL APIs and your use?

Depending on the answers.

Create bindings if:

  • You often call DLLs. Direct calling is much faster.
  • You have a source and check how good they are. Otherwise, you may have problems with memory leaks, calls, etc.
  • DLL APIs are not too complicated, so you do not need to send C ++ objects to them, etc. Or implant a lot of work already done in exe.

Use executable files:

  • If you need to run them only occasionally. The overhead of creating another process is not important to you.
  • If you are not sure about the quality of the code. It will be much safer and more reliable for your code, and not for loading incorrectly implemented DLLs. You can always try to run .exe several times if a problem occurs. But this dll crashes your application, you can not do anything.
  • If the API is very complex and has a lot of functionality, you will have to redefine it.
+4
source

I would say that this will depend on what level of detail will be expected from your code in terms of supporting api from the library.

If the executable encapsulates workflows well enough for you, you can take advantage of the simplicity of invoking the executable.

Also, since you mention that this is native C code, adding a link to a DLL means dealing with unmanaged code, which I personally will not continue if there is no choice for me.

0
source

If the dll is well written and does not have memory leaks, it is better to use a DLL, since it does not require additional costs for creating a process.

0
source

I would say that it all depends on your requirements, the time frame, how stable the output of your exe and how easily it can be analyzed. Both methods are doable.

For example, Mercurial believes its console output is the primary way to interact with it, even if it can directly use its Python code.

On the other hand, calling C functions from C # is pretty simple, so this may be an option. If you, however, need to map hundreds of C functions, you should ask yourself if you have time for this.

0
source

EXE There is one main entry that you call, so you cannot directly access functions. When you call exe, a new process will be created. The input stream is called in the context of the main thread of this process.

DLL Increases flexibility by calling functions directly; there is an entry point for each function; the system loads the DLL into the context of an existing thread

therefore, a DLL call is much better for compute resources and provides greater flixibility. saying that you can call a DLL from managed and unmanaged code, and you can call a managed and unmanaged dll from C #

If the DLL has an interface, you can add refrence directly, and if you still have no way to call it, as shown below

  [DllImport(@"TestLib.dll")] public static extern void InitParam([MarshalAs(UnmanagedType.LPWStr)] string inputFile, [MarshalAs(UnmanagedType.LPWStr)] string outputFile, [MarshalAs(UnmanagedType.LPWStr)] string templateFile, [MarshalAs(UnmanagedType.LPWStr)] string userName, [MarshalAs(UnmanagedType.LPWStr)] string manifestFilePath, [MarshalAs(UnmanagedType.LPWStr)] string usersRightList); 

in simple words, you import the DLL and map the parameters to .net types with sorting

0
source

The answer depends on how the external application uses the DLL .:

  • Calling exe if it calls several dll functions several times and its business process is large and complex - you do not want to redefine all exe logic in C # code.
  • A direct call to dll, if exe calls only one or two functions from the dll, the order and parameters of the call are well known or even absent.

In general, I would prefer to directly contact the dll, because it eliminated a lot of overhead and possible problems with creating a new process and processing its output. And don't be afraid of your own code if your dll functions are simple, and then with PInvoke you can easily call these functions.

0
source

All Articles