青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 17,  comments - 2,  trackbacks - 0

Calling Managed Code from Unmanaged Code and vice-versa

By TarunNeo

This article shows you how to call managed code from unmanaged code and also the other way round.
C++/CLI, VB, VC7.1, C++, Windows, .NET, .NET 1.1VS.NET2003, Visual Studio, Dev

Posted21 Mar 2005
Updated21 Mar 2005 
Views78,822
Bookmarked50 times
22 votes for this Article.
Popularity: 5.47 Rating: 4.07 out of 5
0 votes, 0.0%
1
1 vote, 4.5%
2
3 votes, 13.6%
3
8 votes, 36.4%
4
10 votes, 45.5%
5

Introduction

.NET framework is one of the better development and execution environments for software these days. But there is a very huge amount of software components already developed and being developed out of unmanaged code. So, there needs to be an easy way for managed code to call the unmanaged code and the other way round.

I have seen some articles on this, but I did not find them giving a complete solution of what I was looking for. So here is one.

Background

Microsoft lets you call COM code from .NET code using RCW (Runtime Callable Wrappers). The RCW, a managed wrapper code, wraps the COM component. The .NET code then interacts with the RCW which in turn interacts with the COM component inside it. The reverse communication can be done using CCW (COM callable wrapper).

This article shows a way of manually creating a wrapper. It was fairly easy to call the unmanaged code from the managed code but not the other way around.

Code

The code that I have specified below consists of:

  • Unmanaged class: UnManaged_Class
  • Managed Wrapper class: Managed_Wrapper_Class

This class wraps the unmanaged class. This means that it “contains” an object of the unmanaged type which it uses to call the exposed methods in the unmanaged type.

  • Managed code: Managed_Class

This is how the managed code calls the unmanaged code:

For every exposed method in the unmanaged class, there should be a corresponding method in the Managed_Wrapper_Class. The managed code instantiates an object of theManaged_Wrapper_Class and calls the exposed methods in that class using this instance. These methods in the Managed_Wrapper_Class then call the corresponding methods in the unmanaged code. This is done using pInner as shown in the code:

//

/*//////////////////////////////////////////////////
//Unmanaged_Class.cpp
//////////////////////////////////////////////////*/


#ifndef UNMANAGED
#define UNMANAGED

class Unmanaged_Class
{
public:

    Unmanaged_Class();
    
    /*This is the method that is to be called from Managed code*/
    void methodToBeCalledInUnmanaged(int data);
};

#endif


*//////////////////////////////////////////////////

//Unmanaged_Class.cpp

//////////////////////////////////////////////////*/


#include "StdAfx.h"

#using <mscorlib.dll>
#include "Unmanaged.h"


Unmanaged_Class::Unmanaged_Class()
{
}

void Unmanaged_Class::methodToBeCalledInUnmanaged(int data)
{
    /*Here is the place where the Managed Wrapper code is called. */
    scallback(data+1);
}


/*//////////////////////////////////////////////////
//Managed_Wrapper.h 
//////////////////////////////////////////////////*/

#pragma once

#include "stdafx.h"

#using <mscorlib.dll>
#include "Unmanaged.h"


using namespace System::Runtime::InteropServices;
using namespace System;

namespace Managed_Wrapper
{

    /*Managed Wrapper Class */
    public __gc class Managed_Wrapper_Class
    {
    public: 
    
        //constructor

        Managed_Wrapper_Class();

        /* pInner is used to invoke the exposed 
        methods in the unmanaged class. */
        Unmanaged_Class * pInner; 


        /* An exposed function corresponding 
        to the exopsed function in Unmanaged*/
        void CallUnmanaged(int data);

    };
}

/*//////////////////////////////////////////////////
//Managed_Wrapper.cpp
//////////////////////////////////////////////////*/

#include "stdafx.h"

#include "Managed_Wrapper.h"

#using <mscorlib.dll>

namespace Managed_Wrapper
{
    Managed_Wrapper_Class::Managed_Wrapper_Class(void)
    {
        /* define the pInner object */
        pInner = new Unmanaged_Class();
    }


    void Managed_Wrapper_Class::CallUnmanaged(int data)
    {
        pInner->methodToBeCalledInUnmanaged (data);
    }

}
'/*//////////////////////////////////////////////////

'//Managed Code

'//VB.NET code

'//////////////////////////////////////////////////*/



'Import the Managed Wrapper Namespace

Imports Managed_Wrapper


'Create an instance of the Managed Wrapper class.

Dim forwardCaller As Managed_Wrapper_Class = New Managed_Wrapper_Class


'To call a method in the Managed_Wrapper_Class. This method in 

'turn will call the method in the unmanaged code

forwardCaller.CallUnmanaged(nudNumber.Value)

This was the easy part. Now is the hard part. Calling managed code from the unmanaged code.

The unmanaged code has a function pointer. The address of the function pointer is the address of a method in the managed wrapper class. Also, the function pointer is initialized by the wrapper class and not in the unmanaged code. So this way, when the function pointer is called, the method in the managed wrapper code is called. Half of the task is done. The way of assigning the function pointer in the unmanaged code is not easy because the function has to point to a method which is managed. So, we use the wrapper delegate struct as shown in the code. Then convert this delegate struct to a function pointer of unmanaged type using Marshal::StructureToPtr (_Instance_Of_Delegate_Wrapper, &type_unmanaged_functionptr, false);

The managed wrapper code declares a delegate (a .NET way of a function pointer). The delegate is instantiated by the managed code. So when the method in the managed wrapper class is called (by the unmanaged code), it in turn calls the delegate in the same class (which is initialized by the managed code). As the delegate points to a function in the managed code, the method in the managed code gets called. This was the hard part.

/*///////////////////////////////////////////////////
/*Unmanaged_Class.h */
///////////////////////////////////////////////////*/


#ifndef UNMANAGED
#define UNMANAGED

#using <mscorlib.dll>

typedef void (*w_CallBack) (int status);

class Unmanaged_Class
{
public:

 Unmanaged_Class();
 w_CallBack scallback;

 /* To set the callback function. The address in ptr2F will be the
 address of a method in the Managed Wrapper class and will be assigned
 there. In this case it will be address of ActualMethodInWrapper(int );*/
 void setCallBackInUnmanaged(w_CallBack ptr2F);

 /*This is the method that is to be called from Managed code*/
 void methodToBeCalledInUnmanaged(int data);
};

#endif
/*///////////////////////////////////////////////////
//Unmanaged_Class.cpp
///////////////////////////////////////////////////*/

#include "StdAfx.h"

#using <mscorlib.dll>
#include "Unmanaged.h"


Unmanaged_Class::Unmanaged_Class()
{
}

void Unmanaged_Class::setCallBackInUnmanaged(w_CallBack ptr2F)
{
 /*scallback now points to ActualMethodInWrapper(int) in
 Managed_Wrapper_Class*/
 scallback = ptr2F;
}

void Unmanaged_Class::methodToBeCalledInUnmanaged(int data)
{
 /*Here is the place where the Managed Wrapper code is called. */
 scallback(data+1);
}
/*///////////////////////////////////////////////////
//Managed_Wrapper.h
///////////////////////////////////////////////////*/

#pragma once

#include "stdafx.h"

#using <mscorlib.dll>
#include "Unmanaged.h"


using namespace System::Runtime::InteropServices;
using namespace System;

namespace Managed_Wrapper
{

 /*Declare a delegate. It is to be invoked from the unmanaged code*/
 public __delegate void CallbackDelegate(int data);

 /* Declare a wrapping struct that wraps an object of the above 
 declared delegate. The delegate that this struct contains will 
 point to a method that will be called when this delegate is 
 invoked from the unmanaged code*/
 [StructLayoutAttribute( LayoutKind::Sequential, CharSet = CharSet::Ansi )]
 public __gc struct Managed_Delegate_Wrapper
 {
 [MarshalAsAttribute(UnmanagedType::FunctionPtr)]
 CallbackDelegate* _Delegate;
 };

 /*Managed Wrapper Class */
 public __gc class Managed_Wrapper_Class
 {
 public:

 //constructor

 Managed_Wrapper_Class();

 /* pInner is used to invoke the exposed methods in the unmanaged class. */
 Unmanaged_Class * pInner;

 /* Declare an instance of the wrapping struct */
 Managed_Delegate_Wrapper * _Status_Delegate;

 /* A method that will be called when the callback function in the unmanaged
 code is called. It is this method who’s pointer is passed to the unmanaged
 code.*/
 void ActualMethodInWrapper(int );

 /*A delegate type. To be used for calling managed code from here.*/
 __delegate int statusDelegate(int status);

 /*An object of the above delagate type is declared.
 It will be initialized in the managed code.*/
 statusDelegate *statD;

 /* An exposed function corresponding to the exopsed function in Unmanaged*/
 void CallUnmanaged(int data);

 };
}
/*///////////////////////////////////////////////////
//Managed_Wrapper.cpp
///////////////////////////////////////////////////*/

#include "stdafx.h"

#include "Managed_Wrapper.h"

#using <mscorlib.dll>

namespace Managed_Wrapper
{
 Managed_Wrapper_Class::Managed_Wrapper_Class(void)
 {
 /* define the pInner object */
 pInner = new Unmanaged_Class();

 /* define the wrapping struct instance declared in Managed_Wrapper_Class */
 _Status_Delegate = new Managed_Delegate_Wrapper();

 /* This is the actual delegate that is contained in the wraping struct */
 _Status_Delegate->_Delegate = 
   new CallbackDelegate(this, &Managed_Wrapper_Class::ActualMethodInWrapper);

 /* declare a function pointer of the same type as in unmanaged code */
 w_CallBack callback;

 /*convert the wrapping struct to a function pointer of the above type. */
 Marshal::StructureToPtr (_Status_Delegate, &callback, false);

 /* set this function pointer in the unmanaged code using pInner.*/
 pInner->setCallBackInUnmanaged(callback);
 }

 /*This is the method in the Managed_Wrapper_Class that is called
 when the function pointer in the unmanaged code is called.*/
 void Managed_Wrapper_Class::ActualMethodInWrapper(int status)
 {
 /*This method in turn calls the delegate in the managed code.
 The method that statD is actually poiting is specified in the
 managed code itself.*/
 statD(status);
 }

 void Managed_Wrapper_Class::CallUnmanaged(int data)
 {
 pInner->methodToBeCalledInUnmanaged (data);
 }

}
'/*//////////////////////////////////////////////////

'//Managed Code

'//VB.NET code

'//////////////////////////////////////////////////*/


'Import the Managed Wrapper Namespace

Imports Managed_Wrapper

'Create an instance of the Managed Wrapper class.

Dim forwardCaller As Managed_Wrapper_Class = New Managed_Wrapper_Class

'Create an instance of the delegate declared in the Managed Wrapper

'class. Initialize it with the address of the method that is supposed

'to be called when the delegate in the Managed_Wrapper_Class is called

Dim statDelg As New Managed_Wrapper_Class.statusDelegate(AddressOf status_Recd)

'This function gets called when the unmanaged code calls the

'managed wrapper code and which in turn calls the the delegate

'in there

Public Function status_Recd(ByVal status As Integer) As Integer

 'use status for something now. It took so much effort to get it :)

 MessageBox.Show("Status received is " + status.ToString(), "Status" + 
                 " received from the Unmanaged code")

End Function

 'To call a method in the Managed_Wrapper_Class. This method in

 'turn will call the method in the unmanaged code

 forwardCaller.CallUnmanaged(nudNumber.Value)

 'statD is called from the managed code. And statD in turn

 'will call the method status_Recd

 forwardCaller.statD = statDelg

Compiling and Linking

Choose a C++ .NET Class Library project to wrap your unmanaged code and compile it to generate the DLL. Then in your VB.NET code, add a reference to this DLL using Add Reference->Projects (Browse to the DLL). Also you would need to have your Project properties as in the demo project on the top.

Demo

Open the Managed_VBdotNET.sln solution and start it. Bingo.

Summary

I found this technique particularly useful for one of my projects in which I had some code which was already written in C++ and was a good idea to have it written in C++. I needed to add a GUI to it, for which VB.NET was a very straightforward choice. Through this way I could invoke C++ methods through VB.NET and VB.NET methods through C++.

Any suggestions are welcome and will be appreciated. Please feel free to ask any questions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

TarunNeo


Tarun is a Computer Science Grad. He believes in
".......In between and after is glorious coding"


Occupation:Web Developer
Location:United States United States
posted on 2008-11-14 11:40 BeyondCN 閱讀(1054) 評論(0)  編輯 收藏 引用 所屬分類: .NET
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            老司机午夜精品视频在线观看| 欧美成人精品1314www| 亚洲免费网址| 欧美日韩日日夜夜| 亚洲高清影视| 亚洲精品一二| 女人色偷偷aa久久天堂| 中文在线资源观看视频网站免费不卡| 欧美精品手机在线| 一区二区欧美日韩| 这里只有精品电影| 欧美日韩精品国产| 亚洲天堂网在线观看| 亚洲视频成人| 国产精品网站在线| 欧美资源在线观看| 欧美在线视屏| 在线观看精品一区| 久久综合九色欧美综合狠狠| 亚洲国产精品久久久久秋霞不卡| 99精品视频一区| 国产精品久久久久久亚洲调教 | 久久国产精品色婷婷| 久久男人资源视频| 亚洲精品国产精品乱码不99按摩| 玖玖国产精品视频| 亚洲精品乱码久久久久久蜜桃91 | 国产精品一区一区三区| 欧美在线免费观看| 亚洲国产精品一区二区三区| 亚洲一区二区三区精品在线| 国产喷白浆一区二区三区| 欧美一区二区三区婷婷月色 | 欧美三级欧美一级| 久久久久久久999精品视频| 一本久久综合亚洲鲁鲁| 快射av在线播放一区| 午夜精品久久久久久久白皮肤 | 亚洲第一在线综合在线| 国产精品美腿一区在线看| 免费在线观看精品| 久久大综合网| 亚洲男同1069视频| 日韩午夜av电影| 欧美成人在线免费视频| 欧美在线首页| 校园春色综合网| 一本久道久久综合中文字幕| 激情婷婷亚洲| 国产日韩av高清| 国产精品青草综合久久久久99| 欧美精品尤物在线| 欧美成年人视频| 久久久久久久久久久久久女国产乱 | 亚洲综合日本| 日韩一二在线观看| 亚洲国产另类久久精品| 两个人的视频www国产精品| 欧美制服丝袜| 欧美一区=区| 亚洲欧美国产日韩天堂区| av72成人在线| 一本到高清视频免费精品| 亚洲精品精选| 日韩视频一区二区三区在线播放| 在线看视频不卡| 亚洲第一网站免费视频| 1204国产成人精品视频| 在线免费观看一区二区三区| 黑人一区二区三区四区五区| 国产亚洲欧美中文| 国内精品美女av在线播放| 国产综合色在线| 伊人成人在线视频| 亚洲第一精品福利| 亚洲国产精品久久久久秋霞影院 | 美女免费视频一区| 免费一级欧美片在线播放| 欧美本精品男人aⅴ天堂| 欧美福利视频| 亚洲日韩欧美视频| 亚洲精选大片| 亚洲视频自拍偷拍| 亚洲欧美日韩国产综合| 欧美在线观看视频| 久久一区二区三区超碰国产精品| 久久视频在线视频| 欧美国产综合视频| 国产精品狠色婷| 国产亚洲成av人在线观看导航| 国产亚洲福利社区一区| 一区精品在线| 99国产精品久久久久久久成人热| 一区二区三区欧美激情| 欧美一区二区免费| 免费观看日韩av| 亚洲黄一区二区| 中文精品一区二区三区| 亚洲尤物在线| 老司机一区二区三区| 欧美视频不卡中文| 国产午夜精品麻豆| 亚洲精品一区二区在线观看| 亚洲一区二区三区在线看| 欧美有码视频| 亚洲高清免费在线| 亚洲影视在线播放| 久久综合色综合88| 欧美色视频日本高清在线观看| 国产美女在线精品免费观看| 亚洲第一久久影院| 亚洲伊人观看| 欧美大片免费久久精品三p| 一区二区三欧美| 久久精品国产综合精品| 欧美精品一区二区三区视频| 国产视频一区在线| 中文成人激情娱乐网| 麻豆免费精品视频| 亚洲视频一区二区在线观看 | 久久婷婷国产麻豆91天堂| 欧美日韩一卡| 在线电影国产精品| 午夜精品免费视频| 亚洲国产高清在线观看视频| 亚洲免费中文字幕| 欧美日韩hd| 亚洲高清色综合| 午夜精品久久一牛影视| 亚洲人体一区| 久久久欧美一区二区| 国产精品高清一区二区三区| 亚洲盗摄视频| 久久视频精品在线| 亚洲图片欧美午夜| 欧美日韩美女一区二区| 亚洲黄色成人久久久| 久久精品夜色噜噜亚洲aⅴ| 一二三四社区欧美黄| 欧美高清免费| 亚洲国产精品毛片| 久久夜色精品国产| 午夜日韩在线观看| 国产精品午夜在线| 亚洲图片欧洲图片av| 欧美国内亚洲| 久久av一区| 国产一区视频在线观看免费| 亚洲欧美日韩中文播放| 亚洲美女视频在线免费观看| 免费日本视频一区| 亚洲国产第一| 免费在线日韩av| 久久人人97超碰精品888| 国产真实精品久久二三区| 久久国产精品毛片| 午夜日韩激情| 国产一区日韩一区| 久久婷婷国产综合精品青草| 性欧美激情精品| 国产尤物精品| 久久综合久久久久88| 久久精品在线免费观看| 国内成+人亚洲| 久久免费精品视频| 久久精品官网| 影音先锋国产精品| 欧美大片一区| 欧美精品一区二区三区很污很色的 | 欧美一级大片在线观看| 亚洲欧美一区二区三区久久 | 在线观看福利一区| 欧美激情精品久久久久久| 美女露胸一区二区三区| 亚洲每日在线| 99国产精品99久久久久久| 欧美日韩一区不卡| 欧美在线观看一区二区三区| 亚洲欧美综合| 激情一区二区三区| 亚洲国产99精品国自产| 欧美日产一区二区三区在线观看| 在线一区免费观看| 亚洲欧美激情一区| 尤物yw午夜国产精品视频| 欧美激情小视频| 欧美日韩中文在线| 久久久国产精品一区二区中文 | 欧美日韩国产高清视频| 亚洲欧美中文另类| 欧美在线电影| 亚洲免费福利视频| 午夜精品免费| 91久久午夜| 亚洲一区二区在线看| 在线日本高清免费不卡| 日韩视频在线一区| 在线观看91精品国产麻豆| 日韩一本二本av| 伊人色综合久久天天|