I have a C ++ project in which I need to make selections from WMI. This is much easier with C #, so I would like to do it with it. In essence, I’ll have to pass in lines containing the computer name, namespace, class and field name (3 - 4 for each class), and return the container pairs of type <field name, value>. What is the best way to do this? Is it possible to write a library in C # and use it in C ++? It is unlikely that you can use C ++ / CLI, you will have to rewrite too much.
- oneThis is possible: bbs.vbstreets.ru/viewtopic.php?f=2&t=44349 (for C # module turns into a static class), to transfer strings, you will most likely need to send a pointer to a char using an unsafe code on a sharpe, or bstr with MarshalAs (did not check any option). But I would recommend thinking about writing a dll in c ++ / cli. - Qwertiy ♦
- oneAnd in my opinion it is a perversion. Firstly, WMI takes information either from the registry or from firmware tables, with which you can work from C without problems. Meaning to hang on abstraction another level of abstraction. Secondly, you can just parse the output of wmic, for that matter. - MSDN.WhiteKnight
- one@VadimTagil: Working at the WinAPI level is quite a challenge. Manual memory management and verification of error codes are simple but rather tedious activities that require increased attention and concentration. The interface to WMI in C # is much nicer. - VladD
|
2 answers
I think the easiest way is to write C # code, and a small wrapper on C ++ / CLI.
Example. Let you have a code on C #
public class QueryLibrary { public Dictionary<string, string> RunQuery(string machineName, string className) { ... } } You create a wrapper on C ++ / CLI:
#include "stdafx.h" #include <string> #include <map> #include <msclr\marshal.h> #include <msclr\marshal_cppstd.h> using namespace System; using namespace System::Collections::Generic; using namespace msclr::interop; std::map<std::wstring, std::wstring> RunQuery( const std::wstring& machineName, const std::wstring& className) { QueryLibrary^ ql = gcnew QueryLibrary(); String^ machineNameMng = marshal_as<String^>(machineName); String^ classNameMng = marshal_as<String^>(className); Dictionary<String^, String^>^ result = ql->RunQuery(machineNameMng, classNameMng); std::map<std::wstring, std::wstring> resultMap; for each (KeyValuePair<String^, String^>^ kvp in result) { std::wstring key = marshal_as<std::wstring>(kvp->Key); std::wstring value = marshal_as<std::wstring>(kvp->Value); resultMap.insert(std::make_pair(key, value)); } return resultMap; } Now you get a “normal” function with a native signature, you can use it in C ++ code.
|
You can write a console application in C #, then call it from C ++ code, passing the necessary arguments and reading the results of the program.
Well, it looks like a crutch decision.
|