Csharp/C Sharp by API/System.Runtime.InteropServices/Marshal
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
Marshal.AllocCoTaskMem
using System;
using System.Runtime.InteropServices;
class MainClass
{
static void Main(string[] args)
{
UsePointers();
}
static unsafe public void UsePointers()
{
char * pMyArray = (char*)Marshal.AllocCoTaskMem(6);
while (*pMyArray != "\0")
{
Console.WriteLine(*pMyArray);
pMyArray++;
}
Marshal.FreeCoTaskMem((IntPtr)pMyArray);
}
}
Marshal.AllocHGlobal
using System;
using System.Runtime.InteropServices;
public class MainClass
{
[STAThread]
static void Main(string[] args)
{
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)));
Marshal.FreeHGlobal(ptr);
}
}
Marshal.FreeHGlobal
using System;
using System.IO;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
public class MainClass
{
public static void Main()
{
IntPtr ptr = Marshal.AllocHGlobal(1024);
if (ptr != IntPtr.Zero){
Marshal.FreeHGlobal(ptr);
ptr = IntPtr.Zero;
}
}
}
Marshal.StringToCoTaskMemAnsi
using System;
using System.Runtime.InteropServices;
using LDAP32;
unsafe class MainClass
{
private static uint LDAP_PORT = 389;
[STAThread]
static void Main(string[] args)
{
LDAP32.LDAP* pLdap = null;
sbyte* pServer = (sbyte*)Marshal.StringToCoTaskMemAnsi("dsaddom.nttest.microsoft.ru");
sbyte* pName = (sbyte*)Marshal.StringToCoTaskMemAnsi("CN=Greg MacBeth,CN=Users,DC=dsaddom,DC=nttest,DC=MICROSOFT,DC=COM");
sbyte* pPassword = (sbyte*)Marshal.StringToCoTaskMemAnsi("gregmcb");
uint Result = 0;
pLdap = Wldap32.ldap_init(pServer, LDAP_PORT);
if(pLdap != null)
{
Result = Wldap32.ldap_simple_bind_s(pLdap, pName, pPassword);
}
Wldap32.ldap_unbind(pLdap);
}
}