This example shows how to create a PropertyGrid in which there is a property search system, useful when the object managed by the PropertyGrid is made up of a large number of properties.
There are two paths that can be followed to implement this system:
Ho creato un semplice oggetto COM con il seguente IDL:
// ComObject.idl : IDL source for ComObject // // This file will be processed by the MIDL tool to // produce the type library (ComObject.tlb) and marshalling code. import "oaidl.idl"; import "ocidl.idl"; [ object, uuid(1C0C9AB2-4388-4E1A-A897-D63DD16910AC), dual, nonextensible, pointer_default(unique) ] interface IComClass : IDispatch{ [id(1)] HRESULT CalculateArea(FLOAT a, FLOAT b, [out] FLOAT* area); }; [ uuid(F687E8E6-C714-404B-B946-57DFD49D1D1A), version(1.0), ] library ComObjectLib { importlib("stdole2.tlb"); [ uuid(916BBACF-990F-44DC-80B2-AD2FF5368E57) ] coclass ComClass { [default] interface IComClass; }; };
In questo esempio vi mostro come sia possibile condividere dati tra applicazioni mediante una DLL, detto in altre parole, come sia possibile condividere dati di una DLL tra applicazioni differenti, Maggiori informazioni si possono trovare al seguente link: How do I share data in my DLL with an application or with other DLLs?
#pragma data_seg (".SHARED") int var = 0; // per la condivisione tra processi la variabile delle //essere allocata staticamente ed essere inizializzata #pragma data_seg() extern "C" { bool __declspec(dllexport) __cdecl ReadValue(int* value); bool __declspec(dllexport) __cdecl WriteValue(const int& value); }
// MarshalArrayOfStructures.cpp : main project file. #include "stdafx.h" #include "MyNativeStruct.h" #include "MyManagedStruct.h" #include <stdlib.h> #include <string.h> using namespace System; using namespace System::Runtime::InteropServices; struct MyNativeStruct { public: int myInt; double myDouble; float myFloat; }; [StructLayout(LayoutKind::Sequential)] public value struct MyManagedStruct { public: int myInt; double myDouble; float myFloat; }; array<MyManagedStruct>^ marshalNativeArrayOfMyStruct(const MyNativeStruct* vector, int size) { array<MyManagedStruct>^ result = gcnew array<MyManagedStruct>(static_cast<int>(size)); if (size) { pin_ptr<MyManagedStruct> dest = &result[0]; memcpy(dest, &vector[0], size * sizeof(MyNativeStruct)); } return result; } int main(array<System::String ^> ^args) { MyNativeStruct myNativeStructArray[5] = { { 100, 34.5, 56.4f }, { 200, 66.7, 78.2f }, { 300, 88.9, 8.566f }, { 400, 7438.3, 937589.487f }, { 500, 0.64567, 84677.4887f } }; array<MyManagedStruct>^ myManagedStructArray = gcnew array<MyManagedStruct>(_countof(myNativeStructArray)); myManagedStructArray = marshalNativeArrayOfMyStruct(myNativeStructArray, _countof(myNativeStructArray)); Console::WriteLine(L"Hello World"); return 0; }
Esempio tratto da MSDN How to: Wrap Native Class for Use by C#
// wrap_native_class_for_mgd_consumption.cpp // compile with: /clr /LD #include <windows.h> #include <vcclr.h> #using <System.dll> using namespace System; class UnmanagedClass { public: LPCWSTR GetPropertyA() { return 0; } void MethodB( LPCWSTR ) {} }; public ref class ManagedClass { public: // Allocate the native object on the C++ Heap via a constructor ManagedClass() : m_Impl( new UnmanagedClass ) {} // Deallocate the native object on a destructor ~ManagedClass() { delete m_Impl; } protected: // Deallocate the native object on the finalizer just in case no destructor is called !ManagedClass() { delete m_Impl; } public: property String ^ get_PropertyA { String ^ get() { return gcnew String( m_Impl->GetPropertyA()); } } void MethodB( String ^ theString ) { pin_ptr<const WCHAR> str = PtrToStringChars(theString); m_Impl->MethodB(str); } private: UnmanagedClass * m_Impl; };