CreateConsolScreenBuffer

G

Guest

Really have problems when converting "CreateConsolScreenBuffer" to C#. Can
anyone give me an example for the function ?

typedef struct _SECURITY_ATTRIBUTES
{ DWORD nLength;
LPVOID lpSecurityDescriptor; // not sure abt this part
BOOL bInheritHandle;
}
transfer to the following -----> is that correct ?
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;

--------------------------------------------------------------------------------------------
//The CreateConsoleScreenBuffer function creates a console screen buffer.
HANDLE CreateConsoleScreenBuffer
(
DWORD dwDesiredAccess,
DWORD dwShareMode,
const SECURITY_ATTRIBUTES* lpSecurityAttributes,
DWORD dwFlags,
LPVOID lpScreenBufferData
);
transfer to the following -----> is that correct ?
[DllImport("Kernel32.dll")]
private static extern int CreateConsoleScreenBuffer(int dwDesiredAccess, int
dwShareMode, SECURITY_ATTRIBUTES obj, int dwFlags, object lpScreenBufferData)
-------------------------------------------------------------------------------------
//the statement execution.
int hNewScreenBuffer = CreateConsoleScreenBuffer(
GENERIC_READ | // read/write access
GENERIC_WRITE,
0, // not shared
NULL, // no security attributes
CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE
NULL); // reserved; must be NULL
 
C

cody

[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}

explicit size with SizeConst=? would be more safe. try
sizeof(SECURITY_ATTRIBUTES) to find out exact size.
[DllImport("Kernel32.dll")]
private static extern int CreateConsoleScreenBuffer(int dwDesiredAccess, int
dwShareMode, SECURITY_ATTRIBUTES obj, int dwFlags, object
lpScreenBufferData);

You must pass SECURITY_ATTRIBUTES as ref or out to make it work. The
lpScreenBufferData should be of type byte[] I think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top