-
March 8th, 2009, 11:29 PM
#1
unknown argument problem
hey guys in this program i am trying to access the writefile function by pinvoke but the last argument of the function is i m not able to figure out any one help plz...
Code:
using System.IO;
using Microsoft.Win32.SafeHandles;
using System;
using System.Threading;
using System;
using System.Runtime.InteropServices;
public class PrintFactory
{
public const short FILE_ATTRIBUTE_NORMAL = 0x80;
public const short INVALID_HANDLE_VALUE = -1;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint CREATE_NEW = 1;
public const uint CREATE_ALWAYS = 2;
public const uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("msvcrt")]
static extern int _getch();
[DllImport("kernel32.dll", SetLastError = true)]
static extern unsafe int WriteFile(IntPtr handle, IntPtr buffer,
int numBytesToWrite, IntPtr numBytesWritten, NativeOverlapped* lpOverlapped);
public enum EMoveMethod : uint
{
Begin = 0,
Current = 1,
End = 2
}
// [DllImport("coredll")]
//static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
// out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern unsafe uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] int* lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] out int lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);
[DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Ansi)]
static extern bool WriteFile(IntPtr hFile, System.Text.StringBuilder lpBuffer,
uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
[In] ref System.Threading.NativeOverlapped lpOverlapped);
[DllImport("kernel32.dll")]
static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer,
uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
[In] ref System.Threading.NativeOverlapped lpOverlapped);
public PrintFactory()
{
//CreateFileA("\\\\.\\N:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
IntPtr ptr = CreateFile("\\\\.\\N:", GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
/* Is bad handle? INVALID_HANDLE_VALUE */
if (ptr.ToInt32() == -1)
{
/* ask the framework to marshall the win32 error code to an exception */
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
else
{
String Temp = "This is a test print";
Byte[] Buff = new Byte[512];
for (int i = 0; i < 512; i++)
Buff[i] = 1;
//Check to see if your printer support ASCII encoding or Unicode.
FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
//If unicode is supported, use the following:
Buff = System.Text.Encoding.Unicode.GetBytes(Temp);
// Buff = System.Text.Encoding.ASCII.GetBytes(Temp);
uint outt; IntPtr p=(IntPtr)0;
WriteFile(ptr,Buff,512,out outt,&0);
// Buff = System.Text.Encoding.ASCII.GetBytes(prnCmdCut);
// lpt.Write(Buff, 0, Buff.Length);
lpt.Close();
Console.Write(Temp);
_getch();
}
}
}
class enter
{
public static void Main()
{
new PrintFactory();
}
}
-
March 9th, 2009, 12:19 AM
#2
Re: unknown argument problem
Why you do not use the IO methods from .NET???
Writing:
Code:
// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");
// write a line of text to the file
tw.WriteLine(DateTime.Now);
// close the stream
tw.Close();
Reading:
Code:
// create reader & open file
Textreader tr = new StreamReader("date.txt");
// read a line of text
Console.WriteLine(tr.ReadLine());
// close the stream
tr.Close();
For setting file attributes:
http://www.csharp-examples.net/file-attributes/
-
March 9th, 2009, 12:24 AM
#3
Re: unknown argument problem
actualy m trying to read physical drive not files
-
March 9th, 2009, 01:00 AM
#4
Re: unknown argument problem
actualy m trying to read physical drive not files
-
March 9th, 2009, 02:40 AM
#5
Re: unknown argument problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|