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

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 閱讀(1048) 評論(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>
            国产日韩欧美一区二区| 国产亚洲欧美日韩一区二区| 日韩视频三区| 欧美jizz19性欧美| 亚洲尤物精选| 久久久久久黄| 欧美高清在线视频观看不卡| 欧美日韩一区二区三区视频| 欧美一区二区大片| 国产精品sm| 免费观看在线综合色| 亚洲女爱视频在线| 国产精品亚洲人在线观看| 狂野欧美性猛交xxxx巴西| 欧美一区中文字幕| 久久精品一区二区国产| 国产精品婷婷午夜在线观看| 亚洲一区二区三区国产| 国产精品久久久久99| 欧美亚洲一区| 久久久水蜜桃| 久久精品人人做人人综合| 国内久久视频| 校园激情久久| 欧美日韩国产综合网| 久久免费视频网| 一本色道久久综合亚洲精品高清 | 国产亚洲欧美日韩精品| 亚洲国产高清一区| 欧美一区二区日韩一区二区| 一区二区三区在线观看视频 | 亚洲第一福利在线观看| 亚洲精品一区在线观看| 久久精品1区| 亚洲主播在线播放| 亚洲精品久久久蜜桃| 精品91视频| 欧美大片免费久久精品三p | 国产日韩一区二区三区在线播放| 亚洲精品在线观看视频| 久久久久国产精品www| 黄色一区二区三区| 久久在线播放| 欧美日韩中文字幕在线视频| 日韩一级在线| 欧美激情精品| 久久免费国产精品| 日韩写真在线| 久久中文字幕导航| 在线看视频不卡| 国产精品视频网站| 精品va天堂亚洲国产| 欧美日韩国产美女| 午夜精品久久久| 久久久99国产精品免费| 午夜精品亚洲一区二区三区嫩草| 在线综合欧美| 欧美高清在线一区| 午夜精品一区二区三区在线视| 尤物在线观看一区| 国产精品久久久久影院色老大| 亚洲精品偷拍| 99re热这里只有精品免费视频| 亚洲精品久久久久久下一站| 亚洲男人的天堂在线观看| 久久久久久夜精品精品免费| 亚洲一区二区成人在线观看| 国产麻豆日韩| 久久亚洲午夜电影| 欧美一级淫片aaaaaaa视频| 女人香蕉久久**毛片精品| 午夜精品久久| 久久久久久尹人网香蕉| 国产欧美日韩精品丝袜高跟鞋 | 亚洲午夜免费福利视频| 久久综合成人精品亚洲另类欧美| 欧美成年人视频| 亚洲乱码久久| 久久精品一区四区| 久久性天堂网| 欧美a级片网站| 国产精品久久久久久久久动漫| 欧美日韩一区二区在线观看视频| 欧美日韩一区二区高清| 国产日产欧美a一级在线| 日韩午夜在线电影| 久久夜色精品国产噜噜av| 国产日韩在线视频| 美日韩在线观看| 亚洲欧洲视频在线| 久久综合电影| 欧美国产先锋| 亚洲综合色网站| 欧美激情欧美激情在线五月| 欧美麻豆久久久久久中文| 亚洲免费播放| 日韩视频久久| 久久男人资源视频| 激情六月综合| 亚洲国产精品一区二区www| 亚洲国语精品自产拍在线观看| 免费观看亚洲视频大全| 欧美成人dvd在线视频| 久久亚洲欧美| 亚洲久久一区二区| 亚洲精品视频在线| 小黄鸭视频精品导航| 亚洲人成在线播放网站岛国| 久久国产天堂福利天堂| 日韩视频不卡中文| 国产视频一区欧美| 久久久久久久综合| 欧美精品久久一区| 欧美成人综合在线| 亚洲欧洲一区二区天堂久久 | 国产三级精品三级| 久久久久国产精品麻豆ai换脸| 亚洲一区自拍| 亚洲精品系列| 精品成人在线观看| 亚洲图片在线| 久久精品成人| 欧美成人伊人久久综合网| 在线精品福利| 你懂的成人av| 玖玖玖国产精品| 国产精品免费视频xxxx| 久久国产精品一区二区| 久久狠狠久久综合桃花| 欧美一级播放| 国产午夜精品一区二区三区欧美| 老鸭窝亚洲一区二区三区| 欧美视频在线视频| 亚洲一区免费视频| 国产精品视频99| 国产精品99久久久久久www| 羞羞答答国产精品www一本 | 国产一区二区三区直播精品电影| 亚洲二区在线观看| 亚洲尤物视频网| 亚洲激情综合| 亚洲成人在线观看视频| 欧美日韩999| 久久精品国产一区二区三| 亚洲激情视频在线播放| 欧美激情一区二区在线| 99精品久久久| 久久av在线看| 欧美日韩理论| 久久久久久久久久久成人| 欧美在线视频日韩| 一区二区精品在线| 欧美黄色一级视频| 久久综合给合| 欧美性大战久久久久久久蜜臀| 久久se精品一区精品二区| 久久久在线视频| 亚洲一二三区精品| 亚洲精品国产精品久久清纯直播| 国产乱码精品一区二区三区五月婷 | 在线视频欧美日韩| 亚洲欧美成人综合| 亚洲国产精品视频一区| 欧美日韩国产成人高清视频| 久久国产欧美| 久久人人爽人人| 欧美在线91| 亚洲国产99| 亚洲人成亚洲人成在线观看图片 | 亚洲中午字幕| 国产精品你懂的在线欣赏| 欧美久久久久免费| 亚洲啪啪91| 亚洲网在线观看| 一区二区三区欧美亚洲| 亚洲一区日韩在线| 久久国产精品久久国产精品| 久久先锋影音| 久久精品伊人| 久久综合狠狠综合久久激情| 精品成人一区二区三区四区| 亚洲国产高清自拍| 亚洲国产精品久久久久秋霞影院| 日韩亚洲视频| 亚洲自拍偷拍麻豆| 久久不射中文字幕| 免费不卡在线观看av| 一区二区日韩精品| 欧美成人三级在线| 国产精品二区影院| 日韩天天综合| 亚洲黄色影片| 欧美日韩国产va另类| 99在线精品视频| 欧美激情视频在线播放| 久久爱www久久做| 亚洲愉拍自拍另类高清精品| 美日韩精品免费观看视频| 国产精品va在线| 亚洲精品在线视频|