Search code examples
c#wcfvisual-c++dllimport

C# WCF web service to C++ DLL calls


There's a C++ legacy DLL that's build in Visual Studio 2015 and am working on exposing some of the C++ functions as REST APIs. For this purpose, created a WCF Web serivice project in the same solution and trying to call C++ functions via DllImport.

[DllImport("ExtOutPutGenerator.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int CreateFrameGenerators(string node, string axis);

In the C++ code the above function is declared as,

extern "C" __declspec(dllexport) int CreateFrameGenerators(const char* node, const char* axis);

Now when i call the C++ function via DllImport, it's not working.

i.e., when i call the REST API via the browser it gives the below message.

The server encountered an error processing the request. See server logs for details.

I know for sure that the failure is happening when it calls the C++ function from inside C#. Not sure where to find the server log.

For testing purpose, I created a sample C++ test DLL in same solution and then called it's function via DllImport and that's working fine.

So the problem seems to be with the legacy C++ DLL.

I have no idea what else to look for or how to troubleshoot.

So my question is,

  1. What could well be the problem with the legacy C++ DLL that's causing this issue? Any particular project settings?

  2. Is there another way to achieve this without using the DllImport? Something simpler and cleaner and might work on all C++ projects?


Solution

  • Assuming you got all the coding part correct, the other important thing to check is the target platform. Both C++ & C# needs to be build for the same platform.

    For e.g., if C++ DLL is set to x64 then C# also should be build with x64.