开发者

Generating a C++ executable from a C# application

I have a WCF service accepting requests from our clients. After analyzing the request I need to generate (compile开发者_如何学编程 + link) C++ EXE. What is the best method to create an C++ EXE from a C# application???

10x


I can only guess what it is you want, but I assume your requirements are something like:

  • You run a WCF service on a server somewhere.
  • Upon receiving a certain call, the service must output a (binary) executable, based on the parameters it receives.
  • C++ should be used as the intermediate language.

Let's look at what you need. The obvious first requirement is a C++ compiler/linker that can be invoked programmatically. On Unix systems, that would be g++, and you can simply shell-out to invoke it; on Windows, g++ is also available (under the name MinGW), but the version is pretty outdated, so you might be better off using Microsoft's command-line C++ compiler.

Obviously, you'll also need to generate C++ source code somewhere; I assume most of the source code is more or less the same for each request, so you probably need some sort of templating system. If it's not too complicated, this can be as simple as running a regex search-and-replace over a bunch of template files; otherwise, you need a proper templating language (XSLT is built into .NET, although the syntax takes some getting used to).

And then the glue to make it work together; I suggest something like this:

  • Read request and create a suitable data structure (in a format that your template engine can consume)
  • Pass the data to the template engine, writing the output files to a temporary folder
  • Invoke the compiler on the temporary location
  • Read the executable back in, send it to the client
  • Delete the temporary folder

Since compiling is often a costly operation, consider caching the generated executables (unless they are practically guaranteed to be different every time).

By the way, there is one big caveat: If the client platform is not binary-compatible with the server platform (e.g., the server is running on x64 but the client is x86), the generated executable might not work.

And another one, which is security: By hacking the server, or tricking clients into sending "wrong" requests, an attacker can potentially malicious code through the generated executable; if this application is all but super-trivial, I imagine it's going to be pretty hard to properly secure this thing.


An executable is an executable, and is defined by the abiltity to be executed.

Whatever programming language was, once upon a time, used to write the source code that was fed to a compiler which produced the executable, that no longer matters. An executable looks the same regardless of which language (or languages) you used. (A .NET executable is just an executable with some fairly complex DLL dependencies)

So there is no such thing as a "C++ executable". Perhaps you mean an executable that doesn't depend on the .NET framework?

Or do you simply mean that you have a C++ application that needs to use a WCF service?

Or that you want to rewrite your C# code as C++?


Do you mean you want to compile c# to native machine code? in which case ngen may be some use

http://msdn.microsoft.com/en-us/library/6t9t5wcf%28v=vs.80%29.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜