My Blog

lupok
My Blog
RssIcon

Configuring Properties with Fluent API (Code First)

by host on giovedì 26 marzo 2015 11:49
https://msdn.microsoft.com/en-us/library/hh295844(v=vs.103).aspx

Caricare oggetti COM non registrati

by host on martedì 16 settembre 2014 16:03

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
	{
		[defaultinterface IComClass;
	};
};
 

Condividere dati tra applicazioni mediante DLL

by host on domenica 14 settembre 2014 09:00

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);
}

Marshal native struct array to managed struct array

by host on lunedì 28 luglio 2014 07:25
// 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 MyNativeStructvectorint 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;
}

How to: Wrap Native Class for Use by C#

by host on giovedì 12 giugno 2014 15:39

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;
};

ThreeJS QuoteHelper

by host on lunedì 9 giugno 2014 00:13
ThreeJS QuoteHelper is a utility for displaying the size of an 3d object in technical drawing style

 

 

ThreeJS Collada loader

by host on lunedì 21 aprile 2014 07:14

Magic enum converter

by host on mercoledì 9 aprile 2014 13:12
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
 
namespace MyMagicEnumConverter
{
   #region MyMagicEnumConverter
   public class MyMagicEnumConverter<TENUM, TATTR> : 
      StringConverter where TATTR : MyNameBaseAttribute
   {
 
      public override bool GetStandardValuesSupported(
         ITypeDescriptorContext context)
      {
         return true;
      }
 
      public override bool GetStandardValuesExclusive(
         ITypeDescriptorContext context)
      {
         return true;
      }
 
      public override StandardValuesCollection GetStandardValues(
         ITypeDescriptorContext context)
      {
         List<String> values = new List<String>();
 
         foreach (var item in Enum.GetNames(typeof(TENUM)))
         {
            System.Reflection.MemberInfo[] mi = typeof(TENUM).GetMember(item);
 
            Object[] attributes = mi[0].GetCustomAttributes(typeof(TATTR), false);
 
            if (attributes.Length > 0)
               values.Add(((TATTR)attributes[0]).Name);
            else
               values.Add(item);
         }
 
         return new StandardValuesCollection(values);
      }
 
      public override bool CanConvertFrom(ITypeDescriptorContext context, 
         Type sourceType)
      {
         if (sourceType == typeof(String))
            return true;
 
         return base.CanConvertFrom(context, sourceType);
      }
 
      public override object ConvertFrom(ITypeDescriptorContext context, 
         System.Globalization.CultureInfo culture, object value)
      {
         if (value != null && value.GetType() == typeof(String))
         {
            foreach (var item in Enum.GetNames(typeof(TENUM)))
            {
               System.Reflection.MemberInfo[] mi = typeof(TENUM).GetMember(item);
 
               Object[] attributes = mi[0].GetCustomAttributes(typeof(TATTR), false);
 
               if (attributes.Length > 0)
               {
                  if (Convert.ToString(value) == ((TATTR)attributes[0]).Name)
                     return Enum.Parse(typeof(TENUM), item);
               }
            }
 
            return Enum.Parse(typeof(TENUM), Convert.ToString(value));
         }
 
         return base.ConvertFrom(context, culture, value);
      }
 
 
      public override bool CanConvertTo(ITypeDescriptorContext context, 
         Type destinationType)
      {
         if (destinationType == typeof(TENUM))
            return true;
 
         return base.CanConvertTo(context, destinationType);
      }
 
 
      public override object ConvertTo(ITypeDescriptorContext context, 
         System.Globalization.CultureInfo culture, object value, Type destinationType)
      {
         if (value != null && value.GetType() == typeof(TENUM))
         {
            System.Reflection.MemberInfo[] mi = 
               ((TENUM)value).GetType().GetMember(value.ToString());
 
            Object[] attributes = mi[0].GetCustomAttributes(typeof(TATTR), false);
 
            if (attributes.Length > 0)
               return ((TATTR)attributes[0]).Name;
            else
               return Convert.ToString(value);
         }
 
         return base.ConvertTo(context, culture, value, destinationType);
      }
 
   }
   #endregion
}

Tags