Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I finally found the way. I created a new AppDomain in wich i ran the method to get the cpu serial and then unloaded that domain so the System::Management library that was causing problems got unloaded.</p> <p>ref class MarshalByRefType : MarshalByRefObject { public: // Call this method via a proxy.</p> <pre><code>std::string GetCPUID() { //char * chp = (char *) System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(R671::R671::Value()).ToPointer(); std::string lsCPUID = ""; //return lsCPUID.assign(chp); try { System::Management::ManagementObjectCollection^ moReturn = nullptr; System::Management::ManagementObjectSearcher^ moSearch ; moSearch = gcnew System::Management::ManagementObjectSearcher("Select * from Win32_Processor"); moReturn = moSearch-&gt;Get(); for each ( System::Management::ManagementObject^ mo in moReturn ) { char * chp = (char *) System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(mo["ProcessorID"]-&gt;ToString()).ToPointer(); lsCPUID.assign(chp); } } catch(System::Exception^ ex ) { } AppDomainSetup^ ads = AppDomain::CurrentDomain-&gt;SetupInformation; String^ str = String::Format("AppName={0}, AppBase={1}, ConfigFile={2}", ads-&gt;ApplicationName, ads-&gt;ApplicationBase, ads-&gt;ConfigurationFile); char * chp = (char *) System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str).ToPointer(); lsCPUID.assign(chp); return lsCPUID; } </code></pre> <p>}</p> <pre><code>class Loader { std::string GetCPUID() { String^ callingDomainName = Thread::GetDomain()-&gt;FriendlyName; // Construct and initialize settings for a second AppDomain. AppDomainSetup^ ads = gcnew AppDomainSetup(); ads-&gt;ApplicationBase = "file:///" + Assembly::GetExecutingAssembly()-&gt;Location; ads-&gt;DisallowBindingRedirects = false; ads-&gt;DisallowCodeDownload = true; ads-&gt;ConfigurationFile = AppDomain::CurrentDomain-&gt;SetupInformation-&gt;ConfigurationFile; // Create the second AppDomain. AppDomain^ ad2 = AppDomain::CreateDomain("AD #2", AppDomain::CurrentDomain-&gt;Evidence, ads); String^ sam = R671::R671::typeid-&gt;FullName; // Create an instance of MarshalbyRefType in the second AppDomain. // A proxy to the object is returned. MarshalByRefType^ mbrt = (MarshalByRefType^) ad2-&gt;CreateInstanceFromAndUnwrap( Assembly::GetExecutingAssembly()-&gt;Location, MarshalByRefType::typeid-&gt;FullName ); string lsCPUID; //char * chp = (char *) System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(mbrt2-&gt;Value()).ToPointer(); lsCPUID = mbrt-&gt;GetCPUID(); try { AppDomain::Unload(ad2); } catch ( AppDomainUnloadedException^ /*e*/ ) { } return lsCPUID; } void Load() { //do things std:string cpuid = this-&gt;GetCPUID(); //do things //load c# library and open forms } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload