Question

I am calling an external method in my C# desktop application

[DllImport(CONST_DLL_PATH)]
internal static extern bool ProcessXML(string inputPath, string outputPath);

I have few questions:

  1. How important is to specify EntryPoint or any other attribute for DllImport?
  2. What if ProcessXML fails, how to get program execution control back from external method?
  3. What are the alternatives of DllImport?

I want to make sure in case of failure of ProcessXML, control should be returned to application.

Was it helpful?

Solution

1) If the method name is different from the DLL's entry's name - you'll have to specify that in the EntryPoint property. Otherwise, you may omit it.

2) Control is returned to you after that method's execution ends. How it handles errors is depend on its implementation, but usually methods returns an error code (in this case, probably true if the execution succeeded, and false if it failed). Once again, it depends on its implementation, but it may set the Win32's "LastError". If it does, you should set the property SetLastError of the DllImport attribute, and if it fails, you may call to Marshal.GetLastError(), or throw Win32Exception.

3) DllImport allows you to consume a method from an unmanaged DLL. You can also wrap it with C++/CLI project, to create managed DLL wrapper, but on most cases that would be an over-kill.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top