C# API ´óÈ«
C:\ProgramFiles\MicrosoftVisual Studio .NET\ FrameworkSDK\Samples\ Technologies\ Interop\PlatformInvoke\ WinAPIs\CSĿ¼ÏÂÓдóÁ¿µÄµ÷ÓÃAPIµÄÀý×Ó¡£
Ò»¡¢µ÷Óøñʽ
using System.Runtime.InteropServices; //ÒýÓôËÃû³Æ¿Õ¼ä£¬¼ò»¯ºóÃæµÄ´úÂë
//ʹÓÃDllImportAttributeÌØÐÔÀ´ÒýÈëapiº¯Êý£¬×¢ÒâÉùÃ÷µÄÊÇ¿Õ·½·¨£¬¼´·½·¨ÌåΪ¿Õ¡£
[DllImport("user32.dll")]
public static extern ReturnType FunctionName(type arg1,type arg2,...);
//µ÷ÓÃʱÓëµ÷ÓÃÆäËû·½·¨²¢ÎÞÇø±ð
¿ÉÒÔʹÓÃ×ֶνøÒ»²½ËµÃ÷ÌØÐÔ£¬ÓöººÅ¸ô¿ª£¬È磺
[ DllImport( "kernel32", EntryPoint="GetVersionEx" )]
DllImportAttributeÌØÐԵĹ«¹²×Ö¶ÎÈçÏ£º
1¡¢CallingConvention ָʾÏò·ÇÍйÜʵÏÖ´«µÝ·½·¨²ÎÊýʱËùÓÃµÄ CallingConvention Öµ¡£
CallingConvention.Cdecl : µ÷Ó÷½ÇåÀí¶ÑÕ»¡£ËüʹÄúÄܹ»µ÷ÓþßÓÐ varargs µÄº¯Êý¡£
CallingConvention.StdCall : ±»µ÷Ó÷½ÇåÀí¶ÑÕ»¡£ËüÊÇ´ÓÍйܴúÂëµ÷Ó÷ÇÍйܺ¯ÊýµÄĬÈÏÔ¼¶¨¡£
2¡¢CharSet ¿ØÖƵ÷Óú¯ÊýµÄÃû³Æ°æ±¾¼°Ö¸Ê¾ÈçºÎÏò·½·¨·âËÍ String ²ÎÊý¡£
´Ë×ֶα»ÉèÖÃΪ CharSet ÖµÖ®Ò»¡£Èç¹û CharSet ×Ö¶ÎÉèÖÃΪ Unicode£¬ÔòËùÓÐ×Ö·û´®²ÎÊýÔÚ´«µÝµ½·ÇÍйÜʵÏÖ֮ǰ¶¼×ª»»³É Unicode ×Ö·û¡£Õ⻹µ¼ÖÂÏò DLL EntryPoint µÄÃû³ÆÖÐ×·¼Ó×Öĸ“W”¡£Èç¹û´Ë×Ö¶ÎÉèÖÃΪ Ansi£¬Ôò×Ö·û´®½«×ª»»³É ANSI ×Ö·û´®£¬Í¬Ê±Ïò DLL EntryPoint µÄÃû³ÆÖÐ×·¼Ó×Öĸ“A”¡£´ó¶àÊý Win32 API ʹÓÃÕâÖÖ×·¼Ó“W”»ò“A”µÄÔ¼¶¨¡£Èç¹û CharSet ÉèÖÃΪ Auto£¬ÔòÕâÖÖת»»¾ÍÊÇÓëƽ̨Óйصģ¨ÔÚ Windows NT ÉÏΪ Unicode£¬ÔÚ Windows 98 ÉÏΪ Ansi£©¡£CharSet µÄĬÈÏֵΪ Ansi¡£CharSet ×Ö¶ÎÒ²ÓÃÓÚÈ·¶¨½«´ÓÖ¸¶¨µÄ DLL µ¼ÈëÄĸö°æ±¾µÄº¯Êý¡£CharSet.Ansi ºÍ CharSet.Unicode µÄÃû³ÆÆ¥Åä¹æÔò´ó²»Ïàͬ¡£¶ÔÓÚ Ansi À´Ëµ£¬Èç¹û½« EntryPoint ÉèÖÃΪ“MyMethod”ÇÒËü´æÔڵĻ°£¬Ôò·µ»Ø“MyMethod”¡£Èç¹û DLL ÖÐûÓГMyMethod”£¬µ«´æÔÚ“MyMethodA”£¬Ôò·µ»Ø“MyMethodA”¡£¶ÔÓÚ Unicode À´ËµÔòÕýºÃÏà·´¡£Èç¹û½« EntryPoint ÉèÖÃΪ“MyMethod”ÇÒËü´æÔڵĻ°£¬Ôò·µ»Ø“MyMethodW”¡£Èç¹û DLL Öв»´æÔÚ“MyMethodW”£¬µ«´æÔÚ“MyMethod”£¬Ôò·µ»Ø“MyMethod”¡£Èç¹ûʹÓõÄÊÇ Auto£¬ÔòÆ¥Åä¹æÔòÓëƽ̨Óйأ¨ÔÚ Windows NT ÉÏΪ U
Ïà¹ØÎĵµ£º
ö¾Ù
¡¡¡¡Ã¶¾ÙÀàÐÍÉùÃ÷Ϊһ×éÏà¹ØµÄ·ûºÅ³£Êý¶¨ÒåÁËÒ»¸öÀàÐÍÃû³Æ¡£Ã¶¾ÙÓÃÓÚ“¶àÏîÑ¡Ôñ”³¡ºÏ£¬¾ÍÊdzÌÐòÔËÐÐʱ´Ó±àÒëʱÒѾÉ趨µÄ¹Ì¶¨ÊýÄ¿µÄ“Ñ¡Ôñ”ÖÐ×ö³ö¾ö¶¨¡£
¡¡¡¡Ã¶¾ÙÀàÐÍ£¨Ò²³ÆΪö¾Ù£©Îª¶¨ÒåÒ»×é¿ÉÒÔ¸³¸ø±äÁ¿µÄÃüÃûÕûÊý³£Á¿ÌṩÁËÒ»ÖÖÓÐЧµÄ·½·¨¡£ÀýÈ磬¼ÙÉèÄú±ØÐ붨ÒåÒ»¸ö±äÁ¿£¬¸Ã±äÁ¿ ......
±äÁ¿¡¢³£Á¿¼°±í´ïʽ±äÁ¿ºÍ³£Á¿±äÁ¿(¾²Ì¬¡¢·Ç¾²Ì¬¡¢Êý×éÔªËØ¡¢Öµ²ÎÊý¡¢ÒýÓòÎÊý¡¢Êä³ö²ÎÊý¡¢¾Ö²¿±äÁ¿)¾²Ì¬(static) Èç public static int x; Ò»µ©¾²Ì¬±äÁ¿ËùÊôµÄÀà±»×°ÔØ£¬Ö±µ½°üº¬¸ÃÀàµÄ³ÌÐòÔËÐнáÊøʱËüÒ»Ö±´æÔÚ¡£·Ç¾²Ì¬£º²»´øÓÐstaticÐÞÊηûÉùÃ÷µÄ±äÁ¿³ÆΪʵÀý±äÁ¿£¬Èçint a ;³£Á¿(attributes constnt-modifiers CO ......
//TransmitFileʵÏÖÏÂÔØ
protected void Button1_Click(object sender, EventArgs e)
{
/*
΢ÈíΪResponse¶ÔÏóÌṩÁËÒ»¸öеķ½·¨TransmitFileÀ´½â¾öʹÓÃRespo ......
תÒåÐòÁÐ ×Ö·ûÃû³Æ Unicode ±àÂë
\' µ¥ÒýºÅ 0x0027
\" Ë«Òýº ......
(1)stringÊÇÒýÓÃÀàÐÍ£º
stringÊÇÒýÓÃÀàÐÍ£¬Õâ¾ÍÊÇ˵stringÔÚ¶ÑÉϱ£´æÊý¾Ý£¬¶øÔÚÕ»Öд洢µÄÊǶÔÏóµØÖ·£¬ÔÚÀí½âstringÀàÐÍÌØÐÔʱÊ×ÏÈÐèÒª¼ÇסÕâµã¡£Ö»²»¹ýÓëÆäËüһЩÒýÓÃÀàÐÍÏà±È£¬ËüÓÖÓÐһЩ±È½ÏÌØÊâµÄÌØÐÔ£¬±ÈÈçÏÂÃæµÄÕâЩ£º
(2)stringÊDz»¿É±äµÄ(immutable)£º
stringµÄÕâÒ»ÌØÐÔÊÇÖ¸£¬¶ÔÓÚÒÑÓеÄÒ»¸östring¶ÔÏ󣬵±ÄãÐÞ¸ ......