How to call the Win32 GetCurrentDirectory function from C#?

The prototype of GetCurrentDirectory DWORD GetCurrentDirectory( [in] DWORD nBufferLength, [out] LPTSTR lpBuffer ); DWORD is unsigned long, LPTSTR is a pointer to wchar buffer in Unicode environment. It can be called from C++ #define MAX_BUFFER_LENGTH 256 int main() { TCHAR buffer[MAX_BUFFER_LENGTH]; GetCurrentDirectory(MAX_BUFFER_LENGTH, buffer); return 0; } I tried to encapsulate this win32 function in C#,… Read More How to call the Win32 GetCurrentDirectory function from C#?

Why is StreamWriter adding random bytes to a file?

I’m trying to translate a virtual key code with ToAsciiEx() and write it to a debug file. For some reason, the output file contains a load of random trash bytes interspersed with the key codes I want to log. I’m importing ToAsciiEx() like this: [DllImport("user32.dll")] static extern int ToAsciiEx(uint uVirtKey, uint uScanCode, byte[] lpKeyState, [Out]… Read More Why is StreamWriter adding random bytes to a file?

Alias for function from DllImport

[Win32]::ShowWindowAsync<<..>> works, but this doesn’t Add-Type -TypeDefinition @’ using System; // IntPtr using System.Runtime.InteropServices; // DllImport public class Win32 { [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr a, int b); public static bool f_1(IntPtr a, int b) { return (ShowWindowAsync(IntPtr a, int b)); } } ‘@ write-host ([Win32]::f_1((Get-Process -Id $pid).MainWindowHandle, 2)) read-host ‘end’ 12345678901234567890123456789012345678901234567890123456789012345678901234567890 >Solution :… Read More Alias for function from DllImport