Advertisement
reset_man

111

Apr 18th, 2024
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7. using System.Windows.Forms;
  8. using Microsoft.Win32;
  9.  
  10. namespace Excel2XML
  11. {    public struct GUID_TYPE
  12.     {
  13.         public int Data1;
  14.         public short Data2;
  15.         public short Data3;
  16.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  17.         public byte[] Data4;
  18.     }
  19.  
  20.     public class MakeXML
  21.     {
  22.         [DllImport("ole32.dll")]
  23.         static extern int CoCreateGuid(out Guid guid);
  24.  
  25.         [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
  26.         static extern int StringFromGUID2(Guid guid, IntPtr str, int maxCount);
  27.  
  28.         //созданию GUID и преобразованию его в строку для дальнейшего использования в приложении
  29.         static void MakeGuid()
  30.         {
  31.             GUID_TYPE guid;
  32.             CoCreateGuid(out guid);
  33.  
  34.             const int guidStringSize = 40;
  35.             IntPtr guidString = Marshal.AllocHGlobal(guidStringSize * 2);
  36.             StringFromGUID2(ref guid, guidString, guidStringSize);
  37.  
  38.             string strGuid = Marshal.PtrToStringUni(guidString);
  39.             MessageBox.Show(strGuid);
  40.  
  41.             Marshal.FreeHGlobal(guidString);
  42.         }
  43.  
  44.         static string GetFileName(string Title = "Select file to forming xml....", string InitialPath = null, string MyFilter = "файлы Excel (*.xls*),")
  45.         {
  46.             string res = null;
  47.  
  48.             if (InitialPath != null)
  49.             {
  50.                 try
  51.                 {
  52.                     Environment.CurrentDirectory = InitialPath;
  53.                 }
  54.                 catch
  55.                 {
  56.                     // Обработка ошибки
  57.                 }
  58.             }
  59.  
  60.             OpenFileDialog openFileDialog = new OpenFileDialog
  61.             {
  62.                 Filter = MyFilter,
  63.                 Title = Title,
  64.                 CheckFileExists = true,
  65.                 CheckPathExists = true
  66.             };
  67.  
  68.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  69.             {
  70.                 res = openFileDialog.FileName;
  71.             }
  72.  
  73.             return res ?? "";
  74.         }
  75.  
  76.         static string CreateGuidString()
  77.         {
  78.             Guid guid;
  79.             string strGuid = null;
  80.             int retValue;
  81.  
  82.             const int guidLength = 38; // Длина GUID без фигурных скобок
  83.             retValue = CoCreateGuid(out guid);
  84.  
  85.             if (retValue == 0)
  86.             {
  87.                 IntPtr strPtr = Marshal.AllocHGlobal(guidLength * 2 + 1); // Выделяем память под строку GUID
  88.                 retValue = StringFromGUID2(guid, strPtr, guidLength * 2 + 1);
  89.                 if (retValue > 0) // Проверяем, что получено положительное количество символов
  90.                 {
  91.                     strGuid = Marshal.PtrToStringUni(strPtr, retValue - 1);
  92.                 }
  93.                 Marshal.FreeHGlobal(strPtr); // Освобождаем выделенную память
  94.             }
  95.  
  96.             return strGuid; // Возвращаем строку GUID
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement