Dacă sunteți programator C# probabil ați observat că foarte multe funcții de sistem care există in C++ nu mai există in C# iar pentru a le putea folosi trebuie să faceți tot felul de importări de dll-uri, să scrieți diferite structuri inexistente in C# care necesită atribute ciudate într-un cuvânt unmanaged code.
Foarte mulți oameni s-au confruntat cu această problemă a inexistenței unor funcții in C# iar Microsoft a scos acum un tool care să ajute exact acele persoane care au avut această problemă.
Tool-ul se numeste P/Invoke Interop Assistant.Știe să facă conversia automată a semnăturilor de funcții și chiar știe să scrie corect structurile necesare pentru a folosi o anumită funcție din C# sau Visual Basic. Mi-a plăcut de asemenea că are un mod de căutare a funcțiilor ceea ce înseamnă că nu este neapărat nevoie să țin minte în ce dll se găsesc anumite funcții.
A must have pentru toți programatorii în C#.
Iată și un exemplu de cod generat pentru funcția ReadFileEx
/// Return Type: void
///dwErrorCode: DWORD->unsigned int
///dwNumberOfBytesTransfered: DWORD->unsigned int
///lpOverlapped: LPOVERLAPPED->_OVERLAPPED*
public delegate void LPOVERLAPPED_COMPLETION_ROUTINE(
uint dwErrorCode, uint dwNumberOfBytesTransfered,
ref OVERLAPPED lpOverlapped);
[System.Runtime.InteropServices.StructLayoutAttribute(
System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct OVERLAPPED {
/// ULONG_PTR->unsigned int
public uint Internal;
/// ULONG_PTR->unsigned int
public uint InternalHigh;
/// Anonymous_7416d31a_1ce9_4e50_b1e1_0f2ad25c0196
public Anonymous_7416d31a_1ce9_4e50_b1e1_0f2ad25c0196 Union1;
/// HANDLE->void*
public System.IntPtr hEvent;
}
[System.Runtime.InteropServices.StructLayoutAttribute(
System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct Anonymous_7416d31a_1ce9_4e50_b1e1_0f2ad25c0196 {
/// Anonymous_ac6e4301_4438_458f_96dd_e86faeeca2a6
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public Anonymous_ac6e4301_4438_458f_96dd_e86faeeca2a6 Struct1;
/// PVOID->void*
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public System.IntPtr Pointer;
}
[System.Runtime.InteropServices.StructLayoutAttribute(
System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct Anonymous_ac6e4301_4438_458f_96dd_e86faeeca2a6 {
/// DWORD->unsigned int
public uint Offset;
/// DWORD->unsigned int
public uint OffsetHigh;
}
public partial class NativeMethods {
/// Return Type: BOOL->int
///hFile: HANDLE->void*
///lpBuffer: LPVOID->void*
///nNumberOfBytesToRead: DWORD->unsigned int
///lpOverlapped: LPOVERLAPPED->_OVERLAPPED*
///lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE
[System.Runtime.InteropServices.DllImportAttribute(
"kernel32.dll", EntryPoint="ReadFileEx")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(
System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool ReadFileEx(
[System.Runtime.InteropServices.InAttribute()]
System.IntPtr hFile, System.IntPtr lpBuffer,
uint nNumberOfBytesToRead, ref OVERLAPPED lpOverlapped,
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine) ;
}
Notat cu 5.0 de 1 persoane
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5