简体   繁体   English

C# WCF Web 服务到 C++ DLL 调用

[英]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.有一个在 Visual Studio 2015 中构建的 C++ 旧版 DLL,并且正在将一些 C++ 函数公开为 REST API。 For this purpose, created a WCF Web serivice project in the same solution and trying to call C++ functions via DllImport.为此,在同一个解决方案中创建了一个 WCF Web 服务项目,并尝试通过 DllImport 调用 C++ 函数。

[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,在 C++ 代码中,上述函数被声明为,

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.现在,当我通过 DllImport 调用 C++ 函数时,它不起作用。

ie, when i call the REST API via the browser it gives the below message.即,当我通过浏览器调用 REST API 时,它会给出以下消息。

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#.我确信当它从 C# 内部调用 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.出于测试目的,我在同一个解决方案中创建了一个示例 C++ 测试 DLL,然后通过 DllImport 调用它的函数,并且工作正常。

So the problem seems to be with the legacy C++ DLL.所以问题似乎出在旧的 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?导致此问题的遗留 C++ DLL 的问题可能是什么? Any particular project settings?任何特定的项目设置?

  2. Is there another way to achieve this without using the DllImport?有没有另一种方法可以在不使用 DllImport 的情况下实现这一点? Something simpler and cleaner and might work on all C++ projects?一些更简单、更清晰并且可能适用于所有 C++ 项目的东西?

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. C++ 和 C# 都需要为同一平台构建。

For eg, if C++ DLL is set to x64 then C# also should be build with x64.例如,如果 C++ DLL 设置为 x64,那么 C# 也应该使用 x64 构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM