Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using Microsoft.Win32;
- namespace Excel2XML
- { public struct GUID_TYPE
- {
- public int Data1;
- public short Data2;
- public short Data3;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
- public byte[] Data4;
- }
- public class MakeXML
- {
- [DllImport("ole32.dll")]
- static extern int CoCreateGuid(out Guid guid);
- [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
- static extern int StringFromGUID2(Guid guid, IntPtr str, int maxCount);
- //созданию GUID и преобразованию его в строку для дальнейшего использования в приложении
- static void MakeGuid()
- {
- GUID_TYPE guid;
- CoCreateGuid(out guid);
- const int guidStringSize = 40;
- IntPtr guidString = Marshal.AllocHGlobal(guidStringSize * 2);
- StringFromGUID2(ref guid, guidString, guidStringSize);
- string strGuid = Marshal.PtrToStringUni(guidString);
- MessageBox.Show(strGuid);
- Marshal.FreeHGlobal(guidString);
- }
- static string GetFileName(string Title = "Select file to forming xml....", string InitialPath = null, string MyFilter = "файлы Excel (*.xls*),")
- {
- string res = null;
- if (InitialPath != null)
- {
- try
- {
- Environment.CurrentDirectory = InitialPath;
- }
- catch
- {
- // Обработка ошибки
- }
- }
- OpenFileDialog openFileDialog = new OpenFileDialog
- {
- Filter = MyFilter,
- Title = Title,
- CheckFileExists = true,
- CheckPathExists = true
- };
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- res = openFileDialog.FileName;
- }
- return res ?? "";
- }
- static string CreateGuidString()
- {
- Guid guid;
- string strGuid = null;
- int retValue;
- const int guidLength = 38; // Длина GUID без фигурных скобок
- retValue = CoCreateGuid(out guid);
- if (retValue == 0)
- {
- IntPtr strPtr = Marshal.AllocHGlobal(guidLength * 2 + 1); // Выделяем память под строку GUID
- retValue = StringFromGUID2(guid, strPtr, guidLength * 2 + 1);
- if (retValue > 0) // Проверяем, что получено положительное количество символов
- {
- strGuid = Marshal.PtrToStringUni(strPtr, retValue - 1);
- }
- Marshal.FreeHGlobal(strPtr); // Освобождаем выделенную память
- }
- return strGuid; // Возвращаем строку GUID
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement