public void SetTextureWithAlpha(byte[] imageData) { var imgDataPtr = new AutoPtr <byte[]>(imageData); Gl.BindTexture(TexTarg, TexturePtr); Gl.TexImage2D(TexTarg, 0, AlphaTexForm, Width, Height, 0, AlphaTexPForm, TexPType, imgDataPtr.Address); imgDataPtr.Dispose(); }
public static AutoPtr[] AllocateAuto(this IUnmanagedBufferManager manager, int size) { var ptrs = manager.Allocate(size); var ret = new AutoPtr[ptrs.Length]; for (int i = 0; i < ptrs.Length; i++) { ret[i] = new AutoPtr(ptrs[i], ptr => manager.Free(ptr)); } return(ret); }
/// <summary> /// Creates a service named <paramref name="serviceName"/> and creates an item in Miranda IM contact list. /// </summary> /// <param name="serviceName">Unique name of service to be created.</param> /// <param name="menuItemText">Text of menu item.</param> /// <param name="action">Method to be called when user selects menu item.</param> public ContactListMenuItem(string serviceName, string menuItemText, Action action) { _action = action; _service = Service; Plugin.m_CreateServiceFunction(serviceName, _service); var cListMenuItem = new CListMenuItem { position = -0x7FFFFFFF, flags = 0, name = menuItemText, service = serviceName }; using (var pCListMenuItem = new AutoPtr(Marshal.AllocHGlobal(Marshal.SizeOf(typeof (CListMenuItem))))) { Marshal.StructureToPtr(cListMenuItem, pCListMenuItem, false); Plugin.m_CallService("CList/AddMainMenuItem", IntPtr.Zero, pCListMenuItem); } }
/// <summary> /// Gets contact info string from database. /// </summary> /// <param name="fieldFlag"> /// Flag from ContactInfo.CNF_* list. /// </param> /// <returns> /// String from database interpreted as ANSI. /// </returns> private string GetContactField(byte fieldFlag) { // Create ContactInfo object: var info = new ContactInfo(); info.hContact = hContact; info.dwFlag = fieldFlag; string fieldData = null; using (var pContactInfo = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf(typeof (ContactInfo))))) { // Copy ContactInfo to unmanaged memory: Marshal.StructureToPtr(info, pContactInfo, false); var result = Plugin.m_CallService( "Miranda/Contact/GetContactInfo", IntPtr.Zero, pContactInfo); if (result == IntPtr.Zero) { info = (ContactInfo)Marshal.PtrToStructure(pContactInfo, typeof ( ContactInfo)); fieldData = Marshal.PtrToStringAnsi(info.value.pszVal); } } return fieldData; }
/// <summary> /// Sends simple message to contact. /// </summary> /// <param name="message"> /// Message string to send. /// </param> public void SendMessage(string message) { var eventInfo = new DBEventInfo(); eventInfo.eventType = DBEventInfo.EVENTTYPE_MESSAGE; eventInfo.flags = DBEventInfo.DBEF_SENT; eventInfo.szModule = Protocol; eventInfo.timestamp = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds; using (var pString = new AutoPtr(Marshal.StringToHGlobalAnsi(message))) using (var pDBEventInfo = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf(typeof (DBEventInfo))))) { eventInfo.cbBlob = (uint)message.Length + 1; eventInfo.pBlob = pString; Marshal.StructureToPtr(eventInfo, pDBEventInfo, false); Plugin.m_CallService("DB/Event/Add", hContact, pDBEventInfo); Plugin.m_CallContactService(hContact, "/SendMsg", IntPtr.Zero, pString); } }
/// <summary> /// Called once for options dialog init. /// </summary> /// <param name="wParam"> /// addInfo pointer, must be used in calls to Opt/AddPage Miranda /// service. /// </param> /// <param name="lParam"> /// Not used. /// </param> /// <returns> /// Returns zero on success. /// </returns> private int Initialise(IntPtr wParam, IntPtr lParam) { IntPtr addInfo = wParam; using (var pOptionPage = new AutoPtr(Marshal.AllocHGlobal( Marshal.SizeOf(typeof (OptionsDialogPage))))) { var optionPage = new OptionsDialogPage { position = -800000000, hInstance = hInstance, pszTemplate = new IntPtr(Utils.StubDialogID), pszGroup = groupName, pszTitle = pageName, pfnDlgProc = dlgProc, hLangpack = _hLangpack }; Marshal.StructureToPtr(optionPage, pOptionPage, false); Plugin.m_CallService("Opt/AddPage", addInfo, pOptionPage); } return 0; }
/// <summary> /// Creates instance of history item and loads its content from /// database. /// </summary> /// <param name="contact"> /// Contact with whom history this item is associated. /// </param> /// <param name="hEvent"> /// Miranda event handle. /// </param> internal static HistoryItem Load(Contact contact, IntPtr hEvent) { if (hEvent == IntPtr.Zero) throw new ArgumentException("hEvent cannot be zero."); // Miranda interface for freeing strings: var mmi = MMInterface.GetMMI(); using (var pDbEventInfo = new AutoPtr(Marshal.AllocHGlobal( Marshal.SizeOf(typeof (DBEventInfo))))) { var result = Plugin.m_CallService( "DB/Event/Get", hEvent, pDbEventInfo); if (result != IntPtr.Zero) throw new DatabaseException(); var eventInfo = (DBEventInfo) Marshal.PtrToStructure(pDbEventInfo, typeof (DBEventInfo)); var type = (HistoryItemType)eventInfo.eventType; var direction = (eventInfo.flags & DBEventInfo.DBEF_SENT) != 0 ? HistoryItemDirection.Outgoing : HistoryItemDirection.Incoming; var historyItem = new HistoryItem(type, direction); if (type == HistoryItemType.Message) { // Get message text: using (var pDbEventGetText = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf( typeof (DBEventGetText))))) { var getText = new DBEventGetText(); getText.dbei = pDbEventInfo; getText.datatype = Utils.DBVT_WCHAR; Marshal.StructureToPtr(getText, pDbEventGetText, false); var pString = Plugin.m_CallService( "DB/Event/GetText", IntPtr.Zero, pDbEventGetText); mmi.mmi_free(pString); var message = Marshal.PtrToStringUni(pString); historyItem.MessageText = message; } } historyItem.Contact = contact; historyItem.DateTime = new DateTime(1970, 1, 1) .AddSeconds(eventInfo.timestamp); return historyItem; } }
/// <summary> /// Saves this history item to database for selected contact. /// </summary> public void Save() { var eventInfo = new DBEventInfo(); eventInfo.eventType = (ushort)Type; eventInfo.flags = DBEventInfo.DBEF_SENT; eventInfo.szModule = Contact.Protocol; eventInfo.timestamp = (int)(DateTime - new DateTime(1970, 1, 1)).TotalSeconds; using (var pDbEventInfo = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf(typeof (DBEventInfo))))) { if (MessageText != null) { using (var pString = new AutoPtr(Marshal.StringToHGlobalAnsi(MessageText))) { eventInfo.cbBlob = (uint)MessageText.Length + 1; eventInfo.pBlob = pString; } } Marshal.StructureToPtr(eventInfo, pDbEventInfo, false); Plugin.m_CallService( "DB/Event/Add", Contact.hContact, pDbEventInfo); } }
/// <summary> /// Writes setting to database. /// </summary> /// <param name="hContact"> /// Handle of contact whose setting must be gotten. /// </param> /// <param name="moduleName"> /// Name of module that wrote the setting to get. /// </param> /// <param name="settingName"> /// Name of setting to get. /// <param name="value"> /// Value of one of specified types: byte, ushort, short, uint, int, /// string, byte[]. /// </param> private void SetSetting(IntPtr hContact, string moduleName, string settingName, object value) { using (var pDBContactWriteSetting = new AutoPtr(Marshal.AllocHGlobal( Marshal.SizeOf(typeof (DBContactWriteSetting))))) { var writeSetting = new DBContactWriteSetting(); writeSetting.szModule = moduleName; writeSetting.szSetting = settingName; var variant = new DBVariant(); // This pointer will be set if we allocate unmanaged memory to // store the value: AutoPtr valuePtr = null; if (value is byte) { variant.type = DBVariant.DBVT_BYTE; variant.Value.bVal = (byte)value; } else if (value is ushort) { variant.type = DBVariant.DBVT_WORD; variant.Value.wVal = (ushort)value; } else if (value is short) { variant.type = DBVariant.DBVT_WORD; variant.Value.sVal = (short)value; } else if (value is uint) { variant.type = DBVariant.DBVT_DWORD; variant.Value.dVal = (uint)value; } else if (value is int) { variant.type = DBVariant.DBVT_DWORD; variant.Value.lVal = (int)value; } else if (value is string) { variant.type = DBVariant.DBVT_WCHAR; valuePtr = new AutoPtr( Marshal.StringToHGlobalUni(value as string)); variant.Value.pszVal = valuePtr; } else if (value is byte[]) { var blob = value as byte[]; valuePtr = new AutoPtr(Marshal.AllocHGlobal(blob.Length)); for (int i = 0; i < blob.Length; ++i) Marshal.WriteByte(valuePtr, i, blob[i]); variant.Value.ByteArrayValue.cpbVal = (ushort)blob.Length; variant.Value.ByteArrayValue.pbVal = valuePtr; } else throw new ArgumentException("Type of argument not " + "supported.", "value"); writeSetting.value = variant; Marshal.StructureToPtr(writeSetting, pDBContactWriteSetting, false); IntPtr result = Plugin.m_CallService( "DB/Contact/WriteSetting", hContact, pDBContactWriteSetting); // Free allocated memory: if (valuePtr != null) valuePtr.Dispose(); if (result != IntPtr.Zero) throw new DatabaseException(); } }
/// <summary> /// Reads setting from database for specified contact. Setting can be: /// byte, short, int, string, byte[]. /// </summary> /// <param name="hContact"> /// Handle of contact whose setting must be gotten. /// </param> /// <param name="moduleName"> /// Name of module that wrote the setting to get. /// </param> /// <param name="settingName"> /// Name of setting to get. /// </param> /// <returns> /// Object that may be casted to one of variant types. /// </returns> private object GetSetting(IntPtr hContact, string moduleName, string settingName) { using (var pDBContactGetSetting = new AutoPtr(Marshal.AllocHGlobal( Marshal.SizeOf(typeof (DBContactGetSetting))))) using (var pVariant = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf(typeof (DBVariant))))) { var getSetting = new DBContactGetSetting(); getSetting.szModule = moduleName; getSetting.szSetting = settingName; getSetting.pValue = pVariant; Marshal.StructureToPtr(getSetting, pDBContactGetSetting, false); IntPtr result = Plugin.m_CallService( "DB/Contact/GetSetting", hContact, pDBContactGetSetting); if (result == new IntPtr(2)) throw new SettingDeletedException(); if (result != IntPtr.Zero) throw new DatabaseException(); var variant = Marshal.PtrToStructure(pVariant, typeof (DBVariant)) as DBVariant; switch (variant.type) { case DBVariant.DBVT_DELETED: throw new SettingDeletedException(); case DBVariant.DBVT_BYTE: return variant.Value.bVal; case DBVariant.DBVT_WORD: return variant.Value.sVal; case DBVariant.DBVT_DWORD: return variant.Value.lVal; case DBVariant.DBVT_ASCIIZ: return Marshal.PtrToStringAnsi(variant.Value.pszVal); case DBVariant.DBVT_BLOB: int size = variant.Value.ByteArrayValue.cpbVal; IntPtr pBlob = variant.Value.ByteArrayValue.pbVal; var blob = new byte[size]; for (int i = 0; i < size; ++i) blob[i] = Marshal.ReadByte(pBlob, i); return blob; case DBVariant.DBVT_UTF8: throw new NotImplementedException("UTF-8 decoding " + "still not implemented."); case DBVariant.DBVT_WCHAR: return Marshal.PtrToStringUni(variant.Value.pszVal); default: throw new DatabaseException(); } } }
/// <summary> /// Reads all setting names from database for spedified contact of /// specified module. /// </summary> /// <param name="hContact"> /// Handle of contact whose settings must be gotten. /// </param> /// <param name="moduleName"> /// Name of module that wrote the settings to get. /// </param> /// <returns> /// Array with setting names. /// </returns> private string[] EnumContactSettings(IntPtr hContact, string moduleName) { var result = new List<string>(); using (var pDBContactEnumSettings = new AutoPtr(Marshal.AllocHGlobal( Marshal.SizeOf(typeof (DBContactEnumSettings))))) { var enumSettings = new DBContactEnumSettings(); enumSettings.pfnEnumProc = (settingName, _) => { result.Add(settingName); return 0; }; enumSettings.szModule = moduleName; Marshal.StructureToPtr(enumSettings, pDBContactEnumSettings, false); Plugin.m_CallService("DB/Contact/EnumSettings", hContact, pDBContactEnumSettings); } return result.ToArray(); }
Date_ms() { data = new Date_s(); }