What I want is simple - have C++ appl开发者_开发技巧ication that would be compiled with static runtime ( /MT
, /MTd
flags ) and be capable to open, call, etc classes and thare functions from DLLs (using LoadLibrary, C++). Is such thing possible?
It is not impossible. But making it universal and reliable is a very long distance shot. There is no standardized metadata format in C++ that lets you know that you are passing the correct arguments. Even the name of the exported class or function isn't easily guessable, it is a compiler implementation detail.
COM Automation would be an example where these problems are solved. Covered by the ActiveX Test Container. Or the reflection support in languages like Java or .Net managed languages. Not C++.
I think you're mixing things up. Just because it's called a "static" runtime, it just means that the code for standard C and C++ libraries is statically linked into your application. In general, this will never put any limitations on what your application can do.
All of the standard library classes like ifstream
and functions like printf
will be usable whether their code is inside your EXE in the static runtime, or outside your EXE in the dynamic runtime. All of the Win32 functions like LoadLibrary
and GetProcAddress
are always outside of your application in regular Windows DLLs (like Kernel32.dll
), so they certainly aren't affected by your runtime choice.
I'd say that your real problem is trying to call C++ methods using GetProcAddress
. That Win32 API is only meant for dynamically calling C functions. The first problem that you'll hit is that you won't be able to find names of methods due to C++'s name mangling. You might also have problems allocating objects. My intuition tells me that it would be almost impossible to get working correctly.
Here are three suggested alternatives in place of directly calling C++ methods using GetProcAddress
:
- Wrap your C++ methods in C functions.
- Use a C++ framework like COM or Qt plugins to set up dynamic DLL interfaces.
- Use a different platform such as .NET or Java; one that has full support for object-oriented reflection and dynamic invocation.
精彩评论