Thursday, April 24, 2008

Calling Managed code from Native Code

Subject: Calling Managed code from Native Code

In order to support C++ customer to use ManagedLibraryCE Libray, we have to find a way to call managed code from native code.

There are two possible ways to achieve this.

  1. Mixed Code: CLR options; managed C++ and unmanaged C++.

Use managed C++ to call managed code (ManagedLibraryCE Libray). Basically to create a unmanaged C++ DLL wraper around managed DLL.

  1. COM interface method call.

ManagedLibraryCE Libray COM visible and serves as host, then unmanaged C++ DLL wrapper serves as client.

Mixed Code:

  1. Settings:

Project -> … Properties:

Common Properties->Reference: add ManagedLibraryCE.dll

Configuration Properties->General: Project defaults->Common Language Run Time Support: Common Language Run Time Support (/clr)

  1. Code

#using "ManagedLibraryCE.dll"

using namespace ManagedLibraryCE;

ManagedLibraryObj a;

a.Method();

Problem: there is no “/clr” support for Compact Framework (CF). Managed C++ is not the recommended language for smart device development and Microsoft did not and will not support it. C# and VB.NET are the strongly recommenced languages for CF related development.

Reference:

  1. http://msdn2.microsoft.com/en-us/library/x0w2664k(VS.80).aspx
  2. http://forum.soft32.com/pda/Managed-2005-ftopict56624.html

COM interface method call:

  1. Make managed DLL COM visible

Settings:

Project->…Properties: Build: check “Register for COM interop”.

Code:

namespace ManagedDLL

{

public interface ICalculator

{

int Add(int n1, int n2);

}

public class ManagedClass:ICalculator

{

public int Add(int n1, int n2)

{return n1 + n2;}

}

}

Strong name and ComVisible:

To create a strong name for your class library, type the following command at the Visual Studio .NET command prompt: sn.exe -k MyKeyFile.SNK

Copy the MyKeyFile.SNK file to your project folder.

Double-click the AssemblyInfo.cs file to open the file in Solution Explorer.

Replace the following lines of code in the AssemblyInfo.cs file

               [assembly: ComVisible(false)]
               [assembly: AssemblyDelaySign(false)]
               [assembly: AssemblyKeyFile("")]

with the following.

               [assembly: ComVisible(true)] 
               [assembly: AssemblyDelaySign(false)] 
               [assembly: AssemblyKeyFile("..\\..\\MyKeyFile.SNK")]
  1. Calling the Managed DLL from Native C++ Code

Code:

#import "C:\My Projects\Prac\CallingManagedFromUnmanaged\COM\PC\ManagedDLL\ManagedDLL\bin\Debug\ManagedDLL.tlb" raw_interfaces_only

using namespace ManagedDLL;

//initialize COM

HRESULT hr = CoInitialize(NULL);

//create the interface pointer

ICalculatorPtr pICalc(__uuidof(ManagedClass));

long lResult = 0;

//call the Add method

pICalc->Add(5, 10, &lResult);

CString aa;

aa.Format(L"%d", lResult);

MessageBox(aa, L"", MB_OK);

//UnInitized COM

CoUninitialize();

Reference:

  1. http://support.microsoft.com/kb/828736
  2. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkCOMInteropPart2CServerTutorial.asp
  1. Problem:

For Compact Framework (CF), there is no registration or activation support to make this work.

“There is limited Com Callable Wrappers (CCW) support in CF 2.0. However, there is no registration or activation support in CF 2.0.

What this means is that a managed application can pass managed interfaces to native code, and the native code can treat these interfaces as COM Interface Pointers to make calls back into the managed code. Additionally, CF 2.0 supports passing managed delegates to native code as function pointers which native code can used to call back into managed code. However, there is no support for a purely native app to call into managed code. All pointers need to be passed from managed code to native code before any calls can be made back into managed code.”

---Mike Hall, Microsoft CF team (http://blogs.msdn.com/mikehall/archive/2005/01/26/360936.aspx )

Reference:

a. http://blogs.msdn.com/mikehall/archive/2005/01/26/360936.aspx
b. http://blogs.msdn.com/netcfteam/archive/2005/07/24/442612.aspx

No comments: