Hi, this is an example that writes a text string to a pipe using WriteFile. For P/Invoke you can have several prototypes with different sets of arguments; there is no need to convert to IntPtr yourself. BTW if you provide a string or StringBuilder it will convert Unicode to 8-bit ASCII by default.
private void SendString(StringBuilder sb) {
IntPtr pipe2=CreateFile(pipeName, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
Thread.Sleep(200);
int count;
bool OK=WriteFile(pipe2, sb, sb.Length, out count, 0);
if (!OK) ... report error
ClosePipe(pipe2);
}
// Writes data to a file at the position specified by the file pointer.
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool WriteFile(
IntPtr hHandle, // handle to file
StringBuilder lpBuffer, // data buffer
int nNumberOfBytesToWrite, // number of bytes to write
out int lpNumberOfBytesWritten, // number of bytes written
uint lpOverlapped // overlapped buffer
);
:)
Luc Pattyn [My Articles]