Managed and Unmanaged code

What Is Managed Code?

Managed Code is what Visual Basic .NET and C# compilers create. It compiles to Intermediate Language (IL), not to machine code that could run directly on your computer. The IL is kept in a file called an assembly, along with metadata that describes the classes, methods, and attributes (such as security requirements) of the code you've created. This assembly is the one-stop-shopping unit of deployment in the .NET world. You copy it to another server to deploy the assembly there—and often that copying is the only step required in the deployment.Managed code runs in the Common Language Runtime. The runtime offers a wide variety of services to your running code. In the usual course of events, it first loads and verifies the assembly to make sure the IL is okay. Then, just in time, as methods are called, the runtime arranges for them to be compiled to machine code suitable for the machine the assembly is running on, and caches this machine code to be used the next time the method is called. (This is called Just In Time, or JIT compiling, or often just Jitting.) As the assembly runs, the runtime continues to provide services such as security, memory management, threading, and the like. The application is managed by the runtime.Visual Basic .NET and C# can produce only managed code. If you're working with those applications, you are making managed code. Visual C++ .NET can produce managed code if you like: When you create a project, select one of the application types whose name starts with .Managed., such as .Managed C++ application..

What Is Unmanaged Code?

Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler you may still have kicking around on your hard drive all produced unmanaged code. It compiled directly to machine code that ran on the machine where you compiled it—and on other machines as long as they had the same chip, or nearly the same. It didn't get services such as security or memory management from an invisible runtime; it got them from the operating system. And importantly, it got them from the operating system explicitly, by asking for them, usually by calling an API provided in the Windows SDK. More recent unmanaged applications got operating system services through COM calls.Unlike the other Microsoft languages in Visual Studio, Visual C++ can create unmanaged applications. When you create a project and select an application type whose name starts with MFC, ATL, or Win32, you're creating an unmanaged application.This can lead to some confusion: When you create a .Managed C++ application., the build product is an assembly of IL with an .exe extension. When you create an MFC application, the build product is a Windows executable file of native code, also with an .exe extension. The internal layout of the two files is utterly different. You can use the Intermediate Language Disassembler, ildasm, to look inside an assembly and see the metadata and IL. Try pointing ildasm at an unmanaged exe and you'll be told it has no valid CLR (Common Language Runtime) header and can't be disassembled—Same extension, completely different files.

By Avnish Jassra******************************************************************************************************Live as if your were to die tomorrow. Learn as if you were to live forever******************************************************************************************************

阅读更多

Cannot marshal 'return value': Invalid managed/unmanaged type combination.

07-05

VS2010-C#引用delphi6下编译的dll文件,读出byte[]报错“Cannot marshal 'return value': Invalid managed/unmanaged type combination.”rndelphi6下编译的dll文件 源码如下:rnrn[code=Delphi(Pascal)]rnlibrary dataoutdll;rnusesrn Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,rn Dialogs, DB, ADODB, Grids, DBGrids, DBClient, StdCtrls, Provider;rnrntypern byteBuffer = array[0..1024] of byte;rnvarrnmydataset:tclientdataset;rnmydatasource:tdatasource;rnmyprovider: tdatasetprovider;rnmyquery:tadoquery;rnsqlcmd:TStringList;rnrnms1,ms2,ms3:TMemoryStream;rnbuf: bytebuffer;rn$R *.resrnfunction getbytes():bytebuffer;stdcall;rnbeginrnms1:=TMemoryStream.Create;rnms2:=TMemoryStream.Create;rnms3:=TMemoryStream.Create;rnrnrnmydataset:=tclientdataset.Create(nil);rnmydatasource:=tdatasource.Create(nil);rnmyprovider:=tdatasetprovider.Create(nil);rnmyquery:=tadoquery.Create(nil);rnsqlcmd:=tstringlist.Create;rnrnsqlcmd.Add('select * from trequestprice');rnmyquery .ConnectionString :=rn 'Provider=SQLNCLI.1;Password=qqqqqq;Persist Security Info=True;Us' +rn 'er ID=sa;Initial Catalog=HSEms;Data Source=.' ;rnmyquery .CursorType := ctStatic ;rnmyquery.SQL.AddStrings(sqlcmd) ;rnmyquery.SQL.Strings [0];rnmyquery .Active := True;rnrnmyprovider.DataSet :=myquery;rnmydataset.SetProvider(myprovider) ;rnmydataset.Active := True ;rnmydatasource.DataSet := mydataset;rnrnmydataset.SaveToStream(ms3);rnms3.Position :=0;rnrnFillChar(buf,1025,0);rnms3.Read (buf,sizeof(buf));rnms3.Position :=0;rnresult:=buf;rnend;rnExportsrngetbytes;rnrnbeginrnend.rn[/code]rn代码功能:读取表trequestprice到内存buf中,作为dll的返回值吐出给C#。rnrnVS2010-C#下调用该dll的代码如下:rn[code=C#]rn[DllImport("dataoutdll.dll", EntryPoint = "getbytes", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]rn public static extern byte[] getbytes();rnrn rn private void button1_Click(object sender, EventArgs e)rn rnrn tryrn rn byte[] mybt = new byte[1024];rn mybt = getbytes();//程序执行到这里报错rn rn catch (Exception ex)rn MessageBox.Show(ex .Message ); rn rn rn rn[/code]rnrn错误提示为:Cannot marshal 'return value': Invalid managed/unmanaged type combination.rnrn请问各位:delphi6编译下的dll中的array[0..1024] of byte 跟C#下的byte[1024],在内存上是等效的吗?rn如何解决这个错误?rn在线等。rn谢谢

Invalid managed/unmanaged type combination,托管与非托管 的应用

07-05

VS2010-C#引用delphi6下编译的dll文件,读出byte[]报错“Cannot marshal 'return value': Invalid managed/unmanaged type combination.”rndelphi6下编译的dll文件 源码如下:rnrn[code=Delphi(Pascal)]rnlibrary dataoutdll;rnusesrn Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,rn Dialogs, DB, ADODB, Grids, DBGrids, DBClient, StdCtrls, Provider;rnrntypern byteBuffer = array[0..1024] of byte;rnvarrnmydataset:tclientdataset;rnmydatasource:tdatasource;rnmyprovider: tdatasetprovider;rnmyquery:tadoquery;rnsqlcmd:TStringList;rnrnms1,ms2,ms3:TMemoryStream;rnbuf: bytebuffer;rn$R *.resrnfunction getbytes():bytebuffer;stdcall;rnbeginrnms1:=TMemoryStream.Create;rnms2:=TMemoryStream.Create;rnms3:=TMemoryStream.Create;rnrnrnmydataset:=tclientdataset.Create(nil);rnmydatasource:=tdatasource.Create(nil);rnmyprovider:=tdatasetprovider.Create(nil);rnmyquery:=tadoquery.Create(nil);rnsqlcmd:=tstringlist.Create;rnrnsqlcmd.Add('select * from trequestprice');rnmyquery .ConnectionString :=rn 'Provider=SQLNCLI.1;Password=qqqqqq;Persist Security Info=True;Us' +rn 'er ID=sa;Initial Catalog=HSEms;Data Source=.' ;rnmyquery .CursorType := ctStatic ;rnmyquery.SQL.AddStrings(sqlcmd) ;rnmyquery.SQL.Strings [0];rnmyquery .Active := True;rnrnmyprovider.DataSet :=myquery;rnmydataset.SetProvider(myprovider) ;rnmydataset.Active := True ;rnmydatasource.DataSet := mydataset;rnrnmydataset.SaveToStream(ms3);rnms3.Position :=0;rnrnFillChar(buf,1025,0);rnms3.Read (buf,sizeof(buf));rnms3.Position :=0;rnresult:=buf;rnend;rnExportsrngetbytes;rnrnbeginrnend.rn[/code]rnrn代码功能:读取表trequestprice到内存buf中,作为dll的返回值吐出给C#。rnrnVS2010-C#下调用该dll的代码如下:rn[code=C#]C# codern[DllImport("dataoutdll.dll", EntryPoint = "getbytes", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]rn public static extern byte[] getbytes();rnrn rn private void button1_Click(object sender, EventArgs e)rn rnrn tryrn rn byte[] mybt = new byte[1024];rn mybt = getbytes();//程序执行到这里报错rn rn catch (Exception ex)rn MessageBox.Show(ex .Message ); rn rn rn rnrn[/code]rnrn错误提示为:Cannot marshal 'return value': Invalid managed/unmanaged type combination.rnrn请问各位:delphi6编译下的dll中的array[0..1024] of byte 跟C#下的byte[1024],在内存上是等效的吗?rn如何解决这个错误?rn在线等。rn谢谢

没有更多推荐了,返回首页