using System; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.IO; namespace PACNET { /// MISC API public class MISC { //12.1. AnsiString /// /// This function can convert unicode string to ansi byte array. /// /// [in] A pointer to a buffer that stores unicode string. /// Return ansi byte array. public static byte[] AnsiString([In] string str) { Encoding ascii = Encoding.ASCII; Encoding unicode = Encoding.Unicode; byte[] unicodeBytes = unicode.GetBytes(str); byte[] temp = Encoding.Convert(unicode, ascii, unicodeBytes); byte[] ret = new byte[temp.Length + 1]; temp.CopyTo(ret, 0); return ret; } //12.2. PAC_API void pac_AnsiToWideString(LPCSTR astr, LPTSTR wstr) //12.3. PAC_API void pac_DoEvents() /// /// When you run a Windows Form, it creates the new form, which then waits for events /// to handle. Each time the form handles an event, it processes all the code associated /// with that event. All other events wait in the queue. While your code handles the event, /// your application does not respond. If you call pac_DoEvents in your code, your /// application can handle the other events. /// [DllImport("PACSDK.dll", EntryPoint = "pac_DoEvents")] public static extern void DoEvents(); //12.4. PAC_API void pac_WideStringToAnsi(const TCHAR *wstr, LPSTR astr) //12.5. WideString /// /// This function can convert ansi byte array to unicode string. /// /// [in] A pointer to a buffer that stores ansi byte array. /// Return unicode string. public static string WideString(byte[] CharStr) { Encoding ascii = Encoding.ASCII; char[] asciiChars = new char[ascii.GetCharCount(CharStr, 0, CharStr.Length)]; ascii.GetChars(CharStr, 0, CharStr.Length, asciiChars, 0); string asciiString = new string(asciiChars); return asciiString; } [DllImport("coredll.dll")] private static extern uint GetModuleFileName(IntPtr hModule, StringBuilder lpFileName, int nSize); /// get app path public static string GetAppPath() { string temp = Assembly.GetExecutingAssembly().GetName().CodeBase; return Path.GetDirectoryName(temp); } /// get current directory public static string GetCurrentDirectory() { string currDir = ""; StringBuilder sb = new StringBuilder(256); if (GetModuleFileName(IntPtr.Zero, sb, sb.Capacity) > 0) currDir = sb.ToString(); else return ""; return Path.GetDirectoryName(currDir); } } }