锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 銆銆褰撶劧鏄彲浠ョ殑銆傚叿浣撳弬瑙侊細璁〤++浠g爜涓嶤#浠g爜涓璧風敓鎴愪竴涓崟涓鐨凙ssembly The last two Nuts and Bolts articles focused on database related topics. This month we'll change gears and focus on something different. The focus of this month's article will be .NET remoting. We'll discuss what is .NET Remoting, how it compares to DCOM and Web services, and go through an example. .NET Remoting is an enabler for application communication. It is a generic system for different applications to use to communicate with one another. .NET objects are exposed to remote processes, thus allowing interprocess communication. The applications can be located on the same computer, different computers on the same network, or even computers across separate networks. In the past interprocess communication between applications was handled through Distributed COM, or DCOM. DCOM works well and the performance is adequate when applications exist on computers of similar type on the same network. However, DCOM has its drawbacks in the Internet connected world. DCOM relies on a proprietary binary protocol that not all object models support, which hinders interoperability across platforms. In addition, have you tried to get DCOM to work through a firewall? DCOM wants to communicate over a range of ports that are typically blocked by firewalls. There are a ways to get it to work, but they either decrease the effectiveness of the firewall (why bother to even have the firewall if you open up a ton of ports on it), or require you to get a firewall that allows support for binary traffic over port 80. .NET Remoting eliminates the difficulties of DCOM by supporting different transport protocol formats and communication protocols. This allows .NET Remoting to be adaptable to the network environment in which it is being used. Unless you have been living in a cave, or are way behind in your reading, you have probably read something about Web services. When you read the description of .NET Remoting it may remind you a lot of what you're read about Web services. That is because Web services fall under the umbrella of .NET Remoting, but have a simplified programming model and are intended for a wide target audience. Web services involve allowing applications to exchange messages in a way that is platform, object model, and programming language independent. Web services are stateless and know nothing about the client that is making the request. The clients communicate by transferring messages back and forth in a specific format known as the Simple Object Access Protocol, or SOAP. (Want to get some funny looks in the hallway? Stand around in the hallway near the marketing department with your colleagues and discuss the benefits of using SOAP). The following list outlines some of the major differences between .NET Remoting and Web services that will help you to decide when to use one or the other: Remote objects are accessed through Channels. Channels physically transport the messages to and from remote objects. There are two existing channels TcpChannel and HttpChannel. Their names give away the protocols that they use. In addition, the TcpChannel or HttpChannel can be extended, or a new channel created if you determine the existing channels do not meet your needs. A remotable object is nothing more than an object that inherits from MarshalByRefObject. The following sample demonstrates a simple class to expose the omnipresent hello world. This object exposes a single method HelloWorld that will return a string. The only values that can be returned from methods are the classes in the .NET Framework that are serializable such as string and DataSet. In addition, if you need to return a user-defined object then the object needs to be marked as serializable. Create a new C# class library project. Add a class called SampleObject and put in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. Compile the class to make sure you have everything correct. We need to create a server object that will act as a listener to accept remote object requests. For this example we will use the TCP/IP channel. We first create an instance of the channel and then register it for use by clients at a specific port. The service can be registered as WellKnownObjectMode.SingleCall, which results in a new instance of the object for each client, or as WellKnownObjectMode.Singleton, which results in one instance of the object used for all clients. It is not necessary to create the server listener if you are planning to use IIS. For obvious reasons, IIS only supports the use of the HttpChannel. Create a virtual directory for your application and then put code to register your service in the Application_Start event. For our example, we'll go ahead and create a server listener in case you don't have IIS. Since the service needs to be bound to an available port, for our example I chose 8080, which is a port that I know to be unused on my computer. You may need to choose a different port depending upon what ports you have available. To see a list of the used ports on your computer open a command prompt and issue the command "netstat --a". It may produce a long listing so make sure the command prompt buffer sizes are set to allow scrolling. Compile the class to make sure you have everything correct. Create a new C# console application project. Add a class called SampleServer and paste in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. In addition, add a reference to the project containing the SampleObject, otherwise the code will not compile because it won't know how to find a reference to SampleObject. Now that we have our remotable object and a server object to listen for requests, let's create a client to use it. Our client will be very simple. It will connect to the server, create an instance of the object using the server, and then execute the HelloWorld method. Create a new C# console application project. Add a class called SampleClient and paste in the following code. Add a reference to System.Runtime.Remoting in the project, otherwise the TcpChannel will not be found. In addition, add a reference to the project containing the SampleObject, otherwise the code will not compile because it won't know how to find a reference to SampleObject. Compile the class to make sure you have everything correct. Once you have created the projects and successfully compiled each of them you are ready to try it out. Assuming you chose a free TCP/IP port for the service, start the server executable. After the server successfully starts it will result in a console window being displayed with the message "Press the enter key to exit". The server is listening so you are now ready to run the client. Executing the client should result in "Hello World!" being displayed in a separate console window. The client window will then close while the server remains open and available. If you have multiple computers available to you on a network you could execute the server on one machine and the client on another just to prove to yourself that it really is remoting. In order to run on separate machines you would need to change the reference to localhost in the sample client to point to the appropriate location. .NET Remoting is a powerful way to enable interprocess communication. It is more complicated to program against than Web services. You need to decide for yourself whether your standard architecture is to use .NET Remoting or Web services. The next column will be on the use of encryption in the .NET framework. We'll take a look at some of the encryption algorithms available in the framework and ways to use them. If you have a particular topic in mind please email me at mstrawmyer@crowechizek.com Mark Strawmyer, MCSD, MCSE (NT4/W2K), MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@crowechizek.com.
鎵句簡濂藉嚑澶╀簡錛屽緢澶氳祫婧愰兘涓嬩笉鍔?br>
浠婂ぉ濂戒笉瀹規槗涓嬩簡涓涓紝浣嗘槸棣欐腐鐗堢殑錛屼笖瀹夎鍒伴夋嫨鐩樼鏃訛紝閿洏鎵鏈夐敭閮戒笉鑳戒嬌鐢紝鎵浠ヨ繕鏄棤娉曞畨瑁?br>
璋㈣阿鍚勪綅澶у摜澶у甯府蹇欙紝灝忓紵鎰熸縺涓嶅敖錛?img src ="http://m.shnenglu.com/twzheng/aggbug/109710.html" width = "1" height = "1" />
]]>
鏈枃浣滆咃細sodme
鏈枃鍑哄錛?font style="COLOR: #000000" color=#0000ff>http://blog.csdn.net/sodme
澹版槑錛氭湰鏂囧彲浠ヤ笉緇忎綔鑰呭悓鎰忎換鎰忚漿杞姐佸鍒躲佷紶鎾紝浣嗕換浣曞鏈枃鐨勫紩鐢ㄥ潎欏諱繚鐣欐湰鏂囩殑浣滆呫佸嚭澶勫強鏈澹版槑淇℃伅錛佽阿璋紒
銆銆瀹屾垚绔彛妯″瀷錛岄拡瀵逛簬WIN騫沖彴鐨勫叾瀹冨紓姝ョ綉緇滄ā鍨嬭岃█錛屾渶澶х殑濂藉錛岄櫎浜嗘ц兘鏂歸潰鐨勫崜瓚婂錛岃繕鍦ㄤ簬瀹屾垚绔彛鍦ㄤ紶閫掔綉緇滀簨浠剁殑閫氱煡鏃訛紝鍙互涓騫朵紶閫掍笌姝や簨浠剁浉鍏崇殑搴旂敤灞傛暟鎹傝繖涓簲鐢ㄥ眰鏁版嵁錛屼綋鐜板湪涓や釜鏂歸潰錛氫竴鏄崟鍙ユ焺鏁版嵁錛屼簩鏄崟IO鏁版嵁銆?br>
銆銆GetQueuedCompletionStatus鍑芥暟鐨勫師鍨嬪涓嬶細
銆銆WINBASEAPI
銆銆BOOL
銆銆WINAPI
銆銆GetQueuedCompletionStatus(
銆銆IN HANDLE CompletionPort,
銆銆 OUT LPDWORD lpNumberOfBytesTransferred,
銆銆OUT PULONG_PTR lpCompletionKey,
銆銆OUT LPOVERLAPPED *lpOverlapped,
銆銆IN DWORD dwMilliseconds
銆);
銆銆鍏朵腑錛屾垜浠妸絎笁涓弬鏁發pCompletionKey縐頒負瀹屾垚閿紝鐢卞畠浼犻掔殑鏁版嵁縐頒負鍗曞彞鏌勬暟鎹傛垜浠妸絎洓涓弬鏁發pOverlapped縐頒負閲嶅彔緇撴瀯浣擄紝鐢卞畠浼犻掔殑鏁版嵁縐頒負鍗旾O鏁版嵁銆?br>
銆銆浠ュ瓧闈㈢殑鎰忔濇潵鐞嗚В錛宭pCompletionKey鍐呭寘瀹圭殑涓滆タ搴旇鏄笌鍚勪釜socket涓涓瀵瑰簲鐨勶紝鑰宭pOverlapped鏄笌姣忎竴嬈$殑wsarecv鎴杦sasend鎿嶄綔涓涓瀵瑰簲鐨勩?br>
銆銆鍦ㄧ綉緇滄ā鍨嬬殑甯歌璁捐涓紝褰撲竴涓鎴風榪炴帴鍒版湇鍔″櫒鍚庯紝鏈嶅姟鍣ㄤ細閫氳繃accept鎴朅cceptEx鍒涘緩涓涓猻ocket錛岃屽簲鐢ㄥ眰涓轟簡淇濆瓨涓庢socket鐩稿叧鐨勫叾瀹冧俊鎭紙姣斿錛氳socket鎵瀵瑰簲鐨剆ockaddr_in緇撴瀯浣撴暟鎹紝璇ョ粨鏋勪綋鍐呭惈瀹㈡埛绔疘P絳変俊鎭紝浠ュ強涓轟究浜庡鎴風鐨勯昏緫鍖呮暣鐞嗚屽噯澶囩殑鏁版嵁鏁寸悊緙撳啿鍖虹瓑錛夛紝寰寰闇瑕佸垱寤轟竴涓笌璇ocket涓涓瀵瑰簲鐨勫鎴風搴曞眰閫氫俊瀵硅薄錛岃繖涓璞″彲浠ヨ礋璐d繚瀛樹粎鍦ㄧ綉緇滃眰闇瑕佸鐞嗙殑鏁版嵁鎴愬憳鍜屾柟娉曪紝鐒跺悗鎴戜滑闇瑕佸皢姝ゅ鎴風搴曞眰閫氫俊瀵硅薄鏀懼叆涓涓被浼間簬list鎴杕ap鐨勫鍣ㄤ腑錛屽緟鍒伴渶瑕佷嬌鐢ㄧ殑鏃跺欙紝浣跨敤瀹瑰櫒鐨勬煡鎵劇畻娉曟牴鎹畇ocket鍊兼壘鍒板畠鎵瀵瑰簲鐨勫璞$劧鍚庤繘琛屾垜浠墍闇瑕佺殑鎿嶄綔銆?br>
銆銆璁╀漢闈炲父楂樺叴鐨勬槸錛屽畬鎴愮鍙?#8220;浣撹創鍏ュ井”錛屽畠宸茬粡甯垜浠湪姣忔鐨勫畬鎴愪簨浠墮氱煡鏃訛紝紼嶅甫鐫鎶婅socket鎵瀵瑰簲鐨勫簳灞傞氫俊瀵硅薄鐨勬寚閽堥佺粰浜嗘垜浠紝榪欎釜鎸囬拡灝辨槸lpCompletionKey銆備篃灝辨槸璇達紝褰撴垜浠粠GetQueuedCompletionStatus鍑芥暟鍙栧緱涓涓暟鎹帴鏀跺畬鎴愮殑閫氱煡錛岄渶瑕佸皢姝ゆ鏀跺埌鐨勬暟鎹斁鍒拌socket鎵瀵瑰簲鐨勯氫俊瀵硅薄鏁寸悊緙撳啿鍖哄唴瀵規暟鎹繘琛屾暣鐞嗘椂錛屾垜浠凡緇忎笉闇瑕佸幓鎵цlist鎴杕ap絳夌殑鏌ユ壘綆楁硶錛岃屾槸鍙互鐩存帴瀹氫綅榪欎釜瀵硅薄浜嗭紝褰撳鎴風榪炴帴閲忓緢澶ф椂錛岄綣佹煡琛ㄨ繕鏄緢褰卞搷鏁堢巼鐨勩傚搰鍝︼紝澶竻浜嗭紝涓嶆槸鍚楋紵鍛靛懙銆?br>
銆銆鍩轟簬浠ヤ笂鐨勮璇嗭紝鎴戜滑鐨刲pCompletionKey瀵硅薄鍙互璁捐濡備笅錛?br>銆銆typedef struct PER_HANDLE_DATA
銆銆{
銆銆銆銆SOCKET socket;銆銆銆銆銆銆銆銆銆銆 //鏈粨鏋勪綋瀵瑰簲鐨剆ocket鍊?br>銆銆銆銆sockaddr_in addr;銆銆銆銆銆銆銆銆銆銆//鐢ㄤ簬瀛樻斁瀹㈡埛绔疘P絳変俊鎭?br>銆銆銆銆char DataBuf[ 2*MAX_BUFFER_SIZE ];銆銆//鏁寸悊緙撳啿鍖?鐢ㄤ簬瀛樻斁姣忔鏁寸悊鏃剁殑鏁版嵁
銆銆}
銆銆PER_HANDLE_DATA涓巗ocket鐨勭粦瀹氾紝閫氳繃CreateIOCompletionPort瀹屾垚錛屽皢璇ョ粨鏋勪綋鍦板潃浣滀負璇ュ嚱鏁扮殑絎笁涓弬鏁頒紶鍏ュ嵆鍙傝孭ER_HANDLE_DATA緇撴瀯浣撲腑addr鎴愬憳錛屾槸鍦╝ccept鎵ц鎴愬姛鍚庤繘琛岃祴鍊肩殑銆侱ataBuf鍒欏彲浠ュ湪姣忔WSARecv鎿嶄綔瀹屾垚錛岄渶瑕佹暣鐞嗙紦鍐插尯鏁版嵁鏃朵嬌鐢ㄣ?br>
銆銆涓嬮潰鎴戜滑鍐嶆潵鐪嬬湅瀹屾垚绔彛鐨勬敹銆佸彂鎿嶄綔涓墍浣跨敤鍒扮殑閲嶅彔緇撴瀯浣揙VERLAPPED銆?br>
銆銆鍏充簬閲嶅彔IO鐨勭煡璇嗭紝璇瘋嚜琛孏OOGLE鐩稿叧璧勬枡銆傜畝鍗曞湴璇達紝OVERLAPPED鏄簲鐢ㄥ眰涓庢牳蹇冨眰浜や簰鍏變韓鐨勬暟鎹崟鍏冿紝濡傛灉瑕佹墽琛屼竴涓噸鍙營O鎿嶄綔錛屽繀欏誨甫鏈塐VERLAPPED緇撴瀯銆傚湪瀹屾垚绔彛涓紝瀹冨厑璁稿簲鐢ㄥ眰瀵筄VERLAPPED緇撴瀯榪涜鎵╁睍鍜岃嚜瀹氫箟錛屽厑璁稿簲鐢ㄥ眰鏍規嵁鑷繁鐨勯渶瑕佸湪OVERLAPPED鐨勫熀紜涓婂艦鎴愭柊鐨勬墿灞昈VERLAPPED緇撴瀯銆備竴鑸湴錛屾墿灞曠殑OVERLAPPED緇撴瀯涓紝瑕佹眰鏀懼湪絎竴涓殑鏁版嵁鎴愬憳鏄師OVERLAPPED緇撴瀯銆傛垜浠彲浠ュ艦濡備互涓嬫柟寮忓畾涔夎嚜宸辯殑鎵╁睍OVERLAPPED緇撴瀯錛?br>銆銆typedef struct PER_IO_DATA
銆銆{
銆銆銆銆OVERLAPPED ovl;
銆銆銆銆WSABUF buf;
銆銆銆銆char RecvDataBuf[ MAX_BUFFER_SIZE ]; //鎺ユ敹緙撳啿鍖?br>銆銆銆銆char SendDataBuf[ MAX_BUFFER_SIZE ]; //鍙戦佺紦鍐插尯
銆銆銆銆OpType opType; //鎿嶄綔綾誨瀷錛氬彂閫併佹帴鏀舵垨鍏抽棴絳?br>銆銆}
銆銆
銆銆鍦ㄦ墽琛學SASend鍜學SARecv鎿嶄綔鏃訛紝搴旂敤灞備細灝嗘墿灞昈VERLAPPED緇撴瀯鐨勫湴鍧浼犵粰鏍稿績錛屾牳蹇冨畬鎴愮浉搴旂殑鎿嶄綔鍚庯紝浠嶇劧閫氳繃鍘熸湁鐨勮繖涓粨鏋勪紶閫掓搷浣滅粨鏋滐紝姣斿“鎺ユ敹”鎿嶄綔瀹屾垚鍚庯紝RecvDataBuf閲屽瓨鏀句究鏄嬈℃帴鏀朵笅鏉ョ殑鏁版嵁銆?br>
銆銆鏍規嵁鍚勮嚜搴旂敤鐨勪笉鍚岋紝涓嶅悓鐨勫畬鎴愮鍙h璁¤呭彲鑳戒細璁捐鍑轟笉鍚岀殑PER_HANDLE_DATA
鍜孭ER_IO_DATA錛屾垜榪欓噷緇欏嚭鐨勮璁′篃鍙槸閽堝鑷繁鐨勫簲鐢ㄥ満鍚堢殑錛屼笉涓瀹氬氨閫傚悎浣犮備絾鎴戞兂錛屾渶涓昏鐨勮繕鏄鎼炴槑鐧絇ER_HANDLE_DATA鍜孭ER_IO_DATA涓ょ緇撴瀯浣撶殑鍚箟銆佺敤閫旓紝浠ュ強璋冪敤嫻佺▼銆?br>
]]>
]]>
鏈枃浣滆咃細sodme銆鏈枃鍑哄錛歨ttp://blog.csdn.net/sodme
鐗堟潈澹版槑錛氭湰鏂囧彲浠ヤ笉緇忎綔鑰呭悓鎰忎換鎰忚漿杞斤紝浣嗚漿杞芥椂鐑﹁淇濈暀鏂囩珷寮濮嬪墠涓よ鐨勭増鏉冦佷綔鑰呭強鍑哄淇℃伅銆?br>
銆銆QQ娓告垙浜庡墠鍑犳棩緇堜簬紿佺牬浜嗙櫨涓囦漢鍚屾椂鍦ㄧ嚎鐨勫叧鍙o紝鍚戠潃鏇翠負榪滃ぇ鐨勭洰鏍囪繄榪涳紝榪欒鍏跺畠浼楀浼犵粺鐨勬鐗屼紤闂叉父鎴忓鉤鍙伴化鐒跺け鑹詫紝鐩告瘮涔嬩笅錛岃仈浼椾技涔庡凡緇忔牴鏈笉鏄疩Q鐨勫鎵嬶紝鍥犱負QQ闄や簡榪?00涓囩殑娓告垙鍦ㄧ嚎浜烘暟澶栵紝瀹冭繕鎷ユ湁3浜垮鐨勬敞鍐岄噺錛堝綋鐒跺緢澶氭槸閲嶅娉ㄥ唽鐨勶級浠ュ強QQ鑱婂ぉ杞歡900涓囩殑鍚屾椂鍦ㄧ嚎鐜囷紝鎴戜滑宸茬粡鍙互棰勮鏈潵鐢盦Q鏋勫緩璧鋒潵鐨勫己澶ф鐗屼紤闂叉父鎴忓笣鍥姐?br>銆銆閭d箞錛屽湪鎶鏈笂錛孮Q娓告垙鍒板簳鏄浣曞疄鐜扮櫨涓囦漢鍚屾椂鍦ㄧ嚎騫朵繚鎸佹父鎴忛珮鏁堢巼鐨勫憿錛?br>銆銆浜嬪疄涓婏紝閽堝浜庝換浣曞崟涓鐨勭綉緇滄湇鍔″櫒紼嬪簭錛屽叾鍙壙鍙楃殑鍚屾椂榪炴帴鏁扮洰鏄湁鐞嗚宄板肩殑錛岄氳繃C錛嬶紜涓TSocket鐨勫畾涔夌被鍨嬶細word錛屾垜浠彲浠ュ垽瀹氳繖涓繛鎺ョ悊璁哄嘲鍊兼槸65535錛屼篃灝辨槸璇達紝浣犵殑鍗曚釜鏈嶅姟鍣ㄧ▼搴忥紝鏈澶氬彲浠ユ壙鍙?涓囧鐨勭敤鎴峰悓鏃惰繛鎺ャ備絾鏄紝鍦ㄥ疄闄呭簲鐢ㄤ腑錛岃兘杈懼埌涓涓囦漢鐨勫悓鏃惰繛鎺ュ茍鑳戒繚璇佹甯哥殑鏁版嵁浜ゆ崲宸茬粡鏄緢涓嶅鏄撲簡錛岄氬父榪欎釜鍊奸兘鍦?000鍒?000涔嬮棿錛屾嵁璇碤Q鐨勫崟鍙版湇鍔″櫒鍚屾椂榪炴帴鏁扮洰涔熷氨鏄湪榪欎釜鍊艱繖闂淬?br>銆銆濡傛灉瑕佸疄鐜?000鍒?000鐢ㄦ埛鐨勫崟鏈嶅姟鍣ㄥ悓鏃跺湪綰匡紝鏄笉闅劇殑銆傚湪windows涓嬶紝姣旇緝鎴愮啛鐨勬妧鏈槸閲囩敤IOCP錛嶏紞瀹屾垚绔彛銆備笌瀹屾垚绔彛鐩稿叧鐨勮祫鏂欏湪緗戜笂鍜孋SDN璁哄潧閲屾湁寰堝錛屾劅鍏磋叮鐨勬湅鍙嬪彲浠ヨ嚜宸辨悳绱竴涓嬨傚彧瑕佽繍鐢ㄥ緱褰擄紝涓涓畬鎴愮鍙f湇鍔″櫒鏄畬鍏ㄥ彲浠ヨ揪鍒?K鍒?K鐨勫悓鏃跺湪綰塊噺鐨勩備絾錛?K榪欐牱鐨勬暟鍊肩鐧句竾榪欐牱鐨勬暟鍊煎疄鍦ㄧ浉宸お澶т簡錛屾墍浠ワ紝鐧句竾浜虹殑鍚屾椂鍦ㄧ嚎鏄崟鍙版湇鍔″櫒鑲畾鏃犳硶瀹炵幇鐨勩?br>銆銆瑕佸疄鐜扮櫨涓囦漢鍚屾椂鍦ㄧ嚎錛岄鍏堣瀹炵幇涓涓瘮杈冨畬鍠勭殑瀹屾垚绔彛鏈嶅姟鍣ㄦā鍨嬶紝榪欎釜妯″瀷瑕佹眰鑷沖皯鍙互鎵胯澆2K鍒?K鐨勫悓鏃跺湪綰跨巼錛堝綋鐒訛紝濡傛灉浣燤ONEY澶氾紝浣犱篃鍙互鍙紑鍙戝嚭鏈澶氬厑璁?00浜哄湪綰跨殑鏈嶅姟鍣級銆傚湪鏋勫緩濂戒簡鍩烘湰鐨勫畬鎴愮鍙f湇鍔″櫒涔嬪悗錛屽氨鏄湁鍏蟲湇鍔″櫒緇勭殑鏋舵瀯璁捐浜嗐備箣鎵浠ヨ榪欐槸涓涓湇鍔″櫒緇勶紝鏄洜涓哄畠緇濅笉浠呬粎鍙槸涓鍙版湇鍔″櫒錛屼篃緇濅笉浠呬粎鏄彧鏈変竴縐嶇被鍨嬬殑鏈嶅姟鍣ㄣ?br>銆銆綆鍗曞湴璇達紝瀹炵幇鐧句竾浜哄悓鏃跺湪綰跨殑鏈嶅姟鍣ㄦā鍨嬪簲璇ユ槸錛氱櫥闄嗘湇鍔″櫒錛嬪ぇ鍘呮湇鍔″櫒錛嬫埧闂存湇鍔″櫒銆傚綋鐒訛紝涔熷彲浠ユ槸鍏跺畠鐨勬ā鍨嬶紝浣嗗叾鍩烘湰鐨勬濇兂鏄竴鏍風殑銆備笅闈紝鎴戝皢閫愪竴浠嬬粛榪欎笁綾繪湇鍔″櫒鐨勫悇鑷綔鐢ㄣ?br>銆銆鐧婚檰鏈嶅姟鍣細涓鑸儏鍐典笅錛屾垜浠細鍚戠帺瀹跺紑鏀捐嫢騫蹭釜鍏紑鐨勭櫥闄嗘湇鍔″櫒錛屽氨濡俀Q鐧婚檰鏃惰浣犻夋嫨鐨勪粠鍝釜QQ娓告垙鏈嶅姟鍣ㄧ櫥闄嗕竴鏍鳳紝QQ鐧婚檰鏃惰鐜╁閫夋嫨鐨勫叚涓湇鍔″櫒鍏ュ彛瀹為檯涓婂氨鏄櫥闄嗘湇鍔″櫒銆傜櫥闄嗘湇鍔″櫒涓昏瀹屾垚璐熻澆騫寵 鐨勪綔鐢ㄣ傝緇嗙偣璇村氨鏄紝鍦ㄧ櫥闄嗘湇鍔″櫒鐨勮儗鍚庯紝鏈塏涓ぇ鍘呮湇鍔″櫒錛岀櫥闄嗘湇鍔″櫒鍙槸鐢ㄤ簬涓哄綋鍓嶇殑瀹㈡埛绔繛鎺ラ夋嫨鍏朵笅涓姝ュ簲璇ヨ繛鎺ュ埌鍝釜澶у巺鏈嶅姟鍣紝褰撶櫥闄嗘湇鍔″櫒涓哄綋鍓嶇殑瀹㈡埛绔繛鎺ラ夋嫨浜嗕竴涓悎閫傜殑澶у巺鏈嶅姟鍣ㄥ悗錛屽鎴風寮濮嬫牴鎹櫥闄嗘湇鍔″櫒鎻愪緵鐨勪俊鎭繛鎺ュ埌鐩稿簲鐨勫ぇ鍘呬笂鍘伙紝鍚屾椂瀹㈡埛绔柇寮涓庣櫥闄嗘湇鍔″櫒鐨勮繛鎺ワ紝涓哄叾浠栫帺瀹跺鎴風榪炴帴鐧婚檰鏈嶅姟鍣ㄨ吘鍑哄鎺ュ瓧璧勬簮銆傚湪璁捐鐧婚檰鏈嶅姟鍣ㄦ椂錛岃嚦灝戝簲璇ユ湁浠ヤ笅鍔熻兘錛歂涓ぇ鍘呮湇鍔″櫒鐨勬瘡涓涓ぇ鍘呮湇鍔″櫒閮借涓庢墍鏈夌殑鐧婚檰鏈嶅姟鍣ㄤ繚鎸佽繛鎺ワ紝騫跺疄鏃跺湴鎶婃湰澶у巺鏈嶅姟鍣ㄥ綋鍓嶇殑鍚屾椂鍦ㄧ嚎浜烘暟閫氱煡緇欏悇涓櫥闄嗘湇鍔″櫒錛岃繖鍏朵腑鍖呮嫭錛氱敤鎴瘋繘鍏ユ椂鐨勫悓鏃跺湪綰夸漢鏁板鍔犱俊鎭互鍙婄敤鎴烽鍑烘椂鐨勫悓鏃跺湪綰夸漢鏁板噺灝戜俊鎭傝繖閲岀殑鍚勪釜澶у巺鏈嶅姟鍣ㄥ悓鏃跺湪綰夸漢鏁頒俊鎭氨鏄櫥闄嗘湇鍔″櫒涓哄鎴風閫夋嫨鏌愪釜澶у巺璁╁叾鐧婚檰鐨勪緷鎹備婦渚嬫潵璇達紝鐜╁A閫氳繃鐧婚檰鏈嶅姟鍣?榪炴帴鍒扮櫥闄嗘湇鍔″櫒錛岀櫥闄嗘湇鍔″櫒寮濮嬩負褰撳墠鐜╁鍦ㄤ紬澶氱殑澶у巺鏈嶅姟鍣ㄤ腑鏍規嵁鍝竴涓ぇ鍘呮湇鍔″櫒浜烘暟姣旇緝灝戞潵閫夋嫨涓涓ぇ鍘咃紝鍚屾椂鎶婅繖涓ぇ鍘呯殑榪炴帴IP鍜岀鍙e彂緇欏鎴風錛屽鎴風鏀跺埌榪欎釜IP鍜岀鍙d俊鎭悗錛屾牴鎹繖涓俊鎭繛鎺ュ埌姝ゅぇ鍘咃紝鍚屾椂錛屽鎴風鏂紑涓庣櫥闄嗘湇鍔″櫒涔嬮棿鐨勮繛鎺ワ紝榪欎究鏄敤鎴風櫥闄嗚繃紼嬩腑錛屽湪鐧婚檰鏈嶅姟鍣ㄨ繖涓鍧楃殑澶勭悊嫻佺▼銆?br>銆銆澶у巺鏈嶅姟鍣細澶у巺鏈嶅姟鍣紝鏄櫘閫氱帺瀹剁湅涓嶅埌鐨勬湇鍔″櫒錛屽畠鐨勮繛鎺P鍜岀鍙d俊鎭槸鐧婚檰鏈嶅姟鍣ㄩ氱煡緇欏鎴風鐨勩備篃灝辨槸璇達紝鍦≦Q娓告垙鐨勬湰鍦版枃浠朵腑錛屽叿浣撶殑澶у巺鏈嶅姟鍣ㄨ繛鎺P鍜岀鍙d俊鎭槸娌℃湁淇濆瓨鐨勩傚ぇ鍘呮湇鍔″櫒鐨勪富瑕佷綔鐢ㄦ槸鍚戠帺瀹跺彂閫佹父鎴忔埧闂村垪琛ㄤ俊鎭紝榪欎簺淇℃伅鍖呮嫭錛氭瘡涓父鎴忔埧闂寸殑綾誨瀷錛屽悕縐幫紝鍦ㄧ嚎浜烘暟錛岃繛鎺ュ湴鍧浠ュ強鍏跺畠濡傛父鎴忓府鍔╂枃浠禪RL鐨勪俊鎭備粠鐣岄潰涓婄湅鐨勮瘽錛屽ぇ鍘呮湇鍔″櫒灝辨槸鎴戜滑杈撳叆鐢ㄦ埛鍚嶅拰瀵嗙爜騫舵牎楠岄氳繃鍚庤繘鍏ョ殑娓告垙鎴塊棿鍒楄〃鐣岄潰銆傚ぇ鍘呮湇鍔″櫒錛屼富瑕佹湁浠ヤ笅鍔熻兘錛氫竴鏄悜褰撳墠鐜╁騫挎挱鍚勪釜娓告垙鎴塊棿鍦ㄧ嚎浜烘暟淇℃伅錛涗簩鏄彁渚涙父鎴忕殑鐗堟湰浠ュ強涓嬭澆鍦板潃淇℃伅錛涗笁鏄彁渚涘悇涓父鎴忔埧闂存湇鍔″櫒鐨勮繛鎺P鍜岀鍙d俊鎭紱鍥涙槸鎻愪緵娓告垙甯姪鐨刄RL淇℃伅錛涗簲鏄彁渚涘叾瀹冩父鎴忚緟鍔╁姛鑳姐備絾鍦ㄨ繖浼楀鐨勫姛鑳戒腑錛屾湁涓鐐規槸鏈涓烘牳蹇冪殑錛屽嵆錛氫負鐜╁鎻愪緵榪涘叆鍏蜂綋鐨勬父鎴忔埧闂寸殑閫氶亾錛岃鐜╁欏哄埄榪涘叆鍏舵榪涘叆鐨勬父鎴忔埧闂淬傜帺瀹舵牴鎹悇涓父鎴忔埧闂村湪綰夸漢鏁幫紝鍒ゅ畾鑷繁榪涘叆鍝竴涓埧闂達紝鐒跺悗鍙屽嚮鏈嶅姟鍣ㄥ垪琛ㄤ腑鐨勬煇涓父鎴忔埧闂村悗鐜╁寮濮嬭繘鍏ユ父鎴忔埧闂存湇鍔″櫒銆?br>銆銆娓告垙鎴塊棿鏈嶅姟鍣細娓告垙鎴塊棿鏈嶅姟鍣紝鍏蜂綋鍦拌灝辨槸濡?#8220;鏂楀湴涓?”錛?#8220;鏂楀湴涓?”榪欐牱鐨勬父鎴忔埧闂淬傛父鎴忔埧闂存湇鍔″櫒鎵嶆槸鍏蜂綋鐨勮礋璐f墽琛屾父鎴忕浉鍏抽昏緫鐨勬湇鍔″櫒銆傝繖鏍風殑娓告垙閫昏緫鍒嗕負涓ゅぇ綾伙細涓綾繪槸閫氱敤鐨勬父鎴忔埧闂撮昏緫錛屽錛氳繘鍏ユ埧闂達紝紱誨紑鎴塊棿錛岃繘鍏ユ瀛愶紝紱誨紑妗屽瓙浠ュ強鍦ㄦ埧闂村唴璇磋瘽絳夛紱絎簩綾繪槸娓告垙妗屽瓙閫昏緫錛岃繖涓氨鏄悇縐嶄笉鍚岀被鍨嬫父鎴忕殑涓昏鍖哄埆涔嬪浜嗭紝姣斿鏂楀湴涓諱腑鐨勫彨鍦頒富鎴栦笉鍙湴涓葷殑閫昏緫絳夛紝褰撶劧錛屾父鎴忔瀛愰昏緫閲屼篃鍖呮嫭鏈夐氱敤鐨勫悇涓父鎴忛噷閮藉瓨鍦ㄧ殑娓告垙閫昏緫錛屾瘮濡傚湪妗屽瓙鍐呰璇濈瓑銆傛諱箣錛屾父鎴忔埧闂存湇鍔″櫒鎵嶆槸鐪熸璐熻矗鎵ц娓告垙鍏蜂綋閫昏緫鐨勬湇鍔″櫒銆?br>銆銆榪欓噷鎻愬埌鐨勪笁綾繪湇鍔″櫒錛屾垜鍧囬噰鐢ㄧ殑鏄畬鎴愮鍙fā鍨嬶紝姣忎釜鏈嶅姟鍣ㄦ渶澶氳繛鎺ユ暟鐩槸5000浜猴紝浣嗘槸錛屾垜鍦ㄦ父鎴忔埧闂存湇鍔″櫒涓婁綔浜嗛昏緫灞傜殑闄愬畾錛屾渶澶氬彧鍏佽300浜哄悓鏃跺湪綰褲傚叾浠栦袱涓湇鍔″櫒浠嶇劧鍏佽鏈澶?000浜虹殑鍚屾椂鍦ㄧ嚎銆傚鏋滄寜鐓ц繖鏍風殑緇撴瀯鏉ヨ璁★紝閭d箞瑕佸疄鐜扮櫨涓囦漢鐨勫悓鏃跺湪綰垮氨搴旇鏄繖鏍鳳細棣栧厛鏄ぇ鍘咃紝1000000/5000錛?00銆備篃灝辨槸璇達紝鑷沖皯瑕?00鍙板ぇ鍘呮湇鍔″櫒錛屼絾閫氬父鎯呭喌涓嬶紝鑰冭檻鍒板疄闄呬嬌鐢ㄦ椂鏈嶅姟鍣ㄧ殑澶勭悊鑳藉姏鍜岃礋杞芥儏鍐碉紝搴旇鑷沖皯鍑嗗250鍙板乏鍙崇殑澶у巺鏈嶅姟鍣ㄧ▼搴忋傚彟澶栵紝鍏蜂綋鐨勫悇縐嶇被鍨嬬殑娓告垙鎴塊棿鏈嶅姟鍣ㄩ渶瑕佸灝戯紝灝辮鏍規嵁褰撳墠鐜╁悇縐嶇被鍨嬫父鎴忕殑鐜╁鏁扮洰鍒嗗埆璁$畻浜嗭紝姣斿鏂楀湴涓繪渶澶氭槸鍗佷竾浜哄悓鏃跺湪綰匡紝姣忓彴鏈嶅姟鍣ㄦ渶澶氬厑璁?00浜哄悓鏃跺湪綰匡紝閭d箞闇瑕佺殑鏂楀湴涓繪湇鍔″櫒鏁扮洰灝卞簲璇ヤ笉灝戜簬錛?00000/300=333錛屽噯澶囧緱鍏呭垎涓鐐癸紝灝辮鍑嗗350鍙版枟鍦頒富鏈嶅姟鍣ㄣ?br>銆銆闄ゆ甯哥殑鐜╁榪炴帴澶栵紝榪樿鑰冭檻鍒幫細
銆銆瀵逛簬鐧婚檰鏈嶅姟鍣紝浼氭湁250鍙板ぇ鍘呮湇鍔″櫒榪炴帴鍒版瘡涓櫥闄嗘湇鍔″櫒涓婏紝榪欐槸濮嬬粓閮借淇濇寔鐨勮繛鎺ワ紱
銆銆鑰屽浜庡ぇ鍘呮湇鍔″櫒鑰岃█錛屽鏋滀粎浠呮湁鏂楀湴涓昏繖涓綾葷殑鏈嶅姟鍣紝灝辮鏈?50澶氫釜榪炴帴涓庡悇涓ぇ鍘呮湇鍔″櫒濮嬬粓淇濇寔鐫銆傛墍浠ヤ粠榪欎竴鐐圭湅錛屾垜鐨勭粨鏋勫湪鏌愪簺鏂歸潰榪樺瓨鍦ㄧ潃闇瑕佹敼榪涚殑鍦版柟錛屼絾鏍稿績鎬濇兂鏄細灝藉揩鍦版彁渚涚敤鎴風櫥闄嗙殑閫熷害錛屽敖鍙兘鏂逛究鍦拌鐜╁榪涘叆娓告垙涓?
]]>
]]>
鎽樿嚜錛?a class=headermaintitle id=Header1_HeaderTitle >娌愭灚灝忕瓚 錛堝崥瀹㈠洯鍗氬錛?br>
杞悜.NET鍚庯紝鎵嬪ご涓婂線寰浠嶆湁鏃х殑妯″潡瑕侀噸鐢ㄣ備篃璁歌繖浜涙ā鍧楁槸Delphi鍐欑殑錛屼篃璁告槸C/C++鍐欑殑錛屾垨鑰呮槸鍏跺畠緙栫▼璇█……涓轟簡鑳芥妸瀹冧滑縐繪鍒?NET涓嬶紝鎴栬呮槸鍦?NET涓皟鐢紝To be or not to be, that is a question銆?br>銆銆鍦ㄨ繖閲岋紝鎴戠瑪璁頒簡鍑犱釜鍦ㄥ伐浣滀腑閬囧埌鐨勫嚑涓満鏅備笉榪囷紝榪欓噷涓嶅寘鎷畬鍏ㄤ嬌鐢–#鏉ラ噸鍐欏師鏉ョ敤C++緙栧啓鐨勭▼搴忚繖縐嶅彉鎬佺殑闇姹傘傚綋浣犺瑕佹眰鍋氳繖縐嶄簨鐨勬椂鍊欙紝璇蜂笁鎬濊屽悗琛?#8230;…榪欑畝鐩存槸縐嶉潪浜虹殑鎶樼(銆?br>
鎮ㄤ篃浣跨敤鎵樼C++鍚楋紵 銆濡傛矏鏋灄
銆銆鍦烘櫙涓錛氬湪.NET涓皟鐢╓indowsAPI鎴朌LL銆?br>
銆銆榪欐槸姣旇緝鏅亶鐨勯渶姹傘備竴鑸潵璇達紝綆鍗曠殑鍑芥暟璋冪敤錛屽ぇ鍙洿鎺ョ敤C#/VB.NET錛岀粡榪嘍llImport灞炴у寘瑁呭嚭鍑芥暟鏉ヨ皟鐢ㄣ傚錛?
[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);
銆銆鐢變簬WindowsAPI鐢ㄥ埌鐨勪漢瀹炲湪鏄錛屽洜姝ゆ湁涓涓笓闂ㄧ殑wiki绔欑偣錛屾敹闆嗚繖鏂歸潰鐨勮祫鏂欙細http://www.pinvoke.net/錛屽浜庡父鐢ㄧ殑鍑芥暟鐢氳嚦鏈夊畬鏁寸殑搴旂敤渚嬪瓙鍜屽府鍔┿傚綋鐒訛紝濡傛灉浣犳湁鐩稿簲鐨勮祫鏂欏拰渚嬪瓙錛屼綘涔熷彲浠ヨ礎鐚綘鐨勫姏閲忥紝緇欏叾瀹冧漢甯姪銆?br>
銆銆鍦烘櫙浜?/strong>錛氱敤鎵樼C++鍖呰鐜版湁鐨凞LL錛屼緵C#璋冪敤
銆銆褰撳嚱鏁扮殑鍙傛暟鎴栬繑鍥炲兼瘮杈冨鏉傦紝鎴栧嚱鏁版瘮杈冨鐨勬椂鍊欙紝榪欑鏂規硶瀵逛笌浜烘潵璇達紝瀹炲湪鏄竴涓姌紓ㄣ傚父甯歌繖浜涙帴鍙e拰瀹氫箟灝辮鐢ㄦ帀鍑犲崈琛岀殑浠g爜錛岃屼笖榪樹笉鑳戒繚璇佹槸姝g‘鐨勩傝繖浜涢敊璇線寰鍦ㄨ繍琛屾椂鎵嶈兘鏄劇幇鍑烘潵錛岀敋鑷蟲湁浜涢敊璇細寮曡搗鍐呭瓨娉勬紡錛屾垨鍏跺畠鏇翠負闅愯斀鐨勯敊璇?br>銆銆鍦ㄨ繖縐嶆儏鍐典笅錛屼嬌鐢–++/Managed浠g爜鏉ュ寘瑁咃紝灝辨垚浜嗘渶鍚堢悊鐨勯夋嫨銆傚洜涓烘墭綆++浠g爜鍙互鐩存帴寮曠敤鍘熸湁鐨勫ご鏂囦歡錛岀洿鎺ヨ皟鐢ㄩ潪鎵樼鍑芥暟錛岃屼笉闇瑕佸0鏄庛傝繖鏍鳳紝鏃㈠噺灝戜簡宸ヤ綔閲忥紝鍙堥伩鍏嶅紩鍏ラ敊璇傜己鐐規槸錛岃繖縐嶆柟娉曚細澧炲姞涓涓狣LL銆?span style="COLOR: #ff0000">瑕佹敞鎰忕殑鏄墭綆″瓧絎︿覆鍜岄潪鎵樼瀛楃涓叉槸鏈夊尯鍒殑錛屽茍闇瑕佽漿鎹紙鐗瑰埆瑕佹敞鎰忕殑Unicode瀛楃涓插拰澶氬瓧鑺傚瓧絎︿覆鐨勮漿鎹級銆?br>
銆銆浠嶄互MoveFile涓轟緥鍚э紝榪欐牱姣旇緝綆鍗曪細
#include <windows.h>
#include <vcclr.h>
using namespace System;
namespace wrapper

{
public ref class ApiWrapper
{
public:
bool static MoveFile(String ^ lpExistingFileName, String ^ lpNewFileName )
{
pin_ptr<const wchar_t> src = PtrToStringChars(lpExistingFileName);
pin_ptr<const wchar_t> dst = PtrToStringChars(lpNewFileName);
return ::MoveFile(src, dst);
}
};
}
銆銆鐒跺悗鍦–#涓紝寮曠敤涓婇潰浠g爜鐢熸垚鐨凞LL鏂囦歡錛屽氨鍙互鐩存帴璋冪敤浜嗭細
wrapper.ApiWrapper.MoveFile(@"c:\debug.log", @"c:\debug.txt");
銆銆鍋囧鍘熸湁鐨勪唬鐮佹槸鍩轟簬COM鐨勶紝閭d箞澶ソ浜嗭紝VisualStudio絳塈DE浼氳嚜鍔ㄧ敓鎴愪竴涓敤浜庡寘瑁呯殑dll錛屼緵浣犺皟鐢ㄣ傚綋鐒跺洜鐗規畩闇瑕佽屾墜宸ョ紪鐮佺殑鏄彟涓鍥炰簨銆?br>
銆銆鍦烘櫙涓?/strong>錛氱幇鏈塁++鍘熶唬鐮侊紝鍖呰鍚庝緵C#璋冪敤銆?br>
銆銆C++鐨勫師浠g爜錛屽疄闄呬笂鍙互鐩存帴緙栬瘧鎴愭墭綆′唬鐮併侻FC涔熷ソATL涔熷ソ……榪欐牱鐪嬭搗鏉ュ湪.NET涓渶寮哄ぇ鐨勭紪紼嬭璦灝辨槸C++浜嗭細瀹冧笉浠呭彲浠ョ紪鍐欐墭綆$▼搴忥紝鐢氳嚦鍙互灝嗘爣鍑咰++鐨勪唬鐮佷篃緙栬瘧鎴愭墭綆$▼搴忥紒鍏跺疄VC++鏈寮哄ぇ鐨勫湴鏂逛笉姝㈠姝わ紝瀹冭繕鍦ㄤ簬鑳藉緙栧啓娣峰悎浜嗘墭綆″拰闈炴墭綆$殑浠g爜鐨勭▼搴忥紒錛侊紒榪欐牱鏈澶х殑濂藉涓嶄粎鍙互灝嗗叧閿唬鐮佺洿鎺ョ紪璇戞垚闈炴墭綆$殑浠g爜錛岃繕鍙互閬垮厤琚弽緙栬瘧銆?br>銆銆
銆銆鍋囪鐜版湁C++浠g爜濡備笅錛?br>

class UnmanagedClass
{
public:
LPCWSTR GetPropertyA()
{ return L"Hello!"; }
void MethodB( LPCWSTR )
{}
};
namespace wrapper

{
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;
};
}
銆銆鐒跺悗錛屾敼鍙樼紪璇戦夐」涓?#8220;浣跨敤鍏叡璇█鎵╁睍 /clr”灝卞彲浠ヤ簡銆傝繖鏍鳳紝鎴戜滑鎶婁唬鐮佺紪璇戞垚DLL鏂囦歡灝卞彲浠ヤ緵.NET鍏跺畠璇█璋冪敤浜嗐?br>銆銆鏈鍚庯紝C#涓彲浠ヨ薄濡備笅鐨勪唬鐮佷竴鏍瘋皟鐢–++綾諱簡錛?
ManagedClass mc = new ManagedClass();
mc.MethoB("Hello");
string s = mc.get_PropertyA;
銆銆鍦烘櫙鍥?/strong>錛氬浣曞湪鎵樼C++浠g爜涓販鍚堟墭綆″拰闈炴墭綆′唬鐮?br>
銆銆寰堢畝鍗曪紝鍙浠?pragma unmanaged緙栬瘧鎸囩ず寮濮嬬殑紼嬪簭錛屼竴鐜囩紪璇戞垚闈炴墭綆′唬鐮侊紱瑕佹兂鎭㈠鎴愭墭綆′唬鐮侊紝鍙浣跨敤#pragma managed灝卞彲浠ヤ簡銆傚錛?br>銆銆
#pragma unmanaged
#include <iostream>
using namespace std;
template<typename T>
void f(T t)
{
cout << t << endl;
}
#pragma managed
using namespace System;

void m(String ^ s)
{
Console::WriteLine(s);
}

void main()
{
f("Hello");
m("World");
}
銆銆鐢熸垚exe鏂囦歡鍚庯紝鐢ㄥ弽緙栬瘧紼嬪簭鏌ョ湅 f 鍑芥暟錛?
[PreserveSig, MethodImpl(MethodImplOptions.Unmanaged, MethodCodeType=MethodCodeType.Native), SuppressUnmanagedCodeSecurity]
public static unsafe void modopt(CallConvCdecl) f<char const *>(sbyte modopt(IsSignUnspecifiedByte) modopt(IsConst)*);
銆銆鐪嬩笉鍒版簮鐮侊紝鑰屾柟娉曞睘鎬ф爣璁頒負Unmanaged銆?br>銆銆濡傛灉娌℃湁鍔犱笂#pragma unmanaged錛屽弽緙栬瘧寰楀埌鐨?f 鍑芥暟涓猴細
internal static unsafe void modopt(CallConvCdecl) f<char const *>(sbyte modopt(IsSignUnspecifiedByte) modopt(IsConst)* t)

{
std.basic_ostream<char,std::char_traits<char> >.<<(std.operator<<<struct std::char_traits<char> >(*((basic_ostream<char,std::char_traits<char> >* modopt(IsImplicitlyDereferenced)*) &__imp_std.cout), t), (basic_ostream<char,std::char_traits<char> >* modopt(IsImplicitlyDereferenced) modopt(CallConvCdecl) *(basic_ostream<char,std::char_traits<char> >* modopt(IsImplicitlyDereferenced))) __unep@?endl@std@@$$FYAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z);
}
銆銆鍏朵腑鐨勫嚱鏁板唴瀹逛竴鐩簡鐒躲傚鏋滀綘鐨勫嚱鏁版病鏈夎皟鐢╫perator絳変笉濂界悊瑙g殑綾誨簱錛岄偅涔堝弽緙栬瘧鍑烘潵鐨勪唬鐮佺畝鐩村拰婧愮爜娌″樊鍒?nbsp;
銆銆寮蹇冧竴鍒?/strong>錛氭垜鍙細C++涓嶆噦.NET涓嶆噦C#錛屾庝箞緙栧啓.NET紼嬪簭錛?br>
銆銆寰堢畝鍗曪紝浣犵収鏍風敤浣犵殑C++鍐欎綘鐨勭▼搴忥紝鐒跺悗嫻嬭瘯娌℃湁閿欒鍚庯紝灝嗙紪璇戦夐」鏀逛負/clr錛屽ソ浜嗭紝Rebuild錛屼綘鐨勭▼搴忕幇鍦ㄦ槸.NET浜嗐?br>
銆銆鎭舵悶錛?#8220;鎴戞兂闂竴涓嬶紝鍦ㄨ兘灝嗙幇鏈夌殑C錛嬶紜浠g爜鐩存帴榪涜灝佽錛岃C錛冭繘琛岃皟鐢紝鑰屼笉鏄幓璋冪敤DLL錛屼篃灝辨槸涓嶇敓鎴怐LL錛屽氨鍦–錛冧笅鑳界洿鎺ヨ皟鐢╒C鐨勫伐紼嬫簮鏂囦歡涓嶏紵”
銆銆鎴戞兂錛屾彁闂殑浜烘槸涓嶆槸鎸囷紝鐜版湁c++婧愮爜錛屼絾涓嶆兂璐瑰姴鍘昏漿鎹㈡垚C#婧愮爜錛屼絾鍙堟兂鑳戒笌C#涓璧風紪璇戙?br>銆銆浜庢槸鎴戝氨緇欎簡涓涓瀬鍏跺彉鎬佺殑鏂規硶錛屼笉榪囷紝涓漢鏄笉寤鴻浣跨敤榪欑鍙樻佺殑鏂規硶鍟娿傛柟娉曞涓嬶細
銆銆錛戙鍏堝皢C++婧愮爜錛屾敼鐢–LR緙栬瘧閫夐」錛岀紪璇戞垚.NET鐨凙ssembly錛圖LL鏂囦歡錛夈?
銆銆錛掋鐒跺悗鐢╮eflector絳夊弽緙栬瘧杞歡錛屽弽緙栬瘧鎴怌#浠g爜錛屽茍瀵煎嚭錛坮eflector鏈変笓闂ㄧ殑瀵煎嚭鎻掍歡錛夈?
銆銆錛撱灝嗗鍑虹殑C#浠g爜錛屾坊鍔犱笂鏂板啓鐨凜#浠g爜涓璧風紪璇戙?
銆銆
銆銆榪欑鏂規硶鐢熸垚鐨勪唬鐮佸緢鏄亹鎬栵紝寮虹儓寤鴻涓嶈鎶奀++婧愮爜灝辮繖涔堜涪浜嗭紝鍚﹀垯鍚庢灉鑷礋銆?br>
銆銆鍦烘櫙浜?/strong>錛氫笉鎯寵DLL錛岃兘涓嶈兘鐩存帴鎶奀++婧愪唬鐮佷笌C#婧愪唬鐮佷竴璧風紪璇戞垚涓涓崟鐙殑Assembly鍛紵
-------
閮ㄤ喚渚嬪瓙鏉ヨ嚜MSDN.
]]>
]]>
By Mark Strawmyer
[婧怾 http://www.developer.com/net/cplus/article.php/10919_1479761_1
What is .NET Remoting?
.NET Remoting versus Distributed COM
.NET Remoting versus Web Services
Channels
Create a Remotable Object
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample object to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleObject : MarshalByRefObject
{
/// <summary>
/// Constructor
/// </summary>
public SampleObject()
{
}
/// <summary>
/// Return a hello message
/// </summary>
/// <returns>Hello world message</returns>
public string HelloWorld()
{
return "Hello World!";
}
}
}
Create a Server To Expose the Remotable Object
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample server to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleServer
{
public static int Main(string [] args)
{
// Create an instance of a channel
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
// Register as an available service with the name HelloWorld
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SampleObject),
"HelloWorld",
WellKnownObjectMode.SingleCall );
System.Console.WriteLine("Press the enter key to exit...");
System.Console.ReadLine();
return 0;
}
}
}
Create a Client To Use the Remotable Object
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace CodeGuru.Remoting
{
/// <remarks>
/// Sample client to demonstrate the use of .NET Remoting.
/// </remarks>
public class SampleClient
{
public static int Main(string [] args)
{
// Create a channel for communicating w/ the remote object
// Notice no port is specified on the client
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
// Create an instance of the remote object
SampleObject obj = (SampleObject) Activator.GetObject(
typeof(CodeGuru.Remoting.SampleObject),
"tcp://localhost:8080/HelloWorld" );
// Use the object
if( obj.Equals(null) )
{
System.Console.WriteLine("Error: unable to locate server");
}
else
{
Console.WriteLine(obj.HelloWorld());
}
return 0;
}
}
}
Test the Remoting Sample
Summary
Future Columns
About the Author
]]>
浣滆咃細TomMax (絎戞湜浜虹敓) 鍑哄錛歝sdn community
鏈夎澶氫漢闂殑錛?Net涓殑濮旀墭浠ュ強浜嬩歡澶勭悊銆傛垜鎷跨畝鍗曠殑渚嬪瓙璇存槑涓涓嬶紝鏄幇瀹炰腑鐨勪緥瀛愶細
姣斿璇翠竴涓叕鍙革紙鍦烘櫙錛夛紝浣犳槸鑰佹澘錛屾墜涓嬫湁涓や釜鍛樺伐錛屽皬寮犲拰灝忕帇銆?br>浣犲懡浠ゅ皬鐜嬶紝濡傛灉灝忓紶鐜╂父鎴忥紝鍒欏皬鐜嬫墸鍘誨皬寮?00鍏冮挶銆?/p>
榪欏氨鏄幇瀹炰腑鐨勫鎵樸?/p>
瀹為檯涓婏紝鍦ㄥ啓紼嬪簭涓紝紼嬪簭鍛樺氨鏄佹澘錛屽皬寮犲拰灝忕帇灝辨槸涓や釜瀵硅薄銆傚皬寮犵帺娓告垙鏄竴涓柟娉曪紝灝忓紶榪樻湁涓涓父鎴忎簨浠訛紝浠栫帺娓告垙嬋鍙戣繖涓簨浠躲傝屽皬鐜嬪氨鏄簨浠跺鐞嗗璞★紝浠栬礋璐f妸灝忓紶鐨勯挶鎵i櫎500銆?/p>
鎵浠ワ紝濮旀墭鏈夊涓嬪嚑涓绱狅細
1 嬋鍙戜簨浠剁殑瀵硅薄--灝辨槸灝忓紶
2 澶勭悊瀵硅薄浜嬩歡鐨勫璞?-灝辨槸灝忕帇
3 瀹氫箟濮旀墭錛屽氨鏄綘璁╁皬鐜嬬洃瑙嗗皬寮犮?/p>
濡傛灉榪欎笁涓绱犻兘婊¤凍鐨勮瘽錛屽垯浣犲氨鍐欏嚭浜嗕竴涓畬鏁翠簨浠剁殑澶勭悊銆?/p>
涓嬮潰鏈変釜渚嬪瓙錛氬湪vs.net2003 C#鎺у埗鍙板簲鐢ㄧ▼搴忕紪杈戣繍琛屾垚鍔燂細
using System;
namespace CSharpConsole
{
public class 鍦烘櫙
{
[STAThread]
public static void Main(string[] args)
{
Console.WriteLine("鍦烘櫙寮濮嬩簡....");
// 鐢熸垚灝忕帇
灝忕帇 w = new 灝忕帇();
// 鐢熸垚灝忚處
灝忓紶 z = new 灝忓紶();
// 鎸囧畾鐩戣
z.PlayGame += new PlayGameHandler(w.鎵i挶);
// 寮濮嬬帺娓告垙
z.鐜╂父鎴?);
console.writeline("鍦烘櫙緇撴潫...");
Console.ReadLine();
}
}
// 璐熻矗鎵i挶鐨勪漢
public class 灝忕帇
{
public 灝忕帇()
{
Console.WriteLine("鐢熸垚灝忕帇...");
}
public void 鎵i挶(object sender,EventArgs e)
{
Console.WriteLine("灝忕帇錛氬ソ灝忓瓙錛屼笂鐝椂闂磋儐鏁㈢帺娓告垙...");
Console.WriteLine("灝忕帇錛氱湅鐪嬩綘灝忓瓙鏈夊灝戦挶...");
灝忓紶 f = (灝忓紶)sender;
Console.WriteLine("灝忓紶鐨勯挶錛?" + f.閽?ToString());
Console.WriteLine("寮濮嬫墸閽?.....");
System.Threading.Thread.Sleep(500);
f.閽?= f.閽?- 500;
Console.WriteLine("鎵e畬浜?...鐜板湪灝忓紶榪樺墿涓嬶細" + f.閽?ToString());
}
}
// 濡傛灉鐜╂父鎴忥紝鍒欏紩鍙戜簨浠?br> public class 灝忓紶
{
// 鍏堝畾涔変竴涓簨浠訛紝榪欎釜浜嬩歡琛ㄧず“灝忓紶”鍦ㄧ帺娓告垙銆?br> public event PlayGameHandler PlayGame;
// 淇濆瓨灝忓紶閽辯殑鍙橀噺
private int m_Money;
public 灝忓紶()
{
Console.WriteLine("鐢熸垚灝忓紶....");
m_Money = 1000; // 鏋勯犲嚱鏁幫紝鍒濆鍖栧皬寮犵殑閽便?br> }
public int 閽?// 姝ゅ睘鎬у彲浠ユ搷浣滃皬寮犵殑閽便?br> {
get
{
return m_Money;
}
set
{
m_Money = value;
}
}
public void 鐜╂父鎴?)
{
Console.WriteLine("灝忓紶寮濮嬬帺娓告垙浜?....");
Console.WriteLine("灝忓紶:CS濂界帺錛屽搱鍝堝搱錛?鎴戠帺.....");
System.Threading.Thread.Sleep(500);
System.EventArgs e = new EventArgs();
OnPlayGame(e);
}
protected virtual void OnPlayGame(EventArgs e)
{
if(PlayGame != null)
{
PlayGame(this,e);
}
}
}
// 瀹氫箟濮旀墭澶勭悊紼嬪簭
public delegate void PlayGameHandler(object sender,System.EventArgs e);
}