equivalent char* in C#

The C# way of doing this is by letting the marshaler handle the char* stuff while you work with a string: [DllImport(“pjsipDlld”)] static extern int dll_registerAccount( [MarshalAs(UnmanagedType.LPStr)]string username, [MarshalAs(UnmanagedType.LPStr)]string password); Replace LPStr with LPWStr if you’re working with wide chars.

Change keyboard layout from C# code with .NET 4.5.2

Switching the keyboard layout requires some P/Invoke; you´ll need at least the following Windows functions to get it working: LoadKeyboardLayout, GetKeyboardLayout and ActivateKeyboardLayout. The following import declarations worked for me… [DllImport(“user32.dll”, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, EntryPoint = “LoadKeyboardLayout”, SetLastError = true, ThrowOnUnmappableChar = false)] static extern uint LoadKeyboardLayout( StringBuilder pwszKLID, uint flags); [DllImport(“user32.dll”, … Read more

Target 32 Bit or 64 Bit native DLL depending on environment

Here is the solution I’ve used on many projects: name the 32-bit assembly with a “32-bit oriented name”. For example MyAssembly.Native.x86.dll name the 64-bit assembly with a “64-bit oriented name”. For example MyAssembly.Native.x64.dll compile the managed assembly as ‘Any Cpu’ ship everything in the same path Here is how I declare P/Invoke methods: [DllImport(“MyAssembly.Native.x86.dll”, EntryPoint … Read more

PInvoke C#: Function takes pointer to function as argument

You want to use a delegate that matches the method signature of your “MyFunction” C++ method. [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void MyFunctionDelegate(IntPtr frame); [DllImport(“Cortex_SDK.dll”)] public extern static int Cortex_SetDataHandlerFunc( [MarshalAs(UnmanagedType.FunctionPtr)]MyFunctionDelegate functionCallback);

How can I pass a pointer to an array using p/invoke in C#?

The easiest way to pass an array of bytes is to declare the parameter in your import statement as a byte array. [DllImport EntryPoint=”func” CharSet=CharSet.Auto, SetLastError=true] public extern static void Func(byte[]); byte[] ar = new byte[1000]; Func(ar); You should also be able to declare the parameter as an IntPtr and Marshal the data manually. [DllImport … Read more

Set DllImport attribute dynamically

Yes this is possible, you’ll have to do part of the job that the P/Invoke marshaller does. Loading the DLL and finding the entry point of the exported function. Start by declaring a delegate whose signature matches the exported function: private delegate byte start_api(byte pid, byte stat, byte dbg, byte ka); Then use code like … Read more

Show Authentication dialog in C# for windows Vista/7

I managed to implement a solution that is working for me. Here is the source code: [DllImport(“ole32.dll”)] public static extern void CoTaskMemFree(IntPtr ptr); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct CREDUI_INFO { public int cbSize; public IntPtr hwndParent; public string pszMessageText; public string pszCaptionText; public IntPtr hbmBanner; } [DllImport(“credui.dll”, CharSet = CharSet.Auto)] private static extern bool … Read more